c++程序题,老大们帮帮忙啊,

来源:百度知道 编辑:UC知道 时间:2024/06/22 14:54:59
1、定义抽象基类High,其数据成员为高H,定义Show()为纯虚函数,然后再由High派生出长方体类Cuboid与圆柱体类Cylinder,并在两个派生类中重新定义虚函数Show()。在主函数中,用基类High定义指针变量p,然后用指针p动态调用派生类中的虚函数Show(),显示长方体与圆柱体的体积。
2、定义描述圆的类Circle,其数据成员为圆的半径R,成员函数为计算圆面积的函数Area()与构造函数。再由圆类派生出圆柱体类Cylinder,其数据成员为圆柱体的高H,成员函数为:构造函数、计算体积的函数Vol()、输出体积的函数Show()。主函数中用圆柱体类定义圆柱体对象cy,并赋初值(10,20),最后显示圆柱体的体积。

最好今天晚上前给我 。

#include <iostream>
using namespace std;

class High
{
public:
int h;
void virtual Show()=0;
};

class Cuboid:public High
{
public:
Cuboid(int l,int w);
void virtual Show();
private:
int length; //长
int width; //宽
};

Cuboid::Cuboid(int l,int w)
{
length=l;
width=w;
}
void Cuboid::Show()
{
printf("长方体的体积是:%d\n",h*length*width);
}

class Cylinder:public High
{
public:
Cylinder(int r);
void virtual Show();
private:
int radius; //半径
const double Pi;
};

Cylinder::Cylinder(int r):Pi(3.1415)
{
radius=r;
}

void Cylinder::Show()
{
printf("圆柱体的体积是:%f\n",Pi*radius*radius*h);
}

int main()
{
High *MyHigh;
MyHigh=new Cuboid(2,3);
MyHigh->h =3;
MyHigh->Show();