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();
}
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
Enter 2nd no. : 20
The sum is = 30
Thanks
Mukesh Rajput
Mukesh Rajput
Post A Comment:
0 comments: