10-多继承
题目描述
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函数产生输出。
输入
第一行:最高速度 速度 重量 第二行:高度 第三行:座位数
输出
第一行:Vehicle: 第二行及以后各行:格式见Sample
100 60 20
28
2
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>
using namespace std;
class vehicle
{
protected:
int max,speed,weight;
public:
vehicle(int m,int s,int w):max(m),speed(s),weight(w){}
void display()
{
cout<<"Vehicle:"<<endl;
cout<<"max_speed:"<<max<<endl<<"speed:"<<speed<<endl<<"weight:"<<weight<<endl;
}
};
class bicycle:virtual public vehicle
{
protected:
int height;
public:
bicycle(int m,int s,int w,int h):vehicle(m,s,w),height(h){}
void display()
{
cout<<"Bicycle:"<<endl;
cout<<"max_speed:"<<max<<endl<<"speed:"<<speed<<endl<<"weight:"<<weight<<endl<<"height:"<<height<<endl;
}
};
class motocar:virtual public vehicle
{
protected:
int seatnum;
public:
motocar(int m,int s,int w,int seat):vehicle(m,s,w),seatnum(seat){}
void display()
{
cout<<"Motocar:"<<endl;
cout<<"max_speed:"<<max<<endl<<"speed:"<<speed<<endl<<"weight:"<<weight<<endl<<"seat_num:"<<seatnum<<endl;
}
};
class motocycle:public bicycle,public motocar
{
public:
motocycle(int m,int s,int w,int h,int seat):vehicle(m,s,w),bicycle(m,s,w,h),motocar(m,s,w,seat){}
void display()
{
cout<<"Motocycle:"<<endl;
cout<<"max_speed:"<<max<<endl<<"speed:"<<speed<<endl<<"weight:"<<weight<<endl<<"height:"<<height<<endl<<"seat_num:"<<seatnum<<endl;
}
};
int main()
{
int m,s,w,h,seat;
cin>>m>>s>>w>>h>>seat;
vehicle v(m,s,w);
bicycle bic(m,s,w,h);
motocar car(m,s,w,seat);
motocycle moto(m,s,w,h,seat);
v.display();cout<<endl;
bic.display();cout<<endl;
car.display();cout<<endl;
moto.display();
}