Setter and Getter Function in Object Oriented Programming Using C++
In Object Oriented Programming C++ Setter and Getter Function is Very Use Full in Classes
Setter Function is Using Set The Value of Data Members
Getter Functin is Using Get The value of Data Member
Example 1:Program Code
#include<iostream>
#include<string.h>
using namespace std;
class Book{
private:
int page;
float price;
char name[40];
public:
void setPage(int p){
page = p;
}
void setPrice(float pr){
price = pr;
}
void setName(char *n){
strcpy(name,n);
}
void display(){
cout<<"The Page is:"<<page<<endl;
cout<<"The price is:"<<price<<endl;
cout<<"The name is :"<<name;
}
};
main(){
Book b1;
cout<<"-------------Book Data--------------"<<endl;
b1.setPage(100);
b1.setPrice(500);
b1.setName("Object Oriented Programming");
b1.display();
}
Example 2: Program Code
#include<iostream>
#include<string.h>
using namespace std;
class Book{
private:
int page;
float price;
char name[40];
public:
void setPage(int p){
page = p;
}
void setPrice(float pr){
price = pr;
}
void setName(char *n){
strcpy(name,n);
}
int getPage(){
return page;
}
float getPrice(){
return price;
}
string getName(){
return name;
}
};
main(){
Book b1, b2;
cout<<"-------------Book 1 Data--------------"<<endl;
b1.setPage(100);
b1.setPrice(500);
b1.setName("Object Oriented Programming");
cout<<"The page is:"<<b1.getPage()<<endl;
cout<<"The price is:"<<b1.getPrice()<<endl;
cout<<"The Name is:"<<b1.getName()<<endl;
cout<<"-------------Book 2 Data--------------"<<endl;
b2.setPage(150);
b2.setPrice(650);
b2.setName("Java Programming");
cout<<"The page is:"<<b2.getPage()<<endl;
cout<<"The price is:"<<b2.getPrice()<<endl;
cout<<"The Name is:"<<b2.getName()<<endl;
}
Related Video:
Program Examples:
Data Member and Member Function in Class using Object Oriented Programming
ATM Bank Accounts Program Using Structure in C++
Print All Prime Number in a Between Two Number Intervals Using While Loop in C++
Setter Function is Using Set The Value of Data Members
Getter Functin is Using Get The value of Data Member
Example 1:Program Code
#include<iostream>
#include<string.h>
using namespace std;
class Book{
private:
int page;
float price;
char name[40];
public:
void setPage(int p){
page = p;
}
void setPrice(float pr){
price = pr;
}
void setName(char *n){
strcpy(name,n);
}
void display(){
cout<<"The Page is:"<<page<<endl;
cout<<"The price is:"<<price<<endl;
cout<<"The name is :"<<name;
}
};
main(){
Book b1;
cout<<"-------------Book Data--------------"<<endl;
b1.setPage(100);
b1.setPrice(500);
b1.setName("Object Oriented Programming");
b1.display();
}
Example 2: Program Code
#include<iostream>
#include<string.h>
using namespace std;
class Book{
private:
int page;
float price;
char name[40];
public:
void setPage(int p){
page = p;
}
void setPrice(float pr){
price = pr;
}
void setName(char *n){
strcpy(name,n);
}
int getPage(){
return page;
}
float getPrice(){
return price;
}
string getName(){
return name;
}
};
main(){
Book b1, b2;
cout<<"-------------Book 1 Data--------------"<<endl;
b1.setPage(100);
b1.setPrice(500);
b1.setName("Object Oriented Programming");
cout<<"The page is:"<<b1.getPage()<<endl;
cout<<"The price is:"<<b1.getPrice()<<endl;
cout<<"The Name is:"<<b1.getName()<<endl;
cout<<"-------------Book 2 Data--------------"<<endl;
b2.setPage(150);
b2.setPrice(650);
b2.setName("Java Programming");
cout<<"The page is:"<<b2.getPage()<<endl;
cout<<"The price is:"<<b2.getPrice()<<endl;
cout<<"The Name is:"<<b2.getName()<<endl;
}
Related Video:
Program Examples:
Data Member and Member Function in Class using Object Oriented Programming
ATM Bank Accounts Program Using Structure in C++
Print All Prime Number in a Between Two Number Intervals Using While Loop in C++
Post a Comment