第十三周 项目3车辆类(继承)
生活随笔
收集整理的這篇文章主要介紹了
第十三周 项目3车辆类(继承)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*
* Copyright (c) 2011, 煙臺大學計算機學院
* All rights reserved.
* 作 者:王靜
* 完成日期:2013 年 6 月 2 日
* 版 本 號:v1.0
* 輸入描述:
* 問題描述:在下面一段類的定義中,自行車類的虛基類為車輛類,機動車類的虛基類也為車輛類,摩托車類的基類為自行車類和機動車類,類之間均為公有繼承。
(1)根據上面各類間關系的描述,補全下面程序段中空缺的代碼
(2)實現程序中聲明的成員函數,注意相應操作中的動作發生的條件不能滿足時應給出提示。
(3)運行程序,享受開摩托的過程。(BB平臺上提供了一個可執行文件,可以先運行再編程。不必申請駕照,這個摩托車很安全。)
(4)在報告中回答問題:本題中使用虛基類的好處是什么?
* 程序輸出:
* 問題分析:
* 算法設計:略
*/
#include <iostream>
#include<conio.h>
#include <windows.h>
using namespace std;
enum vehicleStaus {rest, running}; //車輛狀態:泊車、行進
class vehicle //車輛類
{
protected:int maxSpeed; //最大車速int currentSpeed; //當前速度int weight; //車重vehicleStaus status; //rest-泊車狀態;running-行進狀態
public:vehicle(int maxS, int w): currentSpeed(0),maxSpeed(maxS),weight(w),status(rest){} //構造函數,初始時,當前速度總為0且處在停車狀態void start(); //由rest狀態到running, 初速為1void stop(); //由running狀態到rest, 當前速度小于5時,才允許停車void speed_up(); //加速,調用1次,速度加1void slow_down(); //減速,調用1次,速度減1,速度為0時,停車
};void vehicle::start() //由rest狀態到running, 初速為1{if(status==rest){currentSpeed=1;status=running;}elsecout<<"車輛已經行駛!"<<endl;}void vehicle::stop() //由running狀態到rest, 當前速度小于5時,才允許停車{if(status==running){if(currentSpeed<5){status=rest;currentSpeed=0;}else{cout<<"車速太快,先減速!"<<endl;}}elsecout<<"車輛未啟動"<<endl;}void vehicle::speed_up() //加速,調用1次,速度加1{if(status==running)if(currentSpeed<maxSpeed)currentSpeed++;elsecout<<"請不要超速行駛"<<endl;elsecout<<"車輛未啟動"<<endl;}void vehicle::slow_down() //減速,調用1次,速度減1,速度為0時,停車{if(status==running){if(currentSpeed>0)currentSpeed--;}elsecout<<"車輛未啟動!"<<endl;if(currentSpeed==0)status=rest;}
class bicycle :virtual public vehicle//(1)自行車類的虛基類為車輛類
{
protected:double height; //車高
public:bicycle(int maxS=10, int w=50, int h=0.7); //定義構造函數
};
bicycle::bicycle(int maxS,int w,int h):vehicle(maxS,w),height(h){}
class motorcar : virtual public vehicle//(2)機動車類的虛基類也為車輛類
{
protected:int seatNum; //座位數int passengerNum; //乘客人數
public:motorcar(int maxS=150, int w=1500, int s=5, int p=1); //定義構造函數void addPassenger(int p=1); //增加搭載的乘客,超員要拒載,有人下車時,p為負數。當然車上乘客至少有1個(司機)。只有車停穩后才能上下客。
};
motorcar::motorcar(int maxS, int w, int s, int p):vehicle(maxS,w),seatNum(s),passengerNum(p){} //定義構造函數
void motorcar::addPassenger(int p)
{if (status==running){cout<<"車輛正在行駛,停車后再上下車!"<<endl;}else{passengerNum+=p;if(passengerNum>seatNum){passengerNum=seatNum;cout<<"涉嫌超員,已清理后達到滿員!"<<endl;}else if (passengerNum<1){passengerNum=1;cout<<"請司機不要離開崗位!"<<endl;}}
}
class motorcycle:public bicycle,public motorcar //(3)摩托車類的基類為自行車類和機動車類
{
public://定義構造函數motorcycle(int maxS=90, int w=100, int s=3, int p=1, int h=0.7);void show(); //顯示摩托車的運行狀態
};
motorcycle::motorcycle(int maxS, int w, int s, int p, int h):vehicle(maxS, w),bicycle(maxS,w,h),motorcar(maxS,w,s,p){}
void motorcycle::show()//顯示摩托車的運行狀態
{cout<<"狀態:";if(status==rest)cout<<"泊車;\t";elsecout<<"行進;\t";cout<<"車速:"<<currentSpeed<<" / "<< maxSpeed <<"\t當前乘員:"<<passengerNum<<" / "<< seatNum << endl;
}
int main( )
{motorcycle m;bool end=false;while (!end){cout<<"請操作:1-啟動 2-加速 3-減速 4-有人上車 5-有人下車 6-停車 0-結束"<<endl;char keydown= _getch(); //_getch()返回鍵盤上讀取的字符switch(keydown){case '1':cout<<"操作(啟動)\t"; m.start(); break;case '2':cout<<"操作(加速)\t"; m.speed_up(); break;case '3':cout<<"操作(減速)\t"; m.slow_down(); break;case '4':cout<<"操作(有人上車)\t"; m.addPassenger(1); break;case '5':cout<<"操作(有人下車)\t"; m.addPassenger(-1); break;case '6':cout<<"操作(停車)\t"; m.stop(); break;case '0':end=true; break;}m.show();cout<<endl;Sleep(200); //要包含頭文件<windows.h>}return 0;
}
?運行結果:
總結:
很差,這次看到題就害怕了,沒讀懂題,就放棄了,以后不會了,呵呵。
總結
以上是生活随笔為你收集整理的第十三周 项目3车辆类(继承)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: P2617 Dynamic Ra
- 下一篇: 实时稀疏点云分割