一、题目描述
1、建立如下的类继承结构:
1)一个车类CVehicle作为基类,具有max_speed、speed、weight等数据成员,display()等成员函数
2)从CVehicle类派生出自行车类CBicycle,添加属性:高度height
3)从CVehicle类派生出汽车类CMotocar,添加属性:座位数seat_num
4)从CBicycle和CMotocar派生出摩托车类CMotocycle
2、分别定义以上类的构造函数、输出函数display及其他函数(如需要)。
3、在主函数中定义各种类的对象,并测试之,通过对象调用display函数产生输出。
二、输入与输出
1.输入
第一行:最高速度 速度 重量 第二行:高度 第三行:座位数
100 60 20
28
2
2.输出
第一行:Vehicle: 第二行及以后各行:格式见Sample
Vehicle:
max_speed:100
speed:60
weight:20
Bicycle:
max_speed:100
speed:60
weight:20
height:28
Motocar:
max_speed:100
speed:60
weight:20
seat_num:2
Motocycle:
max_speed:100
speed:60
weight:20
height:28
seat_num:2
三、参考代码
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;
class ve
{
int max;
int spe;
int wei;
public:
ve(int m,int s,int w):max(m),spe(s),wei(w){}
void pri1()
{
cout << "max_speed:" << max << endl;
cout << "speed:" << spe << endl;
cout << "weight:" << wei << endl;
}
};
class bic :public ve
{
int hei;
public:
bic(int m, int s, int w,int h):ve(m,s,w),hei(h){}
void pri2()
{
pri1();
cout << "height:" << hei << endl;
}
};
class mo :public ve
{
int set;
public:
mo(int m, int s, int w, int h) :ve(m, s, w), set(h) {}
void pri3()
{
cout << "seat_num:" << set<<endl;
}
};
class moto :public bic, public mo
{
public:
moto(int m, int s, int w, int h, int s1) :bic(m, s, w, h), mo(m, s, w, s1) {}
void pri4()
{
pri2();
pri3();
}
};
int main()
{
int max;
int spe;
int wei;
int h;
int s;
cin >> max >> spe >> wei >> h >> s;
cout << "Vehicle:" << endl;
ve a1(max, spe, wei);
a1.pri1();
cout << endl;
cout << "Bicycle:" << endl;
bic a2(max, spe, wei, h);
a2.pri2();
cout << endl;
cout << "Motocar:" << endl;
mo a3(max, spe, wei, s);
a1.pri1();
a3.pri3();
cout << endl;
cout << "Motocycle:" << endl;
moto a4 (max,spe, wei, h, s);
a4.pri4();
}