要求定义一个描述形状的抽象类shape,类内包括求面积的area和求各图形总面积的total函数.

1个回答

  • class Trapezoid : public Shape

    {

    private:

    double top;

    double bottom;

    double height;

    public:

    Trapezoid(double t, double b, double h)

    {

    top = t;

    bottom = b;

    height = h;

    }

    double Area()

    {

    return (top + bottom) * height / 2;

    }

    };

    #define PI 3.1415926

    class Circle : public Shape

    {

    private:

    double r;

    public:

    Circle(double r)

    {

    this->r = r;

    }

    double Area()

    {

    return PI * r * r;

    }

    };

    class Triangle : public Shape

    {

    private:

    double bottom;

    double height;

    public:

    Triangle(double b, double h)

    {

    bottom = b;

    height = h;

    }

    double Area()

    {

    return bottom * height / 2;

    }

    };