Answer to Question #151125 in Software Engineering for Chathura

Question #151125
Define the object and classes with reference to object oriented concepts
1
Expert's answer
2020-12-15T19:11:32-0500
#include <iostream>
using namespace std;

//define class
    class Rectangle {

    private:
        int width;
        int height;

    public:
        Rectangle();
        Rectangle(int, int);

    public:
         int getArea() const {
            return  width * height ;
        }
        int getWidth() const {
            return width;
        }
        int getHeight() const  {
            return height;
        }
    };
//default constructor
    Rectangle::Rectangle() {
        width = 10;
        height = 5;
    }
//constructor 1
    Rectangle::Rectangle(int a, int b) {
        width = a;
        height = b;
    }





int main() {

// Create an object Rectangle 'rect1' from constructor 1.
// Value 5 is assigned to the width, the value 4 is assigned to the height.
    Rectangle rect1(5, 4);

// Create an object Rectangle 'rect2' from default constructor .
// width, height are assigned default values
    Rectangle rect2;

    cout << "rect1:" << endl;
// Call method to get with
    cout << " width: " << rect1.getWidth() << endl;
// Call method to get heght
    cout << " height: " << rect1.getHeight() << endl;

// Call the method to calculate the area.
    cout << " area: " << rect1.getArea() << endl;

    cout << "rect2:" << endl;
    cout << " width: " << rect2.getWidth() << endl;
    cout << " height: " << rect2.getHeight() << endl;
    cout << " area: " << rect2.getArea() << endl;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS