In: Computer Science
Use DevC++ to implement a program uses objected oriented programming to calculate the area of a rectangle. The program should create a Rectangle class that has the following attributes and member functions:
Attributes:
width and length
Member functions:
setWidth(), setLength(), getWidth() , getLength(), getArea()
Where width and length are the respect width and length of a Rectangle class. The setWidth() and setLength() member function should set the length and width, the getWidth(), getLenght(), and getArea() member function should get the length, width, and area. The program should
1. Ask the user for the length and width of rectangle.
2. Output the length, width, and area of a rectangle.
3. Continue to ask the user to calculate another rectangle area by asking length and width.
C++ ONLY
#include<iostream>
using namespace std;
//rectangle class
class rectangle
{
private: //attributes of the rectangle
double width;
double length;
public:
void setWidth(double)
;//method to set the width
void setLength(double); //method to set
the length
double getLength(); //method to get the
length
double getWidth();//method to get the
width
double getArea();//method to return the
area
};
//definations of the methods()
void rectangle :: setLength(double l)
{
length = l;
}
void rectangle :: setWidth(double w)
{
width=w;
}
double rectangle :: getLength()
{
return length;
}
double rectangle :: getWidth()
{
return width;
}
double rectangle :: getArea()
{
return length * width;
}
//driver program
int main()
{
rectangle robj;//declaration of object
double a,b,ar;
char ch;
//infinite loop
while(1)
{
cout<<endl<<"Enter the length of
rectangle" ;
cin>>a; //input the length
cout<<endl<<"Enter the width of
rectangle";
cin>>b; //input the width
//validate the length
while(a<=0)
{
cout<<endl<<"Invalid
length";
cout<<endl<<"Enter the
length of rectangle" ;
cin>>a;
}
//validate the
width
while(b<=0)
{
cout<<endl<<"Invalid
Width";
cout<<endl<<"Enter the
Width of rectangle" ;
cin>>b;
}
//set the length
robj.setLength(a);
//set the width
robj.setWidth(b);
//display the details
cout<<endl<<"Length of the Rectangle
: "<<robj.getLength()<<" units.";
cout<<endl<<"Width of the Rectangle
: "<<robj.getWidth()<<" units.";
cout<<endl<<"Area of the Rectangle :
"<<robj.getArea()<<" Sq. units.";
//ask for choice
cout<<endl<<"Do you want to find
more area[y/n]";
cin>>ch;
if(ch=='y' || ch=='Y')
continue;
else
break;
}
}
output