A C++ program to print the area of a rectangle by creating a class named Area having two functions, first function named as Setdim takes the length and width of rectangle as parameters and second function named as getArea returns the area of the rectangle, length and width are entered through keyboard.
#include<iostream>
using namespace std;
class Area
{
float length;
float width;
public:
Area(){}
void Setdim()
{
cout << "Please, enter a length: ";
cin >> length;
cout << "Please, enter a width: ";
cin >> width;
}
float getArea()
{
return length*width;
}
};
int main()
{
Area a;
a.Setdim();
cout << "The area of a rectangle is " << a.getArea();
}
Comments
Leave a comment