




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
年4月19日车辆管理系统文档仅供参考,不当之处,请联系改正。C++项目:车辆管理系统目标:写一个小程序,要求到用标准模板库中的list容器和迭代器,能够使用到多继承来进行实验。1.1General(概述)1.在写代码之前要仔细阅读作业要求2.要求对不正确的输入做检查3.在你写完一个类之后,要紧接着写这个类的测试函数,因此,当你调试你写的代码的时候,你能够很容易的对你的代码做重复检查。这样才能够保证你当前写的代码能够准确无误的执行。可能出于某些原因你要对你写过的代码作一此些修改,这样你重新测试你修改的代码就相对比较容易。4.添加完成你的任务所需要的函数。1.2Introduction(简介):渥太华这个城市正在创立一个有关交通工具的“数据库”,来为它的议会(委员会)做预算提供较好的参考。这个城市有许多不同种类的机动车辆:客车,货车,卡车,紧急车辆(救护,消防等),在对这个城市了解之后,要求你设计一个有下图要求的层次的系统。
2Implementthefollowingfunctionsforeachclass:为每个类实现以下函数2.1(Vehicle类)(属性:所有的属性要求为私有的)charlicensePlate[]属性:车的车牌(执照)作为它的id,一个车牌最多可由8组成。char*type属性:车辆类型(例如:汽车,卡车,消防车)char*make属性:车辆的制造商doublegasTankSize属性:总油量doublefuelConsumption属性:单位路程耗油量函数:R1.构造函数vehicle(char*type,char*licensePlate,doublegasTankSize,doublefuelConsumption)Tppe的默认值为:carlicensePlate的默认值为:ottawa01gasTankSize的默认值为:100fuelConsumption的默认值为:10所有其它不在构造函数中的参数要求置0或置空R2.voidsetMake(char*make);设置制造商的值R3.setFuelData(doublegasTankSize,doublefuelConsumption)–设置燃料的有关信息(总油量,单位路程耗油量)R4.setType(char*type)–设置车辆类型R5.setLicensePlate(char*license)–设置车辆的车牌R6.virtualprintSpecifications()-打印车辆的具体信息,例如:制造商、类型,车牌,燃油量和油箱容积R7.virtualdoublecomputeTravelDistance()–这个函数计算车辆可能行使的距离,计算公式为:gasTankSize*fuelConsumption(总油量*单位路程耗油量)2.2ClassLoadVehicle:(LoadVehicle类)Attributes(属性)intnumberOfWheels–numberOfWheels属性:车辆的车轮个数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.这个类的构造函数,各个属性的默认值: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属性:车辆可搭乘的乘客的数量Functions(函数)R12.passengerVehicle(char*type,intnumPassengers,doublegasTankSize,doublefuelConsumption)–ThedefaultvaluesfornumPassengersis5.ThedefaultvaluesforgasTankSizeandfuelConsumptionare100and11respectively.Theconstructorwillinitializeallthefieldsoftheclassasrequiredusingdefaultvaluesofbaseclasses.Allothervariablesthatarenotinitializedaresetto0orNULL.该类的构造函数,成员变量的默认值:numPassengers的默认值:5gasTankSize的默认值:100fuelConsumption的默认值:11要用基类的构造函数对要求有默认值的属性进行初始化,没其它没有初始化的成员要置0或置空R13.setNumPassengers(intnumPassengers)设置可搭乘的乘客人数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)–构造函数,部分属性平的默认值:fornumPassengers的默认值:5gasTankSize的默认值:100fuelConsumption的默认值:11wheels的默认值:6其它没有给初始值的属性要求置0或置空.R17.setAssignment(char*driver,char*station)设置分配(司机,停靠站)R18.printSpecifications()–打印的车辆(车辆规范规格从车辆类,该loadvehcile,客运车辆和emergencyequipment类)。打印车辆的具体信息R19.computeTravelDistance()–行程是loadVehicle和passengerVehicle中较小的行程2.5ClassEmgergencyEquipmen(紧急设备类)Attributes(属性)intsirenNoiseDistance–汽笛能够被听到的距离,默认为500mintnumBeds–能够同时供给病人的床位,默认值为2FunctionsR20.emergencyEquipment(intsirenNoiseDistance,intnumBeds)–构造函数给汽笛的可听见距离,床位初始化R21.getBedsNum()–返回床位数R22.printSpecifications()–打印汽笛的可听见距离,床位数2.6Containerclass(容器类)要求使用标准模板库中的list容器,list容器要求能够存储指向对象的指针,这样所有不同类型的车辆能够在同一个容器中(iterator-迭代器的使用)2.7Decisionclass(决策类)使用相应的迭代器对容器遍历,使用dynamiccasting(运行时类型信息)进行不同类之间的类型转型(如将指向不同派生类对象的基类指针转为派生类对象指针),能够对相应不同类的成员函数的调用,这个类由城市的议会用来做决定Attributes(属性)list<Vehicle*>&vehicleList:各类车辆的指针Functions(方法)R23.decision(list<Vehicle*>&)–构造函数:接收存储各类车辆信息的容器R24.printVehiclesSpecifications-打印所有车辆的具体信息R25.printEmergencyVehicles()-单独打印紧急车辆的紧急数据信息R26.intnumberLongDistanceEmergencyVehicles()–打印不用中途加油能够行使800km以上的紧急车辆的数量R30.intnumBeds()-确定以防紧急事件这个城市能够调遣的移动床位的数量R27.intnumPassengers()–确定以防紧急事件这个城市可转移的乘客人数.2.8Testing(测试)主函数要求能够获得供议会做决定的计算结果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,"优秀");
a1=d1;
a1.print();
cout<<endl;
d1.person::print();
cout<<endl;
d1.print();
}
运行结果:
姓名:张三
年龄:16
姓名:张三
年龄:16
姓名:张三
年龄:16
成绩:89.5
评语:优秀
Stuentdestructor
Persondestructor
Pressanykeytocontinueexercise15
12.Person为基类:虚函数为dailywork()~Person()
定义三个子类:StudentDoctor(char*label)Driver(char*label)
主函数:定义基类指针数组,动态创立子类对象,调用成员函数,删除创立的对象。
#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<<"
医生";
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,"优秀");
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];
}
运行结果:
姓名:张三
年龄:15
学生
评语:好
姓名:李四
年龄:45
医生
评语:认真
姓名:王五
年龄:35
司机
评语:优秀
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析构"<<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析构"<<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析构"<<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析构"<<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();
}
运行结果:
acirclewithcenter(1,2)andradius3
acirclewithcenter(1,2)andradius3
Point析构
Point析构
asquarewithcenter(4,5)sidelength6
TheanglebetweenonesideandtheX-axisis0
TheanglebetweenonesideandtheX-axisis1
Theverticesodthesquareare:
Square析构
Figure析构
Point析构
Circle析构
Figure析构
Point析构
Pressanykeytocontinue/*在Complex类中实现:重载==,!=*/
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.实现一个字符串类String,功能包括:
1、BigThree:析构函数(Destructor)复制构造函数(copyconstructor)复制赋值运算符(copyassignmentoperator)
2、下标操作符重载
:只能作为类的成员函数,不能作为类的友元函数。
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<<"整数下标越界"<<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<<"输入两个整数值";
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
整数下标越界
位置25:
thisisac++program.
thisisa
输入两个整数值2
2
你输入的两个值分别为:2
2
2isequalto2
4Pressanykeytocontinue
1.在Complex类中实现:重载-=,*=,/=操作符。
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.实现一个字符串类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<<"整数下标越界";
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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 施工现场劳务服务协议
- 教师规范教学培训
- 拍卖现场准备协议
- 义工活动保证金合同
- 2025年统编版小学道德与法治二年级下册《清新空气是个宝》说课课件
- 摄影器材交易合同
- 外包环境监测合同
- 劳动合同解约的法律条款
- 房屋交割时房贷状态协议
- 客运座位预订协议
- (新版)首席质量官认证考试复习题库-上(单选题汇总)
- 建筑施工中小型施工机具验收记录表
- 4.3 TIA博途软件的调试
- 新时代背景下妇产科课程思政的构建与探索
- 混凝土拌合站计量系统自检校准记录
- 患者发生呛咳应急预案
- 教科版一年级下册《动物》单元思维导图
- 医院院内科研项目管理办法
- 面瘫中医临床路径完整版
- GB/T 37546-2019无人值守变电站监控系统技术规范
- GA 61-2010固定灭火系统驱动、控制装置通用技术条件
评论
0/150
提交评论