Rectangle Area

Sort by

recency

|

197 Discussions

|

  • + 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 
            }
    };
    
  • + 0 comments
    class Rectangle{
        public:
        int height,width;
        void display(){
            cout<<width<<" "<<height<<endl;
        }
    };
    class RectangleArea: public Rectangle{
        public:
        void read_input(){
            cin>>width>>height;
        }
        void display(){
            cout<<(width*height);
        }
    };
    
  • + 0 comments

    include

    using namespace std;

    class Rectangle{

    public:
    int field_width;
    int field_height;
    

    void display(){ cout< class RectangleArea : public Rectangle{ public: void read_input(){ cin>>field_width; cin>>field_height; } int sum(){ return field_width * field_height; }
    };

    int main(){

    RectangleArea obj1;
    
    obj1.read_input();
    int value = obj1.sum();
    obj1.display();
    cout<<value<<endl;
    
    return 0;
    

    }

  • + 0 comments

    using namespace std;

    class Rectangle { public: int width; int height; virtual void display() { cout << width << " " << height << endl; } }; class RectangleArea : public Rectangle { public: void read_input() { cin >> width; cin >> height;

    }
    void display()override
    {
     Rectangle::display();
     cout << width * height << endl; 
    }
    

    };

    int main() { RectangleArea rectangle1; rectangle1.read_input(); rectangle1.display(); return 0; }

  • + 0 comments
    class Rectangle {
    protected:
        int width;
        int height;
    
    public:
        void display() {
            cout << width << " " << height << endl;
        }
    };
    
    class RectangleArea : public Rectangle {
    public:
        void read_input() {
            cin >> width >> height;
        }
    
        void display() {
            cout << width * height << endl;
        }
    };