shape

#include<iostream>
#define pi 3.14
using namespace std;
class cstereoshape
{
public:
    virtual double getarea() = 0;
    virtual double getvolume() = 0;
};
class ccube : public cstereoshape
{
public:
    ccube(double l = 0,double w = 0,double h = 0)
    {
        length = l;
        wide = w;
        high = h;
    }
    ~ccube(){}
    void set(double l,double w,double h)
    {
        length = l;
        wide = w;
        high = h;
    }
    double getarea()
    {
        return (length * wide + length * high + high * wide ) * 2;
    }
    double getvolume()
    {
        return (length * wide * high);
    }
private:
    double length,wide,high;
};
class csphere:public cstereoshape
{
public:
    csphere(double rr = 0)
    {
        r = rr;
    }
    ~csphere(){}
    void setr( double rr)
    {
        r = rr;
    }
    double getarea()
    {
        return 4 * pi * r * r;
    }
    double getvolume()
    {
        return 4 * pi * r * r * r / 3.0;
    }
private:
    double r;
};
void main()
{
    ccube a_cube(1,1,1);
    csphere c_sphere(1);
    cstereoshape * p;
    a_cube.set(4,5,6);
    p = & a_cube;
    cout << "area: " << p->getarea() << " volume: " << p->getvolume() << endl;
    c_sphere.setr(7);
    p = & c_sphere;
    cout << "area: " << p->getarea() << " volume: " << p->getvolume() << endl;
}

 

posted on 2013-12-26 17:00  了发发  阅读(163)  评论(0)    收藏  举报

导航