6-2 车的继承 (10分) 定义一个车(vehicle)基类,具有Speed、Weight等成员变量,Run、Stop等成员函数,由此派生出自行车(bicycle)类,汽车(motorcar)类。自

6-2 车的继承 (10分)
定义一个车(vehicle)基类,具有Speed、Weight等成员变量,Run、Stop等成员函数,由此派生出自行车(bicycle)类,汽车(motorcar)类。自行车(bicycle)类有高度(Height)等属性,汽车(motorcycle)类有座位数(SeatNum)等属性。从bicycle和motorcycle派生出摩托车(Motorcar)类。完成这些类,使得测试代码可以运行并得到正确的输出结果。

函数接口定义:
class Motorcar:public Bicycle,public Motorcycle
Motorcar 必须按照这种方式继承。

裁判测试程序样例:

/* 请在这里填写答案 */

int main(){
Motorcar moto(30,50,120,5);
cout<<"speed "<<moto.speed<<endl;
cout<<"weight "<<moto.weight<<endl;
cout<<"height "<<moto.height<<endl;
cout<<"seatNum "<<moto.seatNum<<endl;
}
输入样例:

输出样例:
在这里给出相应的输出。例如:

speed 30
weight 50
height 120
seatNum 5

我的代码:

#include<iostream>
using namespace std;

class vehicle
{
public:
	void Run(){cout<<"车开始运行"<<endl;}
	void Stop(){cout<<"车停止运行"<<endl;}
public:
	int speed;
	int weight;
};

class Bicycle:virtual public vehicle
{
public:
	int height;
};

class Motorcycle:virtual public vehicle
{
public:
	int seatNum;
};

class Motorcar:public Bicycle,public Motorcycle
{
public:
	Motorcar(int m,int w,int h,int s)
	{
		speed=m,weight=w,height=h,seatNum=s;
	}

};
```cpp


猜你喜欢

转载自blog.csdn.net/qq_35207086/article/details/111460883