交通工具信息

交通工具信息

时间: 1ms        内存:128M

描述:

有一个交通工具类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

输入:

输入三个派生类对应名字。

输出:

输出对应类型的信息。

示例输入:

Mercedes_Benz
Transport_truck
Boat

示例输出:

Mercedes_Benz
250km/h
14.8L/100km
Transport_truck
140km/h
18L/100km
Boat
50km/h
8.33L/100km

提示:

参考答案(内存最优[0]):

#include<iostream>
#include<string>
using namespace std;
class Vehicle
{
public:
	virtual void showinfo ()=0;
};
class Car:public Vehicle
{
public:
	Car(string n):name(n){}
	void showinfo (){
		cout<<name<<endl;
		cout<<"250km/h"<<endl;
		cout<<"14.8L/100km"<<endl;}
private:
	string name;
};
class Truck:public Vehicle
{
public:
	Truck(string n):name(n){}
	void showinfo (){
		cout<<name<<endl;
		cout<<"140km/h"<<endl;
		cout<<"18L/100km"<<endl;}
private:
	string name;
};
class Boat:public Vehicle
{
public:
	Boat(string n):name(n){}
	void showinfo (){
		cout<<name<<endl;
		cout<<"50km/h"<<endl;
		cout<<"8.33L/100km"<<endl;}
private:
	string name;
};



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;

} 

参考答案(时间最优[0]):

#include <iostream>
#include <string>
using namespace std;
class Vehicle
{
public:
    virtual void showinfo ()=0;
};
class Car:public Vehicle
{
public:
    Car(string a):name(a){}
    virtual void showinfo()
    {
        cout<<"Mercedes Benz"<<endl;
        cout<<"250km/h"<<endl;
        cout<<"14.8 L/100km"<<endl;
    }
protected:
    string name;
};
class Truck:public Vehicle
{
public:
    Truck(string a):name(a){}
    virtual void showinfo()
    {
        cout<<"Transport truck"<<endl;
        cout<<"140km/h"<<endl;
        cout<<"18L/100km"<<endl;
    }
protected:
    string name;
};
class Boat:public Vehicle
{
public:
    Boat(string a):name(a){}
    virtual void showinfo()
    {
        cout<<"Boat"<<endl;
        cout<<"50km/h"<<endl;
        cout<<"8.33L/100km"<<endl;
    }
protected:
    string name;
};
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;

}


题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注