class shape {
public:
virtual float Printarea()const { return 0; };
};
class circle :public shape {
public:
circle(float r) :radius(r) {}
virtual float Printarea()const;//覆写面积函数
private:
float radius;
};
float circle::Printarea()const {
return (radius * radius * 3.14);
}
class rectangle :public shape {
public:
rectangle(float w,float h) :weight(w),height(h) {}
virtual float Printarea()const;
private:
float weight,height;
};
float rectangle::Printarea()const {
return weight * height;
}
class triangle :public shape {
public:
triangle(float z, float x, float n) :a(z), b(x), c(n) {}
virtual float Printarea()const;
private:
float a, b, c;
float p = (a + b + c) / 2;
};
float triangle::Printarea() const {
return sqrt(p * (p - a) * (p - b) * (p - c));
}
class trapezoid :public shape {
public:
trapezoid(float a,float b, float h):s(a),x(b),height(h){}
virtual float Printarea()const;
private:
float s, x, height;
};
float trapezoid::Printarea()const {
return ((s + x) * height) / 2;
}
class square :public shape {
public:
square(float r):a(r){}
virtual float Printarea()const;
private:
float a;
};
float square::Printarea()const {
return a * a;
}
int main() {
circle c(3);
rectangle r(3, 4);
triangle t(3, 4, 5);
square s(2);
trapezoid tr(4, 5, 8);
shape *p[5] = {&c,&t,&s,&tr,&r};
float totalarea = 0;
for (int i = 0; i < 5; i++) {
totalarea += p[i]->Printarea();
}
cout << totalarea;
}