車輛管理系統(tǒng)_第1頁
車輛管理系統(tǒng)_第2頁
車輛管理系統(tǒng)_第3頁
已閱讀5頁,還剩18頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、C+工程:車輛管理系統(tǒng)目標(biāo):寫一個小程序,要求到用標(biāo)準(zhǔn)模板庫中的list容器和迭代器,可以使用到 多繼承來進行實驗。1.1 General概述1. 在寫代碼之前要仔細閱讀作業(yè)要求2. 要求對不正確的輸入做檢查3. 在你寫完一個類之后,要緊接著寫這個類的測試函數(shù),因此,當(dāng)你調(diào)試你寫的 代碼的時候,你能夠很容易的對你的代碼做重復(fù)檢查。這樣才能夠保證你當(dāng)前寫的代碼能夠準(zhǔn)確無誤的執(zhí)行。可能出于某些原因你要對你寫過的代碼作一此些修改,這 樣你重新測試你修改的代碼就相比照較容易。4. 添加完成你的任務(wù)所需要的函數(shù)。1.2 In troductio n簡介:渥太華這個城市正在創(chuàng)立一個有關(guān)交通工具的“數(shù)據(jù)庫,

2、來為它的議會委員會 做預(yù)算提供較好的參考。這個城市有許多不同種類的機動車輛:客車,貨車,卡車, 緊急車輛救護,消防等,在對這個城市了解之后,要求你設(shè)計一個有以下圖要 求的層次的系統(tǒng)。2 Implement the following functions for each class:為每個類實現(xiàn)以下函數(shù)2.1 (Vehicle 類) (屬性:所有的屬性要求為私有的 )char licensePlate 屬性:車的車牌 (執(zhí)照)作為它的 id, 一個車牌最多可由 8組成。char *type 屬性:車輛類型 (例如:汽車,卡車,消防車 )char *make 屬性:車輛的制造商double ga

3、sTankSize 屬性:總油量double fuelConsumption 屬性:單位路程耗油量函數(shù):R1. 構(gòu)造函數(shù) vehicle (char *type, char* licensePlate, double gasTankSize, double fuelConsumption)Tppe 的默認值為: carlicensePlate 的默認值為 : ottawa01gasTankSize 的默認值為 : 100fuelConsumption 的默認值為 : 10所有其他不在構(gòu)造函數(shù)中的參數(shù)要求置0或置空R2. void setMake (char *make);設(shè)置 制造商 的值R3

4、. setFuelData (double gasTankSize, double fuelConsumption)設(shè)置燃料的有關(guān)信息 (總油量,單位路程耗油量 )R4. setType (char *type)-設(shè)置車輛類型R5. setLice nsePlate(char *lice nse)-設(shè)置車輛的車牌R6. virtual printSpecifications() - 打印車輛的具體信息 ,例如:制造商、類型, 車牌,燃油量和油箱容積R7. virtual double computeTravelDistance()-這個函數(shù)計算車輛可能行使的距離,計算公式為: gasTankS

5、ize * fuelConsumption( 總油量 *單位路程耗油量 )Class LoadVehicle:( LoadVehicleAttributes( 屬性 )int nu mberOfWheelscontains the nu mber of wheels that the vehicle numberOfWheels 屬性:車輛的車輪個數(shù)double loadCapacitythe load weight that the vehicle can carry (in kg)double loadCapacity :車輛的負荷double towingCapacity

6、 the total weight that the vehicle can tow (in kg)double towingCapacity :車輛能夠拖曳的重量FunctionsR8. loadVehicle (char *type, int numberOfWheels, double loadCapacity, double towingCapacity, double gasTankSize, double fuelConsumption).A constructor for the class. The default values for numberOfWheels, load

7、Capacity andtowingCapacity are 6, 500kg and 5000kg respectively. The default values for gasTankSize and fuelConsumption are 200 and 6 respectively. The constructor willinitialize all the fields of the class as required. All other member variables that are notinitialized should be set to the default

8、values of the base class(es), or, if no default values exist, to 0 or NULL.這個類的構(gòu)造函數(shù),各個屬性的默認值: loadCapacity 的默認值: 6, 500kg towingCapacity 的默認值: 5000kg gasTankSize 的默認值: 200fuelConsumption 的默認值: 6其他沒有初始化的成員變量應(yīng)該設(shè)置為基類的默認值一致, 如果沒有默認值, 那么 應(yīng)置0或置空R9. setLoadData (double towingCapacity, double loadCapacity)設(shè)

9、置負荷 ,拖曳量R10. printSpecifications()print the specification of the vehicle (the vehiclespecification from the vehicle class, the towing capacity, the load capacity and the number of wheels).打印出車輛的具體信息,包括從基類繼承的和它自身的。R11. double computeTravelDistance every pair of wheels above four reduces thetravel dis

10、tance by 5% (travelDistance = gasTankSize*fuelConsumption*(100- (numberOfWheels-4)/2*5)/100). For example: if the gasTankSize if 100 litres, thefuelConsumption is 10 km/l and there are 10 wheels then the travelDistance= 100*10*(100-(10-4)/2*5)/100 = 1000*85/100 = 850km.超過 4個車輪,每增加兩個車輪,車輛可行使的車程以 5%減少

11、.例如:總油量為 100公升,耗油量為 10km/ 公升,該車車輛有 10個車輪,它可行使的車 程為:100 * 10 * (100-(10-4)/2 * 5)/100 = 1000*85/100 = 850km.2.3 Class passengerVehicle: (passengerVehicleAttributesint nu mberOfPasse ngers-contains the nu mber of passe ngers that thevehicle canCarry int numberOfPassengers 屬性:車輛可搭乘的乘客的數(shù)量Functions( 函數(shù) )

12、R12. passengerVehicle (char *type, int numPassengers, double gasTankSize, double fuelConsumption)- The default values for numPassengers is 5. The default values for gasTankSize and fuelConsumption are 100 and 11 respectively. The constructorwill initialize all the fields of the class as required usi

13、ng default values of base classes. All other variables that are not initialized are set to 0 or NULL. 該類的構(gòu)造函數(shù),成員變量的默認值: numPassengers 的默認值: 5 gasTankSize 的默認值: 100 fuelConsumption 的默認值: 11 要用基類的構(gòu)造函數(shù)對要求有默認值的屬性進行初始化, 沒其他沒有初始化的成 員要置0或置空R13. setNumPassengers (int numPassengers) 設(shè)置可搭乘的乘客人數(shù)R14. printSpeci

14、fications() -print the specification of the vehicle (the vehicle specification from the vehicle class, and the number of passengers). 打印車輛的具體信息R15. computeTravelDistance() - 超過5個人每多搭乘 1個人行程將以 2.5% 減 少例如:總油量為100公升,燃油量為10km/公升,有7個乘客,那么可行使的路程為: 100*10*(100-(7-5)*2.5)/100 = 1000*95/100 = 950km.2.4 Class

15、 emergencyVehicle(emergencyVehicleAttributes( 屬性 )int numWorkers 屬性:為每輛車分配的工作人員char *driver屬性:為車輛分配的駕駛員char *station 屬性:車輛停靠的車站Fun cti onsR16. emerge ncyVehicle (char *type, char*driver, char *stati on, int nu mPasse ngers,double gasTa nkSize, double fuelC on sumpti on) -構(gòu)造函數(shù),局部屬性平的默認值: fornumPassen

16、gers 的默認值:5 gasTa nkSize 的默認值:100 fuelConsumption 的默認值:11 wheels的默認值:6 其他沒有給初始值的屬性要求置0或置空.R17. setAssignment (char *driver, char *station)設(shè)置分配(司機,??空?R18. printSpecifications()丁印的車輛車輛標(biāo)準(zhǔn)規(guī)格從車輛類,該loadvehciie,客運車輛和emergencyequipment類。打印車輛的具體信息R19. computeTravelDistance() -行程是 loadVehicle 和 passengerVehi

17、cle 中較小的行程2.5 Class EmgergencyEquipmen(緊急設(shè)備類 )Attributes 屬性 int sirenNoiseDistanee-汽笛能夠被聽到的距離,默認為500mint numBeds-可以同時供給病人的床位,默認值為 2Fun cti onsR20. emerge ncyEquipme nt (int sire nN oiseDista nee, i nt nu mBeds) 勾造函數(shù) 給汽笛的可聽見距離,床位初始化R21. getBedsNum() -返回床位數(shù)R22. printSpecifications()-打印汽笛的可聽見距離,床位數(shù)2.6

18、Container class(容器類)要求使用標(biāo)準(zhǔn)模板庫中的list容器,list容器要求可以存儲指向?qū)?象的指針,這樣所有不同類型的車輛可以在同一個容器中(iterator-迭代器的使用)2.7 Decisi on class( 決策類)使用相應(yīng)的迭代器對容器遍歷,使用dyn amic casti ng運行時類型信息進行不同類之間的類型轉(zhuǎn)型(如將指向不同派生類對象的基類指針轉(zhuǎn)為派生類對象指針),可以對相應(yīng)不同類的成員函數(shù)的調(diào)用,這個類由城市的議會用來做決定Attributes屬性listvVehicle*& vehicleList :各類車輛的指針Fun cti ons方法R23. dec

19、isio n(list &)-構(gòu)造函數(shù):接收存儲各類車輛信息的容器R24. prin tVehiclesSpecificatio ns-打印所有車輛的具體信息R25. prin tEmerge ncyVehicles()-單獨打印緊急車輛的緊急數(shù)據(jù)信息R26. i nt numberL on gDista nceEmerge ncyVehicles() 丁印不用中途加油能夠行使800km以上的緊急車輛的數(shù)量R30. int numBeds()-確定以防緊急事件這個城市能夠調(diào)遣的移動床位的數(shù)量R27. i nt numPasse ngers()-確定以防緊急事件這個城市可轉(zhuǎn)移的乘客人數(shù).2.8

20、Testing(測試)主函數(shù)要求能夠獲得供議會做決定的計算結(jié)果int main()list vehicleList;LoadVehicle* lv1 = new LoadVehicle( );back(lv1);Passe ngerVehicle* pv1 = new Passe ngeVehicle( ); back(pv1);EmergencyVehicle* ev1 = new Emergency Vehicle( );back(ev1);Decisi on decisi on( vehicleList);();();();();();delete lv1;delete pv1;dele

21、te ev1;return 0;11. 寫一個學(xué)生類,從person類繼承,增加的屬性為成績f和評語label字符串。person:n ame,age,pri nt() #in cludeclass pers onprivate:char *n ame;int age;public:pers on( char *n ame=,i nt age=0)this-n ame=n ame;this-age=age;void prin t()cout : name 年齡:age;pers on() coutPers on destructore ndl;class stude nt:public per

22、s onprivate:double f;char *label;public:stude nt(char *n=,i nt a=0,double b=0,char *c=):pers on(n, a),f(b),label(c) void prin t()pers on:pri nt(); cout成績:f 評語:labelendl;stude nt()coutStue nt destructore ndl;void mai n()pers on a1;student d1(張三,16,89.5,優(yōu)秀);a1=d1;a1.pri nt();coute ndl;d1.pers on:pri n

23、t(); coute ndl;d1.pri nt();:張三年齡16:張三年齡16:張三年齡16成績:89.5評語:優(yōu)秀Stue nt destructorPers on destructorPress any key to con ti nueexercise1512. Person 為基類:虛函數(shù)為 dailywork() Person()定義三個子類:Stude nt Doctor(char* label) Driver(char* label)主函數(shù):定義基類指針數(shù)組,動態(tài)創(chuàng)立子類對象,調(diào)用成員函數(shù),刪除創(chuàng)立的對象。#in cludeclass Pers on public:Pers

24、on( char *n ame=,i nt age=0) this-n ame=n ame; this-age=age;virtual void dailywork()cout : name年齡:age;virtual Pers on() couterson destructore ndl;private:char *n ame; int age;class Stude nt:public Person public:Stude nt(char *n=,i nt a=0,char *c=erson(n, a),label(c)void dailywork()Perso n:dailywork(

25、);cout學(xué)生;cout評語:labelendl;Stude nt()coutStue nt destructore ndl;private:char *label;class Doctor:public Person筍 4. ,* erson(n,a),label(c)public:Doctor(char *n=,i nt a=O,char *c=)void dailywork()Pers on:dailywork(); cout醫(yī)生;cout評語:labelendl;Doctor()coutDoctor destructore ndl;private:char* label;class

26、Driver:public Pers onpublic:Driver(char *n=,i nt a=0,char *c=)erson(n, a),label(c)void dailywork()Pers on:dailywork(); cout司機;cout評語:labelendl;Driver()coutDriver destructore ndl;private:char* label;void mai n()Student *s=new Student(張三,15,好);Doctor *dnew Doctor( 李四,45,認真);Driver *d2=new Driver(王五,35

27、,優(yōu)秀);Pers on *array3;array0=s;array1=d1;array2=d2;for(i nt i=0;idailywork();delete array0;delete array1;delete array3;張三年齡:15學(xué)生評語:好李四年齡:45醫(yī)生評語:認真王五年齡:35司機評語:優(yōu)秀Stuent destructorPerson destructorDoctor destructorPerson destructorDriver destructorPerson destructorPress any key to continue exercise1613.

28、 修改類 Figure ,將 Point 對象指針作為屬性組合進來, 對 Figure 的子類也做相應(yīng)的修改。#includeclass Pointprivate:double x,y;public:Point(double i,double j):x(i),y(j)void print() const cout(x,y);Point()coutPoint析構(gòu) endl;class Figureprivate:Point *str;Point center;public:Figure(double i=0,double j=0):center(i,j)Point& location()str=

29、¢er;return *str;void move(Point p)str=¢er;str=&p;draw();virtual void draw() =0;virtual void rotate(double)=0;virtual Figure()coutFigure 析構(gòu) endl;class Circle:public Figureprivate:double radius;public:Circle(double i=0,double j=0,double r=0):Figure(i,j),radius(r) void draw()couta circle with c

30、enter; location().print();coutand radiusradiusendl;void rotate(double)coutno effectendl; Circle()coutCircle 析構(gòu) endl;class Square:public Figureprivate:double side,angle;public:Square(double i=0,double j=0,double r=0,double a=0):Figure(i,j),side(r),angle(a)void draw()couta square with center; location

31、().print();coutside lengthsideendlThe angle between one side and the X-axisisangleendl;void rotate(double a)angle+=a; coutThe angle between one side and the X-axis isangleendl;void vertices()coutThe vertices od the square are:endl;Square()coutSquare 析構(gòu) draw(); f-move(Point(2,2);();(1);();運行結(jié)果:a circ

32、le with center(1,2)and radius3a circle with center(1,2)and radius3Point 析構(gòu)Point 析構(gòu)a square with center(4,5)side length6The angle between one side and the X-axis is0The angle between one side and the X-axis is1The vertices od the square are:Square 析構(gòu)Figure 析構(gòu)Point 析構(gòu)Circle 析構(gòu)Figure 析構(gòu)Point 析構(gòu)Press an

33、y key to continue/* 在 Complex 類中實現(xiàn):重載 = , !=*/ answer :#includeclass Complexpublic:Complex(int i=0)x=i;friend int operator =(Complex c1,Complex c2);friend int operator !=(Complex c1,Complex c2); void display();private:int x;void Complex:display()coutx;int operator =(Complex c1,Complex c2)if(c1.x=c2.

34、x)return 1;elsereturn 0;int operator !=(Complex c1,Complex c2)if(c1.x!=c2.x)return 1;elsereturn 0;void main()Complex c1(6),c2(8);if(c1=c2)c1.display();coutis equal to;c2.display(); coutendl;if(c1!=c2)c1.display();coutisnt equal to;c2.display(); coutendl;run result :6isnt equal to8本帖最后由 陶亮 于 2022-11-

35、30 16:31 編輯/*1. 實現(xiàn)一個字符串類 String ,功能包括:復(fù)制賦值運算符1、 Big Three: 析構(gòu)函數(shù) Destructor 復(fù)制構(gòu)造函數(shù) copy constructor copy assignment operator 2、下標(biāo)操作符重載 : 只能作為類的成員函數(shù),不能作為類的友元函數(shù)。3、輸入輸出操作符重載4、= 操作符重載5、+ 操作符重載 參考 Vector 類*/ answer :#include#includeclass Stringpublic:String()str=NULL;String(int m):len(m)str=new charlen;St

36、ring(char *s)str = new charstrlen(s)+1;strcpy(str,s);len=strlen(s);String& operator+(String& c);friend istream& operator(istream& is,String& c); friend ostream& operatorlen-1) cout 整數(shù)下標(biāo)越界 endl; return ch; elsereturn *(str+n);void Disp() coutstrendl; void print() cout(istream& is,String& c)is; return

37、 is;ostream& operator(ostream& os,String& c) ();return os;int operator =(String &c1,String &c2)if(c1.len=c2.len)return 1;elsereturn 0;void main()String word(this is a c+ program.);();cout 位置 0: word0endl; cout 位置 15: word15endl; cout 位置 25: word25endl;word0=t;();int f =10;String word2(f);for(int i=0

38、;i10;i+)word2=word;word2.Disp();String s1(8),s2(5);couts1;cins2;cout 你輸入的兩個值分別為: ; couts1endl;couts2endl;if(s1=s2)s1.print();coutis equal to;s2.pri nt(); coute ndl;(s1+s2).pri nt(); run result :this is a c+ program.位置0 : t位置15 : r整數(shù)下標(biāo)越界位置25 :this is a c+ program.this is a輸入兩個整數(shù)值22你輸入的兩個值分別為:222is eq

39、ual to24Press any key to con ti nue 1.在Complex類中實現(xiàn):重載-=,*= , /=操作符。 answer :#in cludeclass Complexpublic:Complex()Complex(double r ,double i)real = r; image = i;Complex operator -=(Complex & t);Complex operator *=(Complex & t);Complex operator /=(Complex & t); Prin t();private:double real;double ima

40、ge;;Complex Complex: operator -=(Complex& t)real-=; |image=; return *this;Complex Complex: operator *=(Complex & t)Complex Complex: operator /=(Complex& t) real/=;image/=;return *this;Complex: Print()if(image0)coutrealimageie ndl;coutreal+imageie ndl;coute ndl;void mai n()Complex c1(10.0,20.0),c2(5.

41、0,4.0),c3(5.1,4.5);c1-=c2;c1.Prin t();c2*=c3;c2.Prin t();c3/=c1;c3.Prin t(); run result :5+16i25.5+18iPress any key to con ti nue8.實現(xiàn)一個字符串類 String,功能包括:1。Big Three2。下標(biāo)操作符重載3。輸入輸出操作符重載4。=操作符重載5。+操作符重載參考Vector類#in clude#in cludeclass MyStri ngJpublic:MyStri ng(char *s)1str=new charstrle n( s)+1; strcp

42、y(str ,s);len=strle n( s);MyStri ng(i nt m)len=m;str=new charle n;MyStri ng()delete str;char & operator(i nt n)static char ch=0;if(n le n-1)cout整數(shù)下標(biāo)越界;return ch;else return *(str+ n);friend ostrea m& operator (istrea m& in,M yStri ng& x)L in;return in;ostrea m& operator (ostream& out,MyStri ng& a)outtstr);return strcat(str1,x.str);delete str1;*str 仁NULL;bool operator=(MyStri ng& a,MyStri ng& b)bool yes=true;if(strle n()!=strle n()yes=false;elseint in dex=0;int s=strle n();while(i ndexs&ai ndex=bi

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論