#include <iostream.h>
class shape {
public: virtual float area()=0;
float total( shape *s[], int n)
{ float sum =0;
for(int i= 0; i< n; i++)
sum+= s[i] -> area( );
return sum;
}
};
class tri: public shape {
protected: float H, W;
public:
tri(float h, float w) {H = h; W = w;}
float area( )
{return H*W*0.5; }
};
class rec: public tri
{
public:
rec(float h, float w):tri(h, w) { }
float area( ) { return H * W; }
};
main( ) {
shape *s[2];
s[0]=&tri(3.0, 4.0);
s[1]=&rec(2.0, 4.0);
float sum = s[0]->total(s,2); //这里
}