Rectangle

Interview

Time limit1sMemory limit512 MB

Summary
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 to width and the height to height. width is a positive integer less than or equal to 1,000, and height is a positive integer less than or equal to 2,000.
    • Member variables

      • int width: the width of the rectangle
      • int 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 to width. If width is 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 to height. If height is 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: returns true if the rectangle is a square, false otherwise.

Examples1

  1. Example 1

    Input
    1 1
    
    Expected output
    1 1 1 4 1
    100 100 10000 400 1
    100 100 10000 400 1
    100 100 10000 400 1