6.13

来源:互联网 发布:mac口红哪款滋润度好 编辑:程序博客网 时间:2024/05/17 15:03
#include<iostream>using namespace std;const double pi=3.14;class Base{protected:double h;public:Base(double high){h=high;};virtual double disp()=0;};class Cuboid:public Base{protected:double l;double w;public:Cuboid(double high,double length,double width):Base(high){l=length;w=width;}double disp(){double i;i=h*l*w;return i;}void show(){cout<<"高为"<<h<<"长为"<<l<<"宽为"<<w<<"的长方体体积为:";}};class Cylinder:public Base{protected:double r;public:Cylinder(double high,double r1):Base(high){r=r1;}double disp(){double j;j=pi*r*r*h;return j;}void show(){cout<<"高为"<<h<<"半径为"<<r<<"的圆柱体体积为:";}};int main(){Base *p;Cuboid c(5.3,4.5,7.3);Cylinder y(4.2,3.7);p=&c;c.show();cout<<p->disp()<<endl;p=&y;y.show();cout<<p->disp()<<endl;system("pause");return 0;}

0 0
原创粉丝点击