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 addition
{
int a,b;
public:
void getdata()
{
cout<<"Enter first number: ";
cin>>a;
cout<<"Enter second number: ";
cin>>b;
}
void add()
{
int sum=a+b;
cout<<"The sum is = "<<sum<<endl;
}
};
int main()
{
addition a1;
a1.getdata();
a1.add();
}
using namespace std;
class addition
{
int a,b;
public:
void getdata()
{
cout<<"Enter first number: ";
cin>>a;
cout<<"Enter second number: ";
cin>>b;
}
void add()
{
int sum=a+b;
cout<<"The sum is = "<<sum<<endl;
}
};
int main()
{
addition a1;
a1.getdata();
a1.add();
}
The output of the program is tested on www.jdoodle.com
Output:
Enter first number: 11
Enter second number: 21
Sum of two number is : 32
Enter second number: 21
Sum of two number is : 32
Thanks
Mukesh Rajput
Mukesh Rajput
Post A Comment:
0 comments: