Rectangle
InterviewTime limit1sMemory limit512 MB
Implement a Rectangle class with width and height, range-checked setters, and methods for area, perimeter, and square detection.
- Level
Easy1 of 10
- Topics
- Implementation, Math, Simulation, Matrix
- Solved
- No attempts yet
Problem
Implement a class Rectangle that stores the information of a rectangle.
-
C++14, C++17
-
Class name:
Rectangle -
Constructor
Rectangle(width, height): sets the width of the rectangle towidthand the height toheight.widthis a positive integer less than or equal to 1,000, andheightis a positive integer less than or equal to 2,000.
-
Member variables
int width: the width of the rectangleint height: the height of the rectangle
-
Member functions
int get_width() const: returns the width of the rectangle.int get_height() const: returns the height of the rectangle.void set_width(int width): changes the width of the rectangle towidth. Ifwidthis less than or equal to 0, or greater than 1,000, the width is not changed.void set_height(int height): changes the height of the rectangle toheight. Ifheightis less than or equal to 0, or greater than 2,000, the height is not changed.int area() const: returns the area of the rectangle.int perimeter() const: returns the perimeter of the rectangle.bool is_square() const: returnstrueif the rectangle is a square,falseotherwise.
-