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

下載本文檔

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

文檔簡介

年4月19日車輛管理系統(tǒng)文檔僅供參考,不當之處,請聯(lián)系改正。C++項目:車輛管理系統(tǒng)目標:寫一個小程序,要求到用標準模板庫中的list容器和迭代器,能夠使用到多繼承來進行實驗。1.1General(概述)1.在寫代碼之前要仔細閱讀作業(yè)要求2.要求對不正確的輸入做檢查3.在你寫完一個類之后,要緊接著寫這個類的測試函數(shù),因此,當你調(diào)試你寫的代碼的時候,你能夠很容易的對你的代碼做重復檢查。這樣才能夠保證你當前寫的代碼能夠準確無誤的執(zhí)行。可能出于某些原因你要對你寫過的代碼作一此些修改,這樣你重新測試你修改的代碼就相對比較容易。4.添加完成你的任務所需要的函數(shù)。1.2Introduction(簡介):渥太華這個城市正在創(chuàng)立一個有關(guān)交通工具的“數(shù)據(jù)庫”,來為它的議會(委員會)做預算提供較好的參考。這個城市有許多不同種類的機動車輛:客車,貨車,卡車,緊急車輛(救護,消防等),在對這個城市了解之后,要求你設計一個有下圖要求的層次的系統(tǒng)。

2Implementthefollowingfunctionsforeachclass:為每個類實現(xiàn)以下函數(shù)2.1(Vehicle類)(屬性:所有的屬性要求為私有的)charlicensePlate[]屬性:車的車牌(執(zhí)照)作為它的id,一個車牌最多可由8組成。char*type屬性:車輛類型(例如:汽車,卡車,消防車)char*make屬性:車輛的制造商doublegasTankSize屬性:總油量doublefuelConsumption屬性:單位路程耗油量函數(shù):R1.構(gòu)造函數(shù)vehicle(char*type,char*licensePlate,doublegasTankSize,doublefuelConsumption)Tppe的默認值為:carlicensePlate的默認值為:ottawa01gasTankSize的默認值為:100fuelConsumption的默認值為:10所有其它不在構(gòu)造函數(shù)中的參數(shù)要求置0或置空R2.voidsetMake(char*make);設置制造商的值R3.setFuelData(doublegasTankSize,doublefuelConsumption)–設置燃料的有關(guān)信息(總油量,單位路程耗油量)R4.setType(char*type)–設置車輛類型R5.setLicensePlate(char*license)–設置車輛的車牌R6.virtualprintSpecifications()-打印車輛的具體信息,例如:制造商、類型,車牌,燃油量和油箱容積R7.virtualdoublecomputeTravelDistance()–這個函數(shù)計算車輛可能行使的距離,計算公式為:gasTankSize*fuelConsumption(總油量*單位路程耗油量)2.2ClassLoadVehicle:(LoadVehicle類)Attributes(屬性)intnumberOfWheels–numberOfWheels屬性:車輛的車輪個數(shù)doubleloadCapacity–theloadweightthatthevehiclecancarry(inkg)doubleloadCapacity:車輛的負荷doubletowingCapacity–thetotalweightthatthevehiclecantow(inkg)doubletowingCapacity:車輛能夠拖曳的重量FunctionsR8.loadVehicle(char*type,intnumberOfWheels,doubleloadCapacity,doubletowingCapacity,doublegasTankSize,doublefuelConsumption).Aconstructorfortheclass.ThedefaultvaluesfornumberOfWheels,loadCapacityandtowingCapacityare6,500kgand5000kgrespectively.ThedefaultvaluesforgasTankSizeandfuelConsumptionare200and6respectively.Theconstructorwillinitializeallthefieldsoftheclassasrequired.Allothermembervariablesthatarenotinitializedshouldbesettothedefaultvaluesofthebaseclass(es),or,ifnodefaultvaluesexist,to0orNULL.這個類的構(gòu)造函數(shù),各個屬性的默認值:loadCapacity的默認值:6,500kgtowingCapacity的默認值:5000kggasTankSize的默認值:200fuelConsumption的默認值:6其它沒有初始化的成員變量應該設置為基類的默認值一致,如果沒有默認值,則應置0或置空R9.setLoadData(doubletowingCapacity,doubleloadCapacity)設置負荷,拖曳量R10.printSpecifications()–printthespecificationofthevehicle(thevehiclespecificationfromthevehicleclass,thetowingcapacity,theloadcapacityandthenumberofwheels).打印出車輛的具體信息,包括從基類繼承的和它自身的。R11.doublecomputeTravelDistance–everypairofwheelsabovefourreducesthetraveldistanceby5%(travelDistance=gasTankSize*fuelConsumption*(100-(numberOfWheels-4)/2*5)/100).Forexample:ifthegasTankSizeif100litres,thefuelConsumptionis10km/landthereare10wheelsthenthetravelDistance=100*10*(100-(10-4)/2*5)/100=1000*85/100=850km.超過4個車輪,每增加兩個車輪,車輛可行使的車程以5%減少.例如:總油量為100公升,耗油量為10km/公升,該車車輛有10個車輪,它可行使的車程為:100*10*(100-(10-4)/2*5)/100=1000*85/100=850km.2.3ClasspassengerVehicle:(passengerVehicle類)AttributesintnumberOfPassengers–containsthenumberofpassengersthatthevehiclecanCarryintnumberOfPassengers屬性:車輛可搭乘的乘客的數(shù)量Functions(函數(shù))R12.passengerVehicle(char*type,intnumPassengers,doublegasTankSize,doublefuelConsumption)–ThedefaultvaluesfornumPassengersis5.ThedefaultvaluesforgasTankSizeandfuelConsumptionare100and11respectively.Theconstructorwillinitializeallthefieldsoftheclassasrequiredusingdefaultvaluesofbaseclasses.Allothervariablesthatarenotinitializedaresetto0orNULL.該類的構(gòu)造函數(shù),成員變量的默認值:numPassengers的默認值:5gasTankSize的默認值:100fuelConsumption的默認值:11要用基類的構(gòu)造函數(shù)對要求有默認值的屬性進行初始化,沒其它沒有初始化的成員要置0或置空R13.setNumPassengers(intnumPassengers)設置可搭乘的乘客人數(shù)R14.printSpecifications()–printthespecificationofthevehicle(thevehiclespecificationfromthevehicleclass,andthenumberofpassengers).打印車輛的具體信息R15.computeTravelDistance()–超過5個人每多搭乘1個人行程將以2.5%減少例如:總油量為100公升,燃油量為10km/公升,有7個乘客,則可行使的路程為:100*10*(100-(7-5)*2.5)/100=1000*95/100=950km.2.4ClassemergencyVehicle(emergencyVehicle類)Attributes(屬性)intnumWorkers屬性:為每輛車分配的工作人員char*driver屬性:為車輛分配的駕駛員char*station屬性:車輛停靠的車站FunctionsR16.emergencyVehicle(char*type,char*driver,char*station,intnumPassengers,doublegasTankSize,doublefuelConsumption)–構(gòu)造函數(shù),部分屬性平的默認值:fornumPassengers的默認值:5gasTankSize的默認值:100fuelConsumption的默認值:11wheels的默認值:6其它沒有給初始值的屬性要求置0或置空.R17.setAssignment(char*driver,char*station)設置分配(司機,??空?R18.printSpecifications()–打印的車輛(車輛規(guī)范規(guī)格從車輛類,該loadvehcile,客運車輛和emergencyequipment類)。打印車輛的具體信息R19.computeTravelDistance()–行程是loadVehicle和passengerVehicle中較小的行程2.5ClassEmgergencyEquipmen(緊急設備類)Attributes(屬性)intsirenNoiseDistance–汽笛能夠被聽到的距離,默認為500mintnumBeds–能夠同時供給病人的床位,默認值為2FunctionsR20.emergencyEquipment(intsirenNoiseDistance,intnumBeds)–構(gòu)造函數(shù)給汽笛的可聽見距離,床位初始化R21.getBedsNum()–返回床位數(shù)R22.printSpecifications()–打印汽笛的可聽見距離,床位數(shù)2.6Containerclass(容器類)要求使用標準模板庫中的list容器,list容器要求能夠存儲指向?qū)ο蟮闹羔?,這樣所有不同類型的車輛能夠在同一個容器中(iterator-迭代器的使用)2.7Decisionclass(決策類)使用相應的迭代器對容器遍歷,使用dynamiccasting(運行時類型信息)進行不同類之間的類型轉(zhuǎn)型(如將指向不同派生類對象的基類指針轉(zhuǎn)為派生類對象指針),能夠?qū)ο鄳煌惖某蓡T函數(shù)的調(diào)用,這個類由城市的議會用來做決定Attributes(屬性)list<Vehicle*>&vehicleList:各類車輛的指針Functions(方法)R23.decision(list<Vehicle*>&)–構(gòu)造函數(shù):接收存儲各類車輛信息的容器R24.printVehiclesSpecifications-打印所有車輛的具體信息R25.printEmergencyVehicles()-單獨打印緊急車輛的緊急數(shù)據(jù)信息R26.intnumberLongDistanceEmergencyVehicles()–打印不用中途加油能夠行使800km以上的緊急車輛的數(shù)量R30.intnumBeds()-確定以防緊急事件這個城市能夠調(diào)遣的移動床位的數(shù)量R27.intnumPassengers()–確定以防緊急事件這個城市可轉(zhuǎn)移的乘客人數(shù).2.8Testing(測試)主函數(shù)要求能夠獲得供議會做決定的計算結(jié)果intmain(){list<Vehicle*>vehicleList;LoadVehicle*lv1=newLoadVehicle(…);vehicleList.push_back(lv1);…PassengerVehicle*pv1=newPassengeVehicle(…);vehicleList.push_back(pv1);….EmergencyVehicle*ev1=newEmergencyVehicle(…);vehicleList.push_back(ev1);….Decisiondecision(vehicleList);decision.printVehiclesSpecifications();decision.printEmergencyVehicles();decision.numberLongDistanceEmergencyVehicles();decision.numBeds();decision.numPassengers();deletelv1;deletepv1;deleteev1;…return0;}11.寫一個學生類,從person類繼承,增加的屬性為成績f和評語label(字符串)。person:name,age,print()

#include<iostream.h>

classperson

{

private:

char*name;

intage;

public:

person(char*name="",intage=0)

{

this->name=name;

this->age=age;

}

voidprint()

{

cout<<"姓名:"<<name<<"

年齡:"<<age;

}

~person(){cout<<"Persondestructor"<<endl;}

};

classstudent:publicperson

{

private:

doublef;

char*label;

public:

student(char*n="",inta=0,doubleb=0,char*c=""):person(n,a),f(b),label(c){}

voidprint()

{

person::print();

cout<<"

成績:"<<f<<"

評語:"<<label<<endl;

}

~student(){cout<<"Stuentdestructor"<<endl;}

};

voidmain()

{

persona1;

studentd1("張三",16,89.5,"優(yōu)秀");

a1=d1;

a1.print();

cout<<endl;

d1.person::print();

cout<<endl;

d1.print();

}

運行結(jié)果:

姓名:張三

年齡:16

姓名:張三

年齡:16

姓名:張三

年齡:16

成績:89.5

評語:優(yōu)秀

Stuentdestructor

Persondestructor

Pressanykeytocontinueexercise15

12.Person為基類:虛函數(shù)為dailywork()~Person()

定義三個子類:StudentDoctor(char*label)Driver(char*label)

主函數(shù):定義基類指針數(shù)組,動態(tài)創(chuàng)立子類對象,調(diào)用成員函數(shù),刪除創(chuàng)立的對象。

#include<iostream.h>

classPerson

{

public:

Person(char*name="",intage=0)

{

this->name=name;

this->age=age;

}

virtualvoiddailywork(){cout<<"姓名:"<<name<<"

年齡:"<<age;}

virtual~Person(){cout<<"ersondestructor"<<endl;}

private:

char*name;

intage;

};

classStudent:publicPerson

{

public:

Student(char*n="",inta=0,char*c="")erson(n,a),label(c){}

voiddailywork()

{

Person::dailywork();

cout<<"

學生";

cout<<"

評語:"<<label<<endl;

}

~Student(){cout<<"Stuentdestructor"<<endl;}

private:

char*label;

};

classDoctor:publicPerson

{

public:

Doctor(char*n="",inta=0,char*c="")erson(n,a),label(c){}

voiddailywork()

{

Person::dailywork();

cout<<"

醫(yī)生";

cout<<"

評語:"<<label<<endl;

}

~Doctor(){cout<<"Doctordestructor"<<endl;}

private:

char*label;

};

classDriver:publicPerson

{

public:

Driver(char*n="",inta=0,char*c="")erson(n,a),label(c){}

voiddailywork()

{

Person::dailywork();

cout<<"

司機";

cout<<"

評語:"<<label<<endl;

}

~Driver(){cout<<"Driverdestructor"<<endl;}

private:

char*label;

};

voidmain()

{

Student*s=newStudent("張三",15,"好");

Doctor*d1=newDoctor("李四",45,"認真");

Driver*d2=newDriver("王五",35,"優(yōu)秀");

Person*array[3];

array[0]=s;

array[1]=d1;

array[2]=d2;

for(inti=0;i<3;i++)array[i]->dailywork();

deletearray[0];

deletearray[1];

deletearray[3];

}

運行結(jié)果:

姓名:張三

年齡:15

學生

評語:好

姓名:李四

年齡:45

醫(yī)生

評語:認真

姓名:王五

年齡:35

司機

評語:優(yōu)秀

Stuentdestructor

Persondestructor

Doctordestructor

Persondestructor

Driverdestructor

Persondestructor

Pressanykeytocontinueexercise16

13.修改類Figure,將Point對象指針作為屬性組合進來,對Figure的子類也做相應的修改。

#include<iostream.h>

classPoint

{

private:

doublex,y;

public:

Point(doublei,doublej):x(i),y(j){}

voidprint()const

{

cout<<"("<<x<<","<<y<<")";

}

~Point(){cout<<"Point析構(gòu)"<<endl;}

};

classFigure

{

private:

Point*str;

Pointcenter;

public:

Figure(doublei=0,doublej=0):center(i,j){}

Point&location()

{

str=¢er;

return*str;

}

voidmove(Pointp)

{

str=¢er;

str=&p;

draw();

}

virtualvoiddraw()=0;

virtualvoidrotate(double)=0;

virtual~Figure(){cout<<"Figure析構(gòu)"<<endl;}

};

classCircle:publicFigure

{

private:

doubleradius;

public:

Circle(doublei=0,doublej=0,doubler=0):Figure(i,j),radius(r){}

voiddraw()

{

cout<<"acirclewithcenter";

location().print();

cout<<"andradius"<<radius<<endl;

}

voidrotate(double){cout<<"noeffect"<<endl;}

~Circle(){cout<<"Circle析構(gòu)"<<endl;}

};

classSquare:publicFigure

{

private:

doubleside,angle;

public:

Square(doublei=0,doublej=0,doubler=0,doublea=0):Figure(i,j),side(r),angle(a){}

voiddraw()

{

cout<<"asquarewithcenter";

location().print();

cout<<"sidelength"<<side<<endl<<"TheanglebetweenonesideandtheX-axis

is"<<angle<<endl;

}

voidrotate(doublea)

{

angle+=a;

cout<<"TheanglebetweenonesideandtheX-axisis"<<angle<<endl;

}

voidvertices()

{

cout<<"Theverticesodthesquareare:"<<endl;

}

~Square(){cout<<"Square析構(gòu)"<<endl;}

};

voidmain()

{

Circlec(1,2,3);

Squares(4,5,6);

Figure*f=&c;

Figure&g=s;

f->draw();

f->move(Point(2,2));

g.draw();

g.rotate(1);

s.vertices();

}

運行結(jié)果:

acirclewithcenter(1,2)andradius3

acirclewithcenter(1,2)andradius3

Point析構(gòu)

Point析構(gòu)

asquarewithcenter(4,5)sidelength6

TheanglebetweenonesideandtheX-axisis0

TheanglebetweenonesideandtheX-axisis1

Theverticesodthesquareare:

Square析構(gòu)

Figure析構(gòu)

Point析構(gòu)

Circle析構(gòu)

Figure析構(gòu)

Point析構(gòu)

Pressanykeytocontinue/*在Complex類中實現(xiàn):重載==,!=*/

answer:

#include<iostream.h>

classComplex

{

public:

Complex(inti=0){x=i;}

friendintoperator==(Complexc1,Complexc2);

friendintoperator!=(Complexc1,Complexc2);

voiddisplay();

private:

intx;

};

voidComplex::display()

{

cout<<x;

}

intoperator==(Complexc1,Complexc2)

{

if(c1.x==c2.x)

return1;

else

return0;

}

intoperator!=(Complexc1,Complexc2)

{

if(c1.x!=c2.x)

return1;

else

return0;

}

voidmain()

{

Complexc1(6),c2(8);

if(c1==c2)

{

c1.display();cout<<"isequalto";c2.display();

cout<<endl;

}

if(c1!=c2)

{

c1.display();cout<<"isn'tequalto";c2.display();

cout<<endl;

}

}

runresult:

6isn'tequalto8本帖最后由陶亮于-11-3016:31編輯

/*

1.實現(xiàn)一個字符串類String,功能包括:

1、BigThree:析構(gòu)函數(shù)(Destructor)復制構(gòu)造函數(shù)(copyconstructor)復制賦值運算符(copyassignmentoperator)

2、下標操作符重載

:只能作為類的成員函數(shù),不能作為類的友元函數(shù)。

3、輸入輸出操作符重載

4、==操作符重載

5、+操作符重載

參考Vector類

*/

answer:

#include<iostream.h>

#include<string.h>

classString

{

public:

String(){str=NULL;}

String(intm):len(m)

{

str=newchar[len];

}

String(char*s)

{

str=newchar[strlen(s)+1];

strcpy(str,s);

len=strlen(s);

}

String&operator+(String&c);

friend

istream&operator>>(istream&is,String&c);

friend

ostream&operator<<(ostream&os,String&c);

friendintoperator==(String&c1,String&c2);

~String()

{

if(str!=NULL)

delete[]str;

}

char&operator[](intn)

{

staticcharch=0;

if(n>len-1)

{

cout<<"整數(shù)下標越界"<<endl;

returnch;

}

else

return*(str+n);

}

voidDisp()

{

cout<<str<<endl;

}

voidprint()

{

cout<<len;

}

private:

intlen;

char*str;

};

String&String::operator+(String&c)

{

len+=c.len;

return*this;

}

istream&operator>>(istream&is,String&c)

{

is>>c.len;

returnis;

}

ostream&operator<<(ostream&os,String&c)

{

c.print();

returnos;

}

intoperator==(String&c1,String&c2)

{

if(c1.len==c2.len)

return1;

else

return0;

}

voidmain()

{

Stringword("thisisac++program.");

word.Disp();

cout<<"位置0:"<<word[0]<<endl;

cout<<"位置15:"<<word[15]<<endl;

cout<<"位置25:"<<word[25]<<endl;

word[0]='t';

word.Disp();

intf=10;

Stringword2(f);

for(inti=0;i<10;i++)

word2=word;

word2.Disp();

Strings1(8),s2(5);

cout<<"輸入兩個整數(shù)值";

cin>>s1;

cin>>s2;

cout<<"你輸入的兩個值分別為:";

cout<<s1<<endl;

cout<<s2<<endl;

if(s1==s2)

{

s1.print();

cout<<"isequalto";

s2.print();

cout<<endl;

}

(s1+s2).print();

}

runresult:

thisisac++program.

位置0:t

位置15:r

整數(shù)下標越界

位置25:

thisisac++program.

thisisa

輸入兩個整數(shù)值2

2

你輸入的兩個值分別為:2

2

2isequalto2

4Pressanykeytocontinue

1.在Complex類中實現(xiàn):重載-=,*=,/=操作符。

answer:

#include<iostream.h>

classComplex

{

public:

Complex(){}

Complex(doubler,doublei)

{

real=r;

image=i;

}

Complexoperator-=(Complex&t);

Complexoperator*=(Complex&t);

Complexoperator/=(Complex&t);

Print();

private:

doublereal;

doubleimage;

};

ComplexComplex::operator-=(Complex&t)

{

real-=t.real;

image-=t.image;

return*this;

}

ComplexComplex::operator*=(Complex&t)

{

real*=t.real;

image*=t.image;

return*this;

}

ComplexComplex::operator/=(Complex&t)

{

real/=t.real;

image/=t.image;

return*this;

}

Complex::Print()

{

if(image<0)

cout<<real<<image<<"i"<<endl;

else

cout<<real<<"+"<<image<<"i"<<endl;

cout<<endl;

}

voidmain()

{

Complexc1(10.0,20.0),c2(5.0,4.0),c3(5.1,4.5);

c1-=c2;

c1.Print();

c2*=c3;

c2.Print();

c3/=c1;

c3.Print();

}

runresult:

5+16i

25.5+18i

1.02+0.28125i

Pressanykeytocontinue8.實現(xiàn)一個字符串類String,功能包括:

1。BigThree

2。下標操作符重載

3。輸入輸出操作符重載

4。==操作符重載

5。+操作符重載

參考Vector類

#include<iostream.h>

#include<string.h>

classMyString

{

public:

MyString(char*s)

{

str=newchar[strlen(s)+1];

strcpy(str,s);

len=strlen(s);

}

MyString(intm)

{

len=m;

str=newchar[len];

}

~MyString()

{

delete[]str;

}

char&operator[](intn)

{

staticcharch=0;

if(n>len-1)

{

cout<<"整數(shù)下標越界";

returnch;

}

elsereturn*(str+n);

}

friendostream&operator<<(ostream&out,MyString&a);

friendistream&operator>>(istream&in,MyString&x);

MyStringoperator+(MyString&x);

friendbooloperator==(MyString&a,MyString&b);

private:

intlen;

char*str;

};

istream&operator>>(istream&in,MyString&x)

{

in>>x.str;

returnin;

}

ostream&operator<<(ostream&out,MyString&a)

{

out<<a.len<<"\t"<<a.str;

returnout;

}

MyStringMyString:perator+(MyString&x)

{

intlen1=strlen(x.str);

len=len+len1;

char*str1;

str1=newchar[len+1];

strcpy(str1,this->str);

returnstrcat(str1,x.str);

deletestr1;

*str1=NULL;

}

booloperator==(MyString&a,MyString&b)

{

boolyes=true;

if(strlen(a.str)!=strlen(b.str)){yes=false;}

else

{

intindex=0;

ints=strlen(a.str);

while(index<s&&a[index]==b[index])

{

++index;

}

if(index<s)

{

yes=false;

}

}

溫馨提示

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

評論

0/150

提交評論