发布网友 发布时间:2024-09-26 21:52
共1个回答
热心网友 时间:2分钟前
#include<iostream>
using namespace std;
class High
{
public:
High(float height):h(height){}
virtual float disp();
protected:
float h;
};
class Cuboid:public High
{
public:
Cuboid(float height,float l,float w):High(height),len(l),width(w){}
virtual float disp();
private:
float len;
float width;
};
class Cylinder:public High
{
public:
Cylinder(float height,float rr):High(height),r(rr){}
virtual float disp();
private:
float r;
};
float High::disp()
{
return h;
}
float Cuboid::disp()
{
return h*len*width;
}
float Cylinder::disp()
{
return 3.14159*r*r*h;
}
int main()
{
Cuboid cub(5,10,10);
Cylinder cy(10,10);
High *p;
p=&cub;
cout<<"长方体的体积:"<<p->disp()<<endl;
p=&cy;
cout<<"圆柱体的体积:"<<p->disp()<<endl;
return 0;
}