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

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

爱神小甜甜 幼苗

共回答了22个问题采纳率:95.5% 举报

class Trapezoid :public Shape
{
private:
x05double top;
x05double bottom;
x05double height;
public:
x05Trapezoid(double t,double b,double h)
x05{
x05x05top = t;
x05x05bottom = b;
x05x05height = h;
x05}
x05double Area()
x05{
x05x05return (top + bottom) * height / 2;
x05}
};
#define PI 3.1415926
class Circle :public Shape
{
private:
x05double r;
public:
x05Circle(double r)
x05{
x05x05this->r = r;
x05}
x05double Area()
x05{
x05x05return PI * r * r;
x05}
};
class Triangle :public Shape
{
private:
x05double bottom;
x05double height;
public:
x05Triangle(double b,double h)
x05{
x05x05bottom = b;
x05x05height = h;
x05}
x05double Area()
x05{
x05x05return bottom * height / 2;
x05}
};

1年前

8
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 16 q. 0.045 s. - webmaster@yulucn.com