Answer to Question #293578 in C++ for Ianidy

Question #293578

a. Create a class called Rectangle with data members: width, length and area. The class should have functions to allow for setting of both length and width, calculate the area, and retrieve the width, length and area. (13 marks)



b. Write a main function that declares an object from the class above, and use the methods in that class to input the length and width, calculate the area and, retrieve and display those values (width, length and area).




1
Expert's answer
2022-02-04T07:59:46-0500
#include<iostream>

using namespace std;

class Rectangle
{
	double width;
	double length;
	double area;
public:
	Rectangle(){}
	void SetWidth()
	{
		cout<<"Please, enter width of rectangle: ";
		cin>>width;
	}
	void SetLength()
	{
		cout<<"Please, enter length of rectangle: ";
		cin>>length;
	}
	void CalcArea()
	{
		area=width*length;
	}
	
	double GetWidth(){return width;}
	double GetLength(){return length;}
	double GetArea(){return area;}
};

int main()
{
	Rectangle rct;
	rct.SetWidth();
	rct.SetLength();
	rct.CalcArea();
	cout<<"\nThe width of the rectangle is "<<rct.GetWidth();
	cout<<"\nThe length of the rectangle is "<<rct.GetLength();
	cout<<"\nThe area of the rectangle is "<<rct.GetArea();
}

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