c++编程题1. 求几何体:长方体和圆柱体的体积。具体要求如下:(1) 设计一个立体图形类(CStereoShape类)

1个回答

  • #include

    #define PI 3.141593

    using namespace std;

    class CStereoShape

    {

    public:

    virtual double GetArea()=0;

    virtual double GetVolumn()=0;

    };

    class CCube :public CStereoShape

    {

    private:

    double length;

    double width;

    double height;

    public:

    CCube(double l=0,double w=0,double h=0)

    {

    width=w;

    height=h;

    length=l;

    }

    double GetArea()

    {

    return 2*(width*height+width*length+length*height);

    }

    double GetVolumn()

    {

    return width*length*height;

    }

    void put(double l,double w,double h)

    {

    width=w;

    height=h;

    length=l;

    }

    };

    class CSphere:public CStereoShape

    {

    private:

    double radius;

    public:

    CSphere(double r=0)

    {

    radius=r;

    }

    double GetArea()

    {

    return 4*PI*radius*radius;

    }

    double GetVolumn()

    {

    return GetArea()*radius/3;

    }

    void put(double r)

    {

    radius=r;

    }

    };

    intmain()

    {

    CCube a_cube;

    CSphere c_sphere;

    a_cube.put(4,5,6);

    c_sphere.put(7);

    CStereoShape *p;

    p=&a_cube;

    cout< cout<

    GetArea()

    p=&c_sphere;

    cout< cout<

    GetArea()

    return 0;

    }