#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; }
14周 项目3 立体类族共有的抽象类,布布扣,bubuko.com
原文地址:http://blog.csdn.net/zjx211314/article/details/27207165