#include<iostream>using namespace std;class Shape{public:virtual double printArea()const{return 0.0;}virtual double printVol()const{return 0.0;}};class Circle:public Shape{public:Circle(double r):rad(r){}double printArea(){return 3.14*rad*rad;}private:double rad;};class Rect:public Shape{public:Rect(double l,double w):len(l),wid(w){}double printArea(){return len*wid;}private:double len,wid;};class Tri:public Shape{public:Tri(double l,double h):len(l),hei(h){}double printArea(){return 0.5*len*hei;}private:double len,hei;};class Cy:public Circle{public:Cy(double r,double h):rad(r),hei(h){}double printArea(){return (2*3.14*rad*rad+2*3.14*rad*hei);}double printVol(){return 3.14*rad*rad*hei;}private:double rad,hei;};class Rec:public Rect{public:Rec(double l,double k,double h):len(l),kuan(k),hei(h){}double printArea(){return (2*len*kuan+2*len*hei+2*hei*kuan);}double printVol(){return len*kuan*hei;}private:double len,kuan,hei;};int main(){Circle a(10);Rect b(10,20);Tri c(10,20);Cy d(10,20);Rec e(10,10,10);Shape * p;p=&a;cout<<p->printArea()<<endl;p=&b;cout<<p->printArea()<<endl;p=&c;cout<<p->printArea()<<endl;p=&d;cout<<p->printArea()<<endl;cout<<p->printVol()<<endl;p=&e;cout<<p->printArea()<<endl;cout<<p->printVol()<<endl;return 0;}
添加回答
举报
0/150
提交
取消