7-1 车辆类(虚基类)

本文介绍了如何使用C++创建Vehicle、Bicycle、Motorcar和Motorcycle四个类,它们通过多继承和虚基类设计,每个类具有特定的数据成员和成员函数。主要展示了如何实例化这些类,以及根据用户选择调用相应对象的输出功能。
摘要由CSDN通过智能技术生成

作者 刘利

单位 惠州学院

根据下图关系,定义4个类:Vehicle、Bicycle、Motorcar、Motorcycle

0013.png

  1. Vehicle类:包含数据成员:最大车速(MaxSpeed)和车重(Weight)
  2. Bicycle继承Vehicle,新增数据成员:车高(Height)
  3. Motorcar继承Vehicle,新增数据成员:座位数(SeatNum)
  4. Motorcycle继承Bicycle和Motorcar,无新增成员。

根据需要定义每个类的成员函数,在main函数中完成如下功能:
分别读入Vehicle、Bicycle、Motorcar、Motorcycle的信息,并定义其对象。
输出如下内容:

list:
1.Vehicle
2.Bicycle
3.Motorcar
4.Motorcycle
Please input your choose:

读入用户的选择,根据选择调用对应对象的成员函数输出内容。
具体见输入输出样例。

备注:你的运行界面可能如下:

image.png


图中以下内容是实例化对象时调用构造函数输出的。
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;
}

 代码有点丑陋

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值