Untitled Document

四、（8分）下列shape类是一个表示形状的抽象类，area( )为求图形面积的函数，total( )则是一个通用的用以求不同形状的图形面积总和的函数。请从shape类派生梯形类（trapezoid）、圆形类（circle），并给出具体的求面积函数

class shape{
public:
virtual float area( )=0
};
float total(shape *s[ ],int n)
{
float sum=0.0;
for(int i=0;i<n;i++)
sum+=s[i]->area( );
return sum;
}
class Circle:public Shape
{
public:
Circle(double r){radius=r;}
double Area() const{return 3.1416*radius*radius;}
private:
double radius;
};

class Trapezoid:public Shape
{
public:
Trapezoid(double top,double bottom,double high)
{T=top;B=bottom;H=high;}
double Area() const{return (T+B)*H*0.5;}
private:
double T,B,H;
};