Problem G: 交通工具資訊
Time Limit: 1 Sec Memory Limit:
128 MB
Submit: 459 Solved: 231
[Submit][Status][Web
Board]
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
提交的時候請把主函式加上:
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;
}
輸入名字的順序與對應的類順序一致,你需要把給出的名字賦給類。
#include <iostream>
using namespace std;
class Vehicle
{
public:
virtual void showinfo()const=0;
};
class Car: public Vehicle
{
public:
Car(string cc):c(cc) {}
virtual void showinfo () const
{
cout<<c<<endl;
cout<<"250km/h"<<endl;
cout<<"14.8L/100km"<<endl;
}
private:
string c;
};
class Truck: public Vehicle
{
public:
Truck(string tt):t(tt) {}
virtual void showinfo () const
{
cout<<t<<endl;
cout<<"140km/h"<<endl;
cout<<"18L/100km"<<endl;
}
private:
string t;
};
class Boat: public Vehicle
{
public:
Boat(string bb):b(bb) {}
virtual void showinfo () const
{
cout<<b<<endl;
cout<<"50km/h"<<endl;
cout<<"8.33L/100km"<<endl;
}
private:
string b;
};
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;
}