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 .
#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;
}
Comments
Leave a comment