下面Shape类是一个表示形状的抽象类,Area ( )为求图形面积的函数.请从Shape类派生梯形类(Trapezoi

问题描述:

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

问题解答:

我来补答
class Trapezoid :public Shape
{
private:
\x05double top;
\x05double bottom;
\x05double height;
public:
\x05Trapezoid(double t,double b,double h)
\x05{
\x05\x05top = t;
\x05\x05bottom = b;
\x05\x05height = h;
\x05}
\x05double Area()
\x05{
\x05\x05return (top + bottom) * height / 2;
\x05}
};
#define PI 3.1415926
class Circle :public Shape
{
private:
\x05double r;
public:
\x05Circle(double r)
\x05{
\x05\x05this->r = r;
\x05}
\x05double Area()
\x05{
\x05\x05return PI * r * r;
\x05}
};
class Triangle :public Shape
{
private:
\x05double bottom;
\x05double height;
public:
\x05Triangle(double b,double h)
\x05{
\x05\x05bottom = b;
\x05\x05height = h;
\x05}
\x05double Area()
\x05{
\x05\x05return bottom * height / 2;
\x05}
};
 
 
展开全文阅读
剩余:2000
上一页:一道物理提题