码迷,mamicode.com
首页 > 其他好文 > 详细

14周 项目3 立体类族共有的抽象类

时间:2014-06-04 23:22:58      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:c   class   blog   code   a   http   

#include <iostream>

using namespace std;

class CSolid
{
public:
    virtual double area() const=0;
    virtual double volume() const=0;
};
class CCube:public CSolid
{
public:
    CCube(double s):side(s) {}
    virtual double area()const
    {
        return 6*side*side;
    }
    virtual double volume()const
    {
        return side*side*side;
    }
protected:
    double side;
};
class CBall:public CSolid
{
public:
    CBall(double r):R(r) {}
    virtual double area()const
    {
        return 4*3.14*R*R;
    }
    virtual double volume()const
    {
        return 4/3*3.14*R*R*R;
    }
protected:
    double R;
};
class CCylinder:public CSolid
{
public:
    CCylinder(double r,double h):R(r),height(h) {}
    virtual double area()const
    {
        return 3.14*R*R*2+2*3.14*R*height;
    }
    virtual double volume()const
    {
        return 3.14*R*R*height;
    }
protected:
    double R;
    double height;
};
int main()
{
    CCube c(4);
    CBall b(2);
    CCylinder cy(2,4);
    CSolid *p[3]= {&c,&b,&cy};
    cout<<"正方体的体积和表面积"<<endl;
    cout<<"表面积"<<p[0]->area()<<endl;
    cout<<"体积"<<p[0]-> volume()<<endl;
    cout<<"球体的体积和表面积"<<endl;
    cout<<"表面积"<<p[1]->area()<<endl;
    cout<<"体积"<<p[1]-> volume()<<endl;
    cout<<"圆柱体的体积和表面积"<<endl;
    cout<<"表面积"<<p[2]->area()<<endl;
    cout<<"体积"<<p[2]-> volume()<<endl;
    cout << "" << endl;
    return 0;
}

bubuko.com,布布扣

14周 项目3 立体类族共有的抽象类,布布扣,bubuko.com

14周 项目3 立体类族共有的抽象类

标签:c   class   blog   code   a   http   

原文地址:http://blog.csdn.net/zjx211314/article/details/27207165

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!