In: Computer Science
IN C++!!!
Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods:
Now design and implement a test program to create two rectangle objects: one with default height and width, and the second is 5 units high and 6 units wide. Next, test the class methods on each object to print the information as shown below.
Program:
#include<iostream>
using namespace std;
class Rectangle
{
public:
double height,width;
Rectangle()
{
height=0;
width=0;
}
Rectangle(double ht,double wd)
{
height=ht;
width=wd;
}
double getHeight()
{
return height;
}
double getWidth()
{
return width;
}
double getArea()
{
return height*width;
}
double getPerimeter()
{
return 2*height+2*width;
}
};
int main()
{
Rectangle rect1;
cout<<endl;
cout<<"Rectangle Object1: "<<endl;
cout<<"---------------------"<<endl;
cout<<"Height: "<<rect1.getHeight()<<endl;
cout<<"Width: "<<rect1.getWidth()<<endl;
cout<<"Area: "<<rect1.getArea()<<endl;
cout<<"Perimeter:
"<<rect1.getPerimeter()<<endl;
cout<<endl;
Rectangle rect2(5,6);
cout<<endl;
cout<<"Rectangle Object2: "<<endl;
cout<<"---------------------"<<endl;
cout<<"Height: "<<rect2.getHeight()<<endl;
cout<<"Width: "<<rect2.getWidth()<<endl;
cout<<"Area: "<<rect2.getArea()<<endl;
cout<<"Perimeter:
"<<rect2.getPerimeter()<<endl;
cout<<endl;
return 0;
}
Output:
Note: "Next, test the class methods on each object to print the information as shown below." nothing is there in your question. still I tried so I hope it will be helpful for you!!