南邮c++实验二 车类继承问题

c++车类继承问题 代码


#include<iostream>
#include<string>
using namespace std;
class Vehicle
{
protected:
	int MaxSpeed;
	int Weight;
public:
	Vehicle(int m, int w)
	{
		MaxSpeed = m;
		Weight = w;
		cout << "Constructing Vehicle...\n";
	}
	~Vehicle()
	{
		cout << "Destructing Vehicle...\n";
	}
	void Run()
	{
		cout << "The vehicle is running!\n";
	}
	void Stop()
	{
		cout << "Please stop running!\n";
	}
	void Show()
	{
		cout << "It\'s maxspeed is: " << MaxSpeed << endl;
		cout << "It\'s weight is: " << Weight << endl;
	}
};
class Bicycle :virtual public Vehicle
{
protected:
	int Height;
public:
	Bicycle(int m, int w, int h) :Vehicle(m, w)
	{
		Height = h;
		cout << "Constructing Bicycle...\n";
	}
	~Bicycle()
	{
		cout << " Destructing Bicycle...\n";
	}
	void Show()
	{
		Vehicle::Show();
		cout << "The Height is: " << Height << endl;
	}
};
class Car :virtual public Vehicle
{
protected:
	int SeatNum;
public:
	Car(int m, int w, int s) :Vehicle(m, w)
	{
		SeatNum = s;
		cout << "Constructing Car...\n";
	}
	~Car()
	{
		cout << " Destructing Car...\n";
	}
	void Show()
	{
		Vehicle::Show();
		cout << "The SeatNum of the car is: " << SeatNum << endl;
	}
};
class MotorCycle :public Bicycle, public Car
{
public:
	MotorCycle(int m, int w, int h, int s) :Bicycle(m, w, h), Car(m, w, s), Vehicle(m, w)
	{
		cout << "Constructing MotorCycle...\n";
	}
	~MotorCycle()
	{
		cout << "Destructing MotorCycle...\n";
	}
	void Show()
	{
		cout << "It\'s maxspeed is:" << Car::MaxSpeed << endl;
		cout << "It\'s weight is: " << Car::Weight << endl;
		cout << "It\'s height is: " << Height << endl;
		cout << " It\'s seatnum is : " << SeatNum << endl;
	}
};
int main()
{
	MotorCycle mc(1, 2, 3, 4);
	mc.Run();
	mc.Stop();
	mc.Show();
	return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值