问题 问答题

下面Shape类是一个表示形状的抽象类,Area()为求图形面积的函数。请从Shape类派生梯形类(Trapezoid)、圆形类(Circle),三角形类(Triangle),并给出具体的求面积函数。其中,所有派生类计算面积需要用到的参数由构造函数给出,梯形面积计算需要上底、下底和高,三角形面积需要底和高,圆形面积需要半径。
形状的抽象类声明如下:
class Shape
public:
virtual double Area()=0;

答案

参考答案:class Circle:public Shape
{
public:
Cirele(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;
};
class Triangle:public Shape
{
public:
Triangle(double bottom,double height:
{
B=bottom;
H=height;
}
double Area()const
{
return B*H*0.5;
}
private:
double B,H;
};

单项选择题
单项选择题