#include<iostream>
using namespace std;
class Bace{ //基类
protected:
double h;
public:
Bace(double a)
{
h=a;
}
virtual void disp()
{
cout<<"High="<<h<<endl;
}
};
class Cuboid :public Bace{ //派生类Cuboid
public:
Cuboid(double a,double b,double c):Bace(a)
{
l=b;
w=c;
}
virtual void disp()
{
cout<<"Volume="<<l*w*h<<endl;
}
private:
double l;
double w;
};
class Cylinder:public Bace{ //派生类Cylinder
public:
Cylinder(double a,double d):Bace(a)
{
radius=d;
}
virtual void disp()
{
cout<<"Volume="<<3.14*radius*radius*h<<endl;
}
private:
double radius;
};
int main()
{
Bace aa(30),*pc;
Cuboid bb(3,4,5);
Cylinder cc(10,1);
pc=&aa;
pc->disp();
pc=&bb;
pc->disp();
pc=&cc;
pc->disp();
return 0;
}
6.13
最新推荐文章于 2024-06-16 13:04:38 发布