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;
public:
int add ( int p, int q)
{
a=p;
b=q;
int c=a+b;
return c;
}
};
int main()
{
addition sum;
int x,y,z;
cin>>x>>y;
z=sum.add(x,y);
cout<<"Sum="<<z;
}
using namespace std;
class addition
{
int a,b;
public:
int add ( int p, int q)
{
a=p;
b=q;
int c=a+b;
return c;
}
};
int main()
{
addition sum;
int x,y,z;
cin>>x>>y;
z=sum.add(x,y);
cout<<"Sum="<<z;
}
The output of the program is tested on www.jdoodle.com
Output:
10
20
Sum = 30
20
Sum = 30
Thanks
Mukesh Rajput
Post A Comment:
0 comments: