Answer to Question #173785 in C++ for Rocky

Question #173785

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.



1
Expert's answer
2021-03-20T23:10:44-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS