Create a class named 'Rectangle' with two private float data members- length and breadth and a public function calculate to calculate and print the area which is 'length*breadth'. The class has three constructors which are :
1 - having no parameter - values of both length and breadth are assigned zero.
2 - having two numbers as parameters - the two numbers are assigned as length and breadth respectively.
3 - having one number as parameter - both length and breadth are assigned that number.
In main method, create objects r1,r2 & r3 of the 'Rectangle' class having none, one and two parameters(receive input from user) respectively. Finally call the calculate method for objects r1,r2 &r3.
Input format:
First line contains a input for one parameter constructor.
Second line contains two inputs for two parameter constructor.
Output format:
First line contains output of r1 object area.
Second line contains output of r2 object area.
Third line contains output of r3 object area.
#include <iostream>
class Rectangle
{
public:
Rectangle() {}
explicit Rectangle(float i) : length(i), breadth(i)
{}
Rectangle(float a, float b) :length(a), breadth(b)
{}
void Calculate() { std::cout << length * breadth<<std::endl; }
private:
float length {0},
breadth {0};
};
int main()
{
Rectangle r1;
Rectangle r2{ 10 };
Rectangle r3{ 15,45 };
r1.Calculate();
r2.Calculate();
r3.Calculate();
return 0;
}
Comments
Leave a comment