#include <iostream>
using std::cout;
using std::endl;
class Shape {
double w;
double h;
public:
Shape() {
w = h = 0.0;
}
Shape(double _w, double _h) {
w = _w;
h = _h;
}
void show() {
cout << "w = " << w << " h = " << h << endl;
}
double getw() {
return w;
}
double geth() {
return h;
}
void setw(double _w) {
w = _w;
}
void seth(double _h) {
h = _h;
}
virtual double area() {
cout << "Error: area() is virtual not overridden ";
return 0.0;
}
};
class Triangle : public Shape {
public:
Triangle(double w, double h) : Shape(w, h) { }
double area() {
return getw() * geth() / 2;
}
};
class Rectangle : public Shape {
public:
Rectangle(double w, double h) : Shape(w, h) { }
double area() {
return getw() * geth();
}
};
int main() {
Shape *shapes[3];
shapes[0] = &Triangle(5.0, 9.0);
shapes[1] = &Rectangle(8.0, 3.0);
shapes[2] = &Shape(2.0, 7.0);
for (int i = 0; i < 3; i++)
cout << "Area = " << shapes[i]->area() << endl;
return 0;
}
/*
run:
Area = 22.5
Area = 24
Error: area() is virtual not overridden Area = 0
*/