6.11虚基类

来源:互联网 发布:iphone库存软件 编辑:程序博客网 时间:2024/06/07 23:54
#include<iostream>using namespace std;class shape{    public:        virtual void SurfaceArea(){        }        virtual void Volume(){        }};class Cube:public shape{    double length;    public:        Cube(double length):length(length){        }        void SurfaceArea(){            cout<<"表面积是:"<<length*length*6<<endl;        }        void Volume(){            cout<<"体积是:"<<length*length*length<<endl;        }};class Cylinder:public shape{    double radius;    double heigth;    public:        Cylinder(double radius,double heigth):radius(radius),heigth(heigth){        }        void SurfaceArea(){            cout<<"表面积是:"<<(radius*2*3.14)*heigth+radius*radius*3.14*2<<endl;        }        void Volume(){            cout<<"体积是:"<<radius*radius*3.14*heigth<<endl;        }};class Ball:public shape{    double radius;    public:        Ball(double radius):radius(radius){        }        void SurfaceArea(){            cout<<"表面积是:"<<4*3.14*radius*radius<<endl;        }        void Volume(){            cout<<"体积是:"<<4.0/3.0*3.14*radius*radius*radius<<endl;        }};int main(void){    shape *s;    s = new Cube(5);    s->SurfaceArea();    s->Volume();    s = new Cylinder(1,1);    s->SurfaceArea();    s->Volume();    s = new Ball(2);    s->SurfaceArea();    s->Volume();    return 0;}
0 0
原创粉丝点击