Answer to Question #324204 in C++ for abc

Question #324204

Create a rectangle class having length and width as parameter. Write getter/setter for each parameter. Write default constructor that should initialize the default value to the length and width.. Write member functions to calculate area and another function to calculate perimeter in class. Write a function to print the information regarding rectangle which include length, width, area and parameter.


Formula to calculate area = 0.5 * length * width

Formula to calculate perimeter=2 * length+ 2 * width

1
Expert's answer
2022-04-05T13:17:48-0400
#include <iostream>

using namespace std;

class rectangle
{
	float length;
	float width;
public:
	rectangle():length(0),width(0){}
	void SetLength()
	{
		cout << "Please, enter a length of rectangle: ";
		cin >> length;
	}
	void SetWidth()
	{
		cout << "Please, enter a width of rectangle: ";
		cin >> width;
	}
	float GetLength() {return length;}
	float GetWidth() {return width;}
	float Area()
	{
		return (0.5*length*width);
	}
	float Perimetr()
	{
		return (2 * length + 2 * width);
	}
	void Display()
	{
		cout << "\nLength of rectangle is " << length
			<< "\nWidth of rectangle is " << width
			<< "\nArea of rectangle is " << Area()
			<< "\nPerimetr of rectangle is " << Perimetr();
	}
};

int main()
{
	rectangle rect;
	cout << "Default rectangle: "<<endl;
	rect.Display();
	cout << endl;
	rect.SetLength();
	rect.SetWidth();
	cout<<"\nLength: "<<rect.GetLength();
	cout << "\nWidth: " << rect.GetWidth();
	rect.Display();
}



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