The simple example of CLASS in C++, To find the addition of two number using the class:
Program Code:
#include <iostream>
using namespace std;
class add
{
private:
int a,b,addi;
public:
void get_data(int ,int );
void sum();
void display();
};
void add::get_data(int x,int y)
{
a=x;
b=y;
}
void add::sum()
{
addi=a+b;
}
void add::display()
{
cout<<"Sum of two number is :"<<addi<<endl;
}
int main()
{
add obj1,obj2;
int a,b;
cin>>a>>b;
obj1.get_data(a,b);
obj1.sum();
obj1.display();
return 0;
}
using namespace std;
class add
{
private:
int a,b,addi;
public:
void get_data(int ,int );
void sum();
void display();
};
void add::get_data(int x,int y)
{
a=x;
b=y;
}
void add::sum()
{
addi=a+b;
}
void add::display()
{
cout<<"Sum of two number is :"<<addi<<endl;
}
int main()
{
add obj1,obj2;
int a,b;
cin>>a>>b;
obj1.get_data(a,b);
obj1.sum();
obj1.display();
return 0;
}
The output of the program is tested on www.jdoodle.com
Output:
1
2
Sum of two number is : 3
2
Sum of two number is : 3
Thanks
Mukesh Rajput
Mukesh Rajput
Post A Comment:
0 comments: