[YTU]_2575( 交通工具信息)
生活随笔
收集整理的這篇文章主要介紹了
[YTU]_2575( 交通工具信息)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述
有一個交通工具類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#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; }總結
以上是生活随笔為你收集整理的[YTU]_2575( 交通工具信息)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [YTU]_2576( 虚函数练习:动物
- 下一篇: [YTU]_2477( C++习题 抽象