作者 刘利
单位 惠州学院
根据下图关系,定义4个类:Vehicle、Bicycle、Motorcar、Motorcycle
- Vehicle类:包含数据成员:最大车速(MaxSpeed)和车重(Weight)
- Bicycle继承Vehicle,新增数据成员:车高(Height)
- Motorcar继承Vehicle,新增数据成员:座位数(SeatNum)
- Motorcycle继承Bicycle和Motorcar,无新增成员。
根据需要定义每个类的成员函数,在main函数中完成如下功能:
分别读入Vehicle、Bicycle、Motorcar、Motorcycle的信息,并定义其对象。
输出如下内容:
list:
1.Vehicle
2.Bicycle
3.Motorcar
4.Motorcycle
Please input your choose:
读入用户的选择,根据选择调用对应对象的成员函数输出内容。
具体见输入输出样例。
备注:你的运行界面可能如下:
图中以下内容是实例化对象时调用构造函数输出的。
vehicle(100,200)
vehicle(20,10)bicycle(1)
vehicle(150,250)motorcar(6)
vehicle(100,150)bicycle(1)motorcar(2)motorcycle
语法要求:
多继承、虚基类、同名成员
输入格式:
共5行
第1行:MaxSpeed,Weight;
第2行:MaxSpeed,Weight,Height;
第3行:MaxSpeed,Weight,SeatNum;
第4行:MaxSpeed,Weight,Height,SeatNum;
第5行:1~4的整数(表示用户的选择)
输出格式:
调用各构造函数输出内容
根据选择调用对象成员函数输出内容
输入样例1:
100 200
20 10 1
150 250 6
100 150 1 2
1
输出样例1:
vehicle(100,200)
vehicle(20,10)bicycle(1)
vehicle(150,250)motorcar(6)
vehicle(100,150)bicycle(1)motorcar(2)motorcycle
list:
1.vehicle
2.bicycle
3.motorcar
4.motorcycle
please input your choose:
a vehicle is running!
maxspeed:100公里/时.
weight:200千克.
输入样例2:
100 200
20 10 1
150 250 6
100 150 1 2
2
输出样例2:
vehicle(100,200)
vehicle(20,10)bicycle(1)
vehicle(150,250)motorcar(6)
vehicle(100,150)bicycle(1)motorcar(2)motorcycle
list:
1.vehicle
2.bicycle
3.motorcar
4.motorcycle
please input your choose:
a bicycle is running!
maxspeed:20公里/时.
weight:10千克.
height:1米.
输入样例3:
100 200
20 10 1
150 250 6
100 150 1 2
3
输出样例3:
vehicle(100,200)
vehicle(20,10)bicycle(1)
vehicle(150,250)motorcar(6)
vehicle(100,150)bicycle(1)motorcar(2)motorcycle
list:
1.vehicle
2.bicycle
3.motorcar
4.motorcycle
please input your choose:
a motorcar is running!
maxspeed:150公里/时.
weight:250千克.
seatnum:6人.
输入样例4:
100 200
20 10 1
150 250 6
100 150 1 2
4
输出样例4:
vehicle(100,200)
vehicle(20,10)bicycle(1)
vehicle(150,250)motorcar(6)
vehicle(100,150)bicycle(1)motorcar(2)motorcycle
list:
1.vehicle
2.bicycle
3.motorcar
4.motorcycle
please input your choose:
a motorcycle is running!
maxspeed:100公里/时.
weight:150千克.
height:1米.
seatnum:2人.
--------------------------------------------------------------------------------------------------------------------------------
答案
#include<iostream>
using namespace std;
class Vehicle
{
public:
int MaxSpeed, Weight;
Vehicle(int m, int w) : MaxSpeed(m), Weight(w){}
void show01()
{
cout << "vehicle(" << MaxSpeed << "," << Weight << ")" << endl;
}
void show02()
{
cout << "maxspeed:" << MaxSpeed << "公里/时." << endl;
cout << "weight:" << Weight << "千克." << endl;
}
};
class Bicycle : virtual public Vehicle
{
public:
int Height;
Bicycle(int m,int w,int h): Vehicle::Vehicle(m,w), Height(h) {}
void show01()
{
cout << "vehicle(" << MaxSpeed << "," << Weight << ")" << "bicycle(" << Height << ")" << endl;
}
void show02()
{
Vehicle::show02();
cout << "height:" << Height << "米." << endl;
}
};
class Motorcar :virtual public Vehicle
{
public:
int SeatNum;
Motorcar(int m, int w, int s) : Vehicle::Vehicle(m, w), SeatNum(s) {}
void show01()
{
cout << "vehicle(" << MaxSpeed << "," << Weight << ")" << "motorcar(" << SeatNum << ")" << endl;
}
void show02()
{
Vehicle::show02();
cout << "seatnum:" << SeatNum << "人." << endl;
}
};
class Motorcycle :public Motorcar, public Bicycle
{
public:
Motorcycle(int m, int w, int h,int s) : Vehicle::Vehicle(m, w), Bicycle(m, w, h), Motorcar(m, w, s) {}
void show01()
{
cout << "vehicle(" << MaxSpeed << "," << Weight << ")" << "bicycle(" << Height << ")" << "motorcar(" << SeatNum << ")" << "motorcycle" << endl;
}
void show02()
{
Vehicle::show02();
cout << "height:" << Height << "米." << endl;
cout << "seatnum:" << SeatNum << "人." << endl;
}
};
void test()
{
cout << "list:" << endl;
cout << "1.vehicle" << endl;
cout << "2.bicycle" << endl;
cout << "3.motorcar" << endl;
cout << "4.motorcycle" << endl;
cout << "please input your choose:" << endl;
}
int main()
{
int m, w, s, h;
cin >> m >> w;
Vehicle v(m,w);
v.show01();
cin >> m >> w >> h;
Bicycle b(m, w, h);
b.show01();
cin >> m >> w >> s;
Motorcar g(m, w, s);
g.show01();
cin >> m >> w >> h >> s;
Motorcycle t(m, w, h, s);
t.show01();
test();
int n;
cin >> n;
switch (n)
{
case 1:
{
cout << "a vehicle is running!" << endl;
v.show02();
break;
}
case 2:
{
cout << "a bicycle is running!" << endl;
b.show02();
break;
}
case 3:
{
cout << "a motorcar is running!" << endl;
g.show02();
break;
}
case 4:
{
cout << "a motorcycle is running!" << endl;
t.show02();
break;
}
}
return 0;
}
代码有点丑陋