Problem statement:
You are given a class Rectangle with the following specifications:
Two private integers width and height representing the dimensions of a rectangle.
A public member function display() that prints the width and height of the rectangle.
You need to create a class RectangleArea that inherits from Rectangle and has the following specifications:
A private integer area to store the area of the rectangle.
A public member function read_input() that reads the values of width and height from input.
A public member function display() that overrides the display() function of the base class and prints the width, height, and area of the rectangle.
Sample Input:
The input consists of two integers separated by a space, representing the width and height of the rectangle.
4 5
Sample Output:
The output should consist of three lines:
The first line should print "Width: " followed by the value of width.
The second line should print "Height: " followed by the value of height.
The third line should print "Area: " followed by the value of area.
Width: 4
Height: 5
Area: 20
Explanation:
In this example, the width of the rectangle is 4 and the height is 5. The area of the rectangle is calculated as width * height, which is 20. The display() function of the RectangleArea class is called, which prints the width, height, and area of the rectangle.
#include <iostream>
using namespace std;
class Rectangle {
protected:
int width;
int height;
public:
Rectangle() {}
Rectangle(int w, int h) : width(w), height(h) {}
void display() {
cout << "Width: " << width << endl;
cout << "Height: " << height << endl;
}
};
class RectangleArea : public Rectangle {
public:
void read_input() {
cin >> width >> height;
}
void display() {
cout << "Area: " << width * height << endl;
}
};
int main() {
RectangleArea r_area;
r_area.read_input();
r_area.Rectangle::display();
r_area.display();
return 0;
}
Code Explanation
The code starts by including the necessary header files, including
The Rectangle class is defined with two private member variables width and height to represent the dimensions of a rectangle.
The Rectangle class has a constructor that initializes the width and height variables, and a display() function that prints the width and height.
The RectangleArea class is defined, which inherits publicly from the Rectangle class.
The RectangleArea class has a read_input() function that reads the values of width and height from the input.
The RectangleArea class also has a display() function that overrides the display() function of the base class. It calculates and prints the area of the rectangle by multiplying width and height.
In the main() function, an instance of RectangleArea is created.
The read_input() function is called to read the input values for width and height.
The display() function of the base class Rectangle is called using the scope resolution operator :: to print the width and height values.
The display() function of the derived class RectangleArea is called to print the area of the rectangle.
The program ends by returning 0 from the main() function.