Rectangle Area

  • + 1 comment

    Here's my code, pretty simple, easy to understand. The comments will help you :)

    class Rectangle {
        public:
            int width;    // Represents the width of the rectangle
            int height;   // Represents the height of the rectangle
    
            // Virtual function for displaying rectangle properties
            virtual void display() {
                cout << width << " " << height << endl; 
            }
    };
    
    class RectangleArea : public Rectangle { 
        public:
            void read_input() { 
                cin >> width; // Reads the width from the user input
                cin >> height; // Reads the height from the user input
            }
    
            // Overrides the display() method from the base class
            void display() override { 
                cout << width * height << endl; // Displays the area of the rectangle 
            }
    };