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;
}