The simple example of CLASS in C++, To find the area of the rectangle:
Program Code:
#include <iostream>
using namespace std;
class Rectangle
{
int l,b,c,area;
public:
void getData()
{
cout<<"Enter- the Length of the rectangle";
cin>>l;
cout<<"Enter the breath of the rectangle";
cin>>b;
}
void arearec()
{
area=l*b;
}
void displayData()
{
cout<<"Area of the given Rectangle is "<<area;
}
};
int main()
{
Rectangle obj;
obj.getData();
obj.arearec();
obj.displayData();
}
using namespace std;
class Rectangle
{
int l,b,c,area;
public:
void getData()
{
cout<<"Enter- the Length of the rectangle";
cin>>l;
cout<<"Enter the breath of the rectangle";
cin>>b;
}
void arearec()
{
area=l*b;
}
void displayData()
{
cout<<"Area of the given Rectangle is "<<area;
}
};
int main()
{
Rectangle obj;
obj.getData();
obj.arearec();
obj.displayData();
}
The output of the program is tested on www.jdoodle.com
Output:
Enter- the Length of the rectangle 5
Enter the breath of the rectangle 7
Area of the given Rectangle is 35
Enter the breath of the rectangle 7
Area of the given Rectangle is 35
Thanks
Mukesh Rajput
Post A Comment:
0 comments: