Implement the C++ class Rectangle below. write C++ code to test the class in the
ID: 3840609 • Letter: I
Question
Implement the C++ class Rectangle below. write C++ code to test the class in the case of Rectangle object: l. Centered at the origin, and has width = 6, height = 4 using the class constructor 2. Centered at (x, y) = (1, 1) and has width = height = 5 using the class shift and reset methods #includ e using namespace std; class Rectangle {public: Rectangle(double center_x, double center_y, double width, double height);//Constructor double area();//Return area of a Rectangle object double perimeter();//Return perimeter of a Rectangle object void shift(double shift_x, double shift_y);//Shift a Rectangle object from the center by the amount {shift _x, shift y) void reset(double width, double height);//Mutate function double get();//Accessor function private: double center_x, center_y double width', double height-, };Explanation / Answer
#include <iostream>
using namespace std;
class Rectangle
{
public:
Rectangle(double center_x,double center_y,double weight,double height)
{
this->center_x=center_x;
this->center_y=center_y;
this->weight=weight;
this->height=height;
}
double area()
{
return weight*height;
}
double perimeter()
{
return 2*(weight+height);
}
void shift(double shift_x,double shift_y)
{
center_x+=shift_x;
center_y+=shift_y;
}
void reset(double weight, double height)
{
this->weight=weight;
this->height=height;
}
double getWeight()
{
return weight;
}
double getHeight()
{
return height;
}
private:
double center_x,center_y;
double weight;
double height;
};
int main()
{
Rectangle rect(1,2,3,5);
cout << "Area: " <<rect.area()<< endl;
cout << "Perimeter: " <<rect.perimeter()<< endl;
return 0;
}
OUTPUT:
Area: 15
Perimeter: 16
Process returned 0 (0x0) execution time : 0.047 s
Press any key to continue.