#include<iostream>
using namespace std;
class Base
{
double h;
public:
Base(double a)
{h=a;}
virtual void disp()=0;
double Getterh()
{return h;}
};
class Cuboid:public Base
{
double chuang;
double kuang;
public:
Cuboid(double a,double b,double c):Base(a)
{chuang=b;
kuang=c;
}
void disp()
{
cout<<"高为:"<<Getterh()<<" "<<"长为"<<chuang<<" "<<"宽为"<<kuang<<" "<<"的长方体体积:"<<chuang*kuang*Getterh()<<endl;
}
};
class Cylinder:public Base
{
double r;
public:
Cylinder(double a,double b):Base(a)
{r=b;}
void disp()
{
cout<<"底面半径为"<<r<<" "<<"高为"<<Getterh()<<" "<<"的圆柱体体积为:"<<3.14*r*r*Getterh()<<endl;
}
};
int main()
{
Base* p;
Cuboid a(10.0,5.0,6.0);
Cylinder b(5.0,1.0);
p=&a;
p->disp();
p=&b;
p->disp();
return 0;
}
<img src="https://img-blog.csdn.net/20150502200710075?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHVhbmdjaG9uZ3dlbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" align="bottom" alt="" />