Answer to Question #185655 in C++ for VASANTH V

Question #185655

Define a class Shape and include length,width attribute as protected and include member functions setwidth(int w) and setHeight(int h).Derive a class Rectangle and include calculate_Area() and display area of rectangle. Create an object Rect to store value of length and breadth from user and derive objects Area .


1
Expert's answer
2021-04-26T07:47:57-0400
#include <iostream>

class Shape {
public:
    void SetWidth(int w) {
        width = w;
    }
    void SetHeight(int h) {
        length = h;
    }
protected:
    int length;
    int width;
};

class Rectangle : public Shape {
public:
    int CalculateArea() const {
        return length * width;
    }
};

int main() {
    int w, h;
    std::cout << "Enter width: ";
    std::cin >> w;
    std::cout << "Enter height: ";
    std::cin >> h;
    Rectangle rect;
    rect.SetWidth(w);
    rect.SetHeight(h);
    std::cout << "Area : " << rect.CalculateArea() << '\n';
    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