The simple example of CLASS in C++, To find the addition of two number using class:

Program Code:
#include <iostream>
using namespace std;
class addition
{
int a,b,sum;
public:
void getdata(int x, int y);
void plus();
void display();
};
void addition :: getdata(int x, int y)
{
a=x;
b=y;
}
void addition :: plus()
{
sum=a+b;
}
void addition :: display()
{
cout<<"The sum is = "<<sum<<endl;
}
int main() 
{
int one, two;
addition a1;
cout<<"Enter 1st no. :";
cin>>one;
cout<<"Enter 2nd no. :";
cin>>two;
a1.getdata(one,two);
a1.plus();
a1.display();
}

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

Output:
Enter 1st no. : 10
Enter 2nd no. : 20
The sum is = 30


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: