Problem E: 交通工具信息
Description
有一个交通工具类vehicle。将它为 基类派生的有派生小车类car,卡车类truck和轮船类boat。定义这些类,并使其可以显示其各类交通工具的具体信息。
他们包括的信息,有例如以下几种:
1.名字 -----与输入的名字相符合
2.时速(km/h) -----最高时速
3.耗油量(L/100km) -----在经济时速下測得耗油量
小车类:
250km/h
14.8 L/100km
卡车类:
140km/h
18L/100km
轮船类:
50km/h
8.33L/100km
Input
Output
Sample Input
Mercedes_Benz
Transport_truck
Boat
Sample Output
Mercedes_Benz
250km/h
14.8L/100km
Transport_truck
140km/h
18L/100km
Boat
50km/h
8.33L/100km
HINT
#include <iostream>
#include <string>
using namespace std;
class Vehicle
{
public:
string name ;
public:
Vehicle (string h): name(h){}
virtual void showinfo () const
{
cout<<name<<endl;
}
};
class Car :virtual public Vehicle
{
public:
double Speed;
double fuel ;
public:
Car (string h): Vehicle (h),Speed(250),fuel(14.8){}
virtual void showinfo () const
{
cout<<name<<endl;
cout<<Speed<<"km/h"<<endl;
cout<<fuel<<"L/100km"<<endl;
}
};
class Truck :virtual public Vehicle
{
public:
double Speed;
double fuel ;
public:
Truck (string h): Vehicle (h),Speed(140),fuel(18){}
virtual void showinfo () const
{
cout<<name<<endl;
cout<<Speed<<"km/h"<<endl;
cout<<fuel<<"L/100km"<<endl;
}
};
class Boat :virtual public Vehicle
{
public:
double Speed;
double fuel ;
public:
Boat (string h): Vehicle (h),Speed(50),fuel(8.33){}
virtual void showinfo () const
{
cout<<name<<endl;
cout<<Speed<<"km/h"<<endl;
cout<<fuel<<"L/100km"<<endl;
}
};
int main(){
Vehicle *vp;
string name0,name1,name2;
cin>>name0;
Car car(name0);
cin>>name1;
Truck truck(name1);
cin>>name2;
Boat boat(name2);
vp=&car;
vp->showinfo ();
vp=&truck;
vp->showinfo ();
vp=&boat;
vp->showinfo ();
return 0;
}