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
#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();
}
Comments
Leave a comment