Answer to Question #152390 in C++ for Noman Naeem

Question #152390
Create two classes:
BaseClass
The Rectangle class should have two data fields-width and height of int types. The class should have display()method, to print the width and height of the rectangle separated by space.
DerivedClass
The RectangleArea class is derived from Rectangle class, i.e., it is the sub-class of Rectangle class. The class should have read_input() method, to read the values of width and height of the rectangle. The RectangleArea class should also override the display() method to print the area (width*height) of the rectangle.
1
Expert's answer
2020-12-21T12:47:14-0500
#include <iostream>


class Rectangle {
protected:
	int width;
	int height;
public:		
	Rectangle(int width = 0, int height=0) {
		this->width = width;
		this->height = height;
	}
	void display() {
		std::cout << width << " " << height<<std::endl;
	}
};


class RectangleArea : public Rectangle {
public:	
	RectangleArea() {}
	RectangleArea(int width, int height) :Rectangle(width, height) {}
	void read_input() {
		std::cout << "Enter width of rectangle: ";
		std::cin >> this->width;
		std::cout << "Enter height of rectangle: ";
		std::cin >> this->height;
	}
	void display() {
		std::cout << this->width*this->height << std::endl;
	}
};
int main() {
	Rectangle* rectangle = new Rectangle(1,2);
	rectangle->display();
	RectangleArea *rectangleArea = new RectangleArea();
	rectangleArea->read_input();
	rectangleArea->display();
	system("pause");
	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