[YTU]_2575( 交通工具信息)

来源:互联网 发布:3ds淘宝哪家 编辑:程序博客网 时间:2024/06/15 03:15

题目描述

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

样例输出

Mercedes_Benz250km/h14.8L/100kmTransport_truck140km/h18L/100kmBoat50km/h8.33L/100km
#include <iostream>using namespace std;class animal{    public:animal(int h,int w,char s):height(h),weight(w),sex(s){}    virtual void display()    {        cout<<"height:"<<height<<endl<<"weight:"<<weight<<endl<<"sex:"<<sex<<endl;    }protected:        int height,weight;        char sex;};class aqu_animal:public animal{    public:        aqu_animal(int h,int w,char s,int s_p):animal(h,w,s),swimming_speed(s_p){}  void display()        {            cout<<"height:"<<height<<endl<<"weight:"<<weight<<endl<<"sex:"<<sex<<endl<<"swimming_speed:"<<swimming_speed<<endl;        }    protected:        int swimming_speed;};class ter_animal:public animal{public:    ter_animal(int h,int w,char s,int r_p):animal(h,w,s),running_speed(r_p){} void display()    {          cout<<"height:"<<height<<endl<<"weight:"<<weight<<endl<<"sex:"<<sex<<endl<<"running_speed:"<<running_speed<<endl;    }protected:  int running_speed;};int main(){    int a,b,s,r;    char c;    animal *p;    cin>>a>>b>>c;    animal pa(a,b,c);    p=&pa;    p->display();    cin>>a>>b>>c>>s;    aqu_animal pb(a,b,c,s);    p=&pb;    p->display();    cin>>a>>b>>c>>r;    ter_animal pc(a,b,c,r);    p=&pc;    p->display();    return 0;}