The simple example of CLASS in C++, To find the area of the rectangle:
Program Code:
#include <iostream>
using namespace std;
class rectangle
{
int a;
int b;
int s;
public:
void area(int x, int y);
void display();
};
void rectangle::area(int x, int y)
{
a=x;
b=y;
s=a*b;
}
void rectangle::display()
{
cout<<"The Area is:"<<s;
cout<<"\n";
}
int main()
{
int x;
int y;
cin>>x;
cin>>y;
rectangle obj1;
obj1.area(x,y);
obj1.display();
}
using namespace std;
class rectangle
{
int a;
int b;
int s;
public:
void area(int x, int y);
void display();
};
void rectangle::area(int x, int y)
{
a=x;
b=y;
s=a*b;
}
void rectangle::display()
{
cout<<"The Area is:"<<s;
cout<<"\n";
}
int main()
{
int x;
int y;
cin>>x;
cin>>y;
rectangle obj1;
obj1.area(x,y);
obj1.display();
}
The output of the program is tested on www.jdoodle.com
Output:
10
20
The Sum is: 200
20
The Sum is: 200
Thanks
Mukesh Rajput
Post A Comment:
0 comments: