Write the definition ofa class named Rectangleto represents rectangles. The clas
ID: 3608400 • Letter: W
Question
Write the definition ofa class named Rectangleto represents rectangles. The classshould contain two
private datafields int widthand int height. The defaultvalues for widthand height are both1.
Your class shouldcontain the following public methods:
•Rectangle(): the default constructor which constructs a defaultrectangle.
•Rectangle(int w, inth): a constructor whichconstructs a rectangle with the given width and
height.
•void setWidth(int w)
•int getWidth()
•void setHeight(int h)
•int getHeight()
•int getArea()
•int getPerimeter()
•bool isSquare()
In addition, you should write acomplete set of unit tests to test your class.
Explanation / Answer
please be sure to rate, thanks #include using namespace std; class Rectangle { private: int height, width; public: Rectangle() { height = 1; width = 1; } Rectangle(int w, int h) { height = h; width =w; } void setWidth(int w) { width = w; } intgetWidth() { return width; } voidsetHeight(int h) { height = h; } intgetHeight() { return height; } intgetArea() { return (height*width); } intgetPerimeter() { return ((height*2)+(width*2)); } boolisSquare() { if(width==height) return true; return false; } }; int main() { Rectangle rect, rect2(10,10); rect.setHeight(5); rect.setWidth(3); coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.