Vehicle类

Vehicle.h

#include<iostream>
using namespace std;
#include<string>

class Vehicle{
private:
	float maxSpeed;
	float weight;
public:
	void run();
	void stop();
	float getms();
	float getw();
	Vehicle(float,float);
};

void Vehicle::run(){
	cout<<"开喽!";
}

void Vehicle::stop(){
	cout<<"停了~";
}

Vehicle::Vehicle(float a,float b){
	maxSpeed=a;
	weight=b;
}

float Vehicle::getms(){
	return maxSpeed;
}

float Vehicle::getw(){
	return weight;
}


other.h

#include"Vehicle.h"

/*--------------------------------Bicycle-------------------------------------------*/
class Bicycle: virtual public Vehicle{
private:
	float heigh;	
public:	
	Bicycle(float,float,float);
	float geth();
	void print();
};

Bicycle::Bicycle(float a,float b,float c):Vehicle(a,b){
	heigh=c;
}

void Bicycle::print(){
	cout<<"该自行车最大速度:"<<getms()<<"  重量:"<<getw()<<"  高度:"<<heigh<<endl;
}

float Bicycle::geth(){
	return heigh;
}

/*--------------------------------Motorcar---------------------------------------*/
class Motorcar: virtual public Vehicle{
private:
	int seatNum;
public:
	Motorcar(float,float,int);
	int getseat();
	void print();
};

Motorcar::Motorcar(float a,float b,int c):Vehicle(a,b){
	seatNum=c;
}

int Motorcar::getseat(){
	return seatNum;
}

void Motorcar::print(){
	cout<<"该小汽车最大速度:"<<getms()<<"  重量:"<<getw()<<"  座位:"<<seatNum<<endl;
}

/*------------------------------Motorcycle---------------------------------------*/
class Motorcycle:public Motorcar,public Bicycle{
public:
	Motorcycle(float,float,float,int);
	void print();
};

Motorcycle::Motorcycle(float m,float n,float c,int d):Vehicle(m,n),Motorcar(m,n,d),Bicycle(m,n,c){}

void Motorcycle::print(){
	cout<<"------------------该摩托车信息-------------------\n";
	cout<<"最高速:"<<getms();
	cout<<"\n重 量:"<<getw();
	cout<<"\n座位数:"<<getseat();
	cout<<"\n高 度:"<<geth()<<endl;
}


main.cpp

#include"other.h"

int main(){
	float maxspeed,weight,heigh;
	int seatnum;
	cout<<"创建一个车对象:\n";
	cout<<"请输入最快速度:";
	cin>>maxspeed;
	cout<<"请输入重量:";
	cin>>weight;
	Vehicle vehicle(maxspeed,weight);
	cout<<"=========车辆信息=========\n";
	cout<<"最高速度:"<<vehicle.getms()<<"重量:"<<vehicle.getw()<<endl;
	vehicle.run();
	vehicle.stop();
	cout<<"\n\n**********创建一辆汽车**********\n";
	cout<<"请输入座位数:";
	cin>>seatnum;
	Motorcar motorcar(maxspeed,weight,seatnum);
	motorcar.print();
	cout<<"\n**********创建一辆自行车**********\n";
	cout<<"请输入车辆高度:";
	cin>>heigh;
	Bicycle bicycle(maxspeed,weight,heigh);
	bicycle.print();

	cout<<"\n\n**********创建一辆摩托车**********\n输入最高速度:";
	cin>>maxspeed;
	cout<<"输入重量:";
	cin>>weight;
	cout<<"输入座位数:";
	cin>>seatnum;
	cout<<"输入高度:";
	cin>>heigh;
	Motorcycle motorcycle(maxspeed,weight,heigh,seatnum);
	motorcycle.print();

	system("pause");
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值