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