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;
}


The output of the program is tested on www.jdoodle.com

Output:
1
2
Sum of two number is : 3


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: