The simple example of CLASS in C++, To find the area of the rectangle:
Program Code:
#include <iostream>
using namespace std;
class rect
{
int b,l,a;
public:
void getdata()
{
cout<<"enter length";
cin>>l;
cout<<"enter breadth";
cin>>b;
}
void area()
{
a=l*b;
}
void display()
{
cout<<"area of rectangle= "<<a;
}
};
int main()
{
rect obj;
obj.getdata();
obj.area();
obj.display();
return 0;
}
using namespace std;
class rect
{
int b,l,a;
public:
void getdata()
{
cout<<"enter length";
cin>>l;
cout<<"enter breadth";
cin>>b;
}
void area()
{
a=l*b;
}
void display()
{
cout<<"area of rectangle= "<<a;
}
};
int main()
{
rect obj;
obj.getdata();
obj.area();
obj.display();
return 0;
}
The output of the program is tested on www.jdoodle.com
Output:
enter length 5
enter breadth 7
area of rectangle= 200
enter breadth 7
area of rectangle= 200
Thanks
Mukesh Rajput
Post A Comment:
0 comments: