下列shape类是一个表示形状的抽象类,area( )为求图形面积的函数,total( )则是一个通用的用以求不同形状的图形面积总和的函数。请从shape类派生三角形类(triangle)、矩形类(rectangle),并给出具体的求面积函数
class shape{ //形状基类
public:
virtual float area( )=0;
};
float total(shape *s[ ],int n)
{
float sum=0.0;
for(int i=0;iarea( );
return sum;
}
class Triangle:public Shape //三角形
{
public:
Triangle(double h,double w){H=h;W=w;}
double Area() const{return H*W*0.5;}
private:
double H,W;
};
class Rectangle:public Shape //矩形
{
public:
Rectangle(double h,double w){H=h;W=w;}
double Area()const{return H*W;}
private:
double H,W;
};