How to share base class members between inherited derived class in C++

1 Answer

0 votes
#include <iostream>

using std::cout;
using std::endl;

class Area {
public:
	double height;
	double width;
};

class Rectangle : public Area {
public:
	Rectangle(double h, double w) {
		height = h;
		width = w;
	}
	double area() {
		return height * width;
	}
};

class IsoscelesTriangle : public Area {
public:
	IsoscelesTriangle(double h, double w) {
		height = h;
		width = w;
	}
	double area() {
		return 0.5 * width * height;
	}
};

int main()
{
	Rectangle o1(13, 7.0);
	IsoscelesTriangle o2(3, 9);

	cout << o1.area() << endl;
	cout << o2.area() << endl;

	return 0;
}



/*
run:

91
13.5

*/

 



answered Mar 26, 2018 by avibootz

Related questions

...