下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、C+项目:车辆管理系统目标:写一个小程序,要求到用标准模板库中的list容器和迭代器,可以使用到 多继承来进行实验。1.1 General(概述)1. 在写代码之前要仔细阅读作业要求2. 要求对不正确的输入做检查3. 在你写完一个类之后,要紧接着写这个类的测试函数,因此,当你调试你写的 代码的时候,你能够很容易的对你的代码做重复检查。这样才能够保证你当前写的代码能够准确无误的执行。可能出于某些原因你要对你写过的代码作一此些修改,这 样你重新测试你修改的代码就相对比较容易。4. 添加完成你的任务所需要的函数。1.2 In troductio n(简介):渥太华这个城市正在创建一个有关交通工具的“
2、数据库”,来为它的议会(委员会) 做预算提供较好的参考。这个城市有许多不同种类的机动车辆:客车,货车,卡车, 紧急车辆(救护,消防等),在对这个城市了解之后,要求你设计一个有下图要求 的层次的系统。2 Implement the following functions for each class:为每个类实现以下函数2.1 (Vehicle 类) (属性:所有的属性要求为私有的 )char licensePlate 属性:车的车牌 (执照)作为它的 id, 一个车牌最多可由 8组 成。char *type 属性:车辆类型 (例如:汽车,卡车,消防车 )char *make 属性:车辆的制造商
3、double gasTankSize 属性:总油量double fuelConsumption 属性:单位路程耗油量函数:R1. 构造函数 vehicle (char *type, char* licensePlate, double gasTankSize, double fuelConsumption)Tppe 的默认值为: carlicensePlate 的默认值为 : ottawa01gasTankSize 的默认值为 : 100fuelConsumption 的默认值为 : 10 所有其他不在构造函数中的参数要求置0或置空R2. void setMake (char *make);设
4、置 制造商 的值R3. setFuelData (double gasTankSize, double fuelConsumption)设置燃料的有关信息 (总油量,单位路程耗油量 )R4. setType (char *type)-设置车辆类型R5. setLice nsePlate(char *lice nse)-设置车辆的车牌R6. virtual printSpecifications() - 打印车辆的具体信息 ,例如:制造商、类型, 车牌,燃油量和油箱容积R7. virtual double computeTravelDistance()-这个函数计算车辆可能行使的距离,计算公式为
5、: gasTankSize * fuelConsumption( 总油量 *单位路程耗油量 )2.2Class LoadVehicle:( LoadVehicleAttributes( 属性 )int nu mberOfWheelscontains the nu mber of wheels that the vehicle numberOfWheels 属性:车辆的车轮个数double loadCapacitythe load weight that the vehicle can carry (in kg)double loadCapacity :车辆的负荷double t
6、owingCapacity 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 numberO
7、fWheels, loadCapacity 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
8、 the default values of the base class(es), or, if no default values exist, to 0 or NULL.这个类的构造函数,各个属性的默认值: loadCapacity 的默认值: 6, 500kg towingCapacity 的默认值: 5000kg gasTankSize 的默认值: 200fuelConsumption 的默认值: 6其他没有初始化的成员变量应该设置为基类的默认值一致, 如果没有默认值, 则 应置0或置空R9. setLoadData (double towingCapacity, double lo
9、adCapacity)设置负荷 ,拖曳量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 t
10、hetravel distance 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个车轮,每增加两个车轮,车辆
11、可行使的车程以 5%减少.例如:总油量为 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 属性:车辆可搭乘的乘客的数量Fun
12、ctions( 函数 )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
13、required using default values of base classes. All other variables that are not initialized are set to 0 or NULL. 该类的构造函数,成员变量的默认值: numPassengers 的默认值: 5 gasTankSize 的默认值: 100 fuelConsumption 的默认值: 11 要用基类的构造函数对要求有默认值的属性进行初始化, 没其他没有初始化的成 员要置0或置空R13. setNumPassengers (int numPassengers) 设置可搭乘的乘客人数R14
14、. printSpecifications() -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 = 950k
15、m.2.4 Class emergencyVehicle(emergencyVehicleAttributes( 属性 )int numWorkers 属性:为每辆车分配的工作人员char *driver属性:为车辆分配的驾驶员char *station 属性:车辆停靠的车站Fun cti onsR16. emerge ncyVehicle (char *type, char*driver, char *stati on, intnu mPasse ngers,double gasTa nkSize, double fuelC on sumpti on)-构造函数,部分属性平的默认值:forn
16、umPassengers 的默认值:5gasTa nkSize 的默认值:100fuelConsumption 的默认值:11 wheels的默认值:6其他没有给初始值的属性要求置0或置空.R17. setAssignment (char *driver, char *station)设置分配(司机,停靠站)R18. printSpecifications()丁印的车辆(车辆规范规格从车辆类,该loadvehciie,客运车辆和emergencyequipment类)。打印车辆的具体信息R19. computeTravelDistance() -行程是 loadVehicle 和 passen
17、gerVehicle 中较小的行程2.5 Class EmgergencyEquipmen(紧急设备类 )Attributes (属性)int sirenNoiseDistanee-汽笛能够被听到的距离,默认为500mint numBeds-可以同时供给病人的床位,默认值为 2Fun cti onsR20. emerge ncyEquipme nt(int sire nN oiseDista nee, i nt nu mBeds)勾造函数给汽笛的可听见距离,床位初始化R21. getBedsNum() -返回床位数R22. printSpecifications()-打印汽笛的可听见距离,床位
18、数2.6 Container class(容器类)要求使用标准模板库中的list容器,list容器要求可以存储指向对象的指针,这样所有不同类型的车辆可以在同一个容器中(iterator-迭代器的使用)2.7 Decisi on class( 决策类)使用相应的迭代器对容器遍历,使用dyn amic casti ng(运行时类型信息)进行不同类之间的类型转型(如将指向不同派生类对象的基类指针转为派生类对象指针),可以对相应不同类的成员函数的调用,这个类由城市的议会用来做决定Attributes(属性)listvVehicle*>& vehicleList :各类车辆的指针Fun c
19、ti ons(方法)R23. decisio n(list<Vehicle*> &)-构造函数:接收存储各类车辆信息的容器R24. prin tVehiclesSpecificatio ns-打印所有车辆的具体信息R25. prin tEmerge ncyVehicles()-单独打印紧急车辆的紧急数据信息R26. i nt numberL on gDista nceEmerge ncyVehicles() 丁印不用中途加油能够行使800km以上的紧急车辆的数量R30. int numBeds()-确定以防紧急事件这个城市能够调遣的移动床位的数量R27. i nt numP
20、asse ngers()-确定以防紧急事件这个城市可转移的乘客人数.2.8 Testing(测试)主函数要求能够获得供议会做决定的计算结果int main()list<Vehicle*> vehicleList;LoadVehicle* lv1 = new LoadVehicle( );vehicleList.push_back(lv1);Passe ngerVehicle* pv1 = new Passe ngeVehicle();vehicleList.push_back(pv1);Emerge ncyVehicle* ev1 = new Emerge ncyVehicle()
21、;vehicleList.push_back(ev1);Decisi on decisi on( vehicleList);decisio n.pri ntVehiclesSpecificatio ns();decisio n.pri ntEmerge ncyVehicles();decisio n.nu mberL on gDista nceEmerge ncyVehicles();decisi on.nu mBeds();decisio n.nu mPasse ngers();delete lv1;delete pv1;delete ev1;return 0;11. 写一个学生类,从per
22、son类继承,增加的属性为成绩f和评语label (字符串)。person:n ame,age,pri nt() #in clude<iostream.h>class 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<<"年龄:"<<a
23、ge;pers on() cout<<"Pers on destructor"<<e ndl;class stude nt:public pers onprivate:public:double f;char *label;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<&
24、lt;" 评语:"<<label<<endl;stude nt()cout<<"Stue nt destructor"<<e ndl;void mai n() 1pers on a1;stude nt d1('张三",16,89.5,"优秀");a1=d1;a1.pri nt(); cout<<e ndl;d1.pers on:pri nt(); cout<<e ndl; d1.pri nt();姓名张三年龄16姓名张三年龄16姓名张三年龄16成
25、绩:89.5评语:优秀Stue nt destructorStude nt(char *n="",i nt a=0,char *c=erson(n, a),label(c)Pers on destructorPress any key to con ti nueexercise1512. Person 为基类:虚函数为 dailywork() Person() 定义三个子类:Stude nt Doctor(char* label) Driver(char* label)主函数:定义基类指针数组,动态创建子类对象,调用成员函数,删除创建的对象。#in clude<ios
26、tream.h> class Person public:Pers 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() cout<<"erson destructor"<<e ndl;private:cha
27、r *n ame; int age;class Stude nt:public Person public:void dailywork()Perso n:dailywork();cout<<"学生"cout<<"评语:"<<label<<endl;Stude nt()cout<<"Stue nt destructor"<<e ndl;private:char *label;class Doctor:public Personpublic:3 erson(n, a
28、),label(c)Doctor(char *n="",i nt a=O,char *c="")void dailywork()Pers on:dailywork();cout<<"医生"cout<<"评语:"<<label<<endl;Doctor()cout<<"Doctor destructor"<<e ndl;private:char* label;class Driver:public Pers onpublic:
29、Driver(char *n="",int a=0,char *c="")erson(n,a),label(c)void dailywork()Pers on:dailywork();cout<<"司机"cout<<"评语:"<<label<<endl;Driver()cout<<"Driver destructor"<<e ndl;private:char* label;void mai n()Student *s=new
30、 Student(”张三",15,"好");Doctor *dnew Doctor(" 李四",45,"认真");Driver *d2=new Driver("王五",35,"优秀");Pers on *array3;array0=s;array1=d1;array2=d2;for(i nt i=0;i<3;i+)arrayi_>dailywork();delete array0;delete array1;delete array3;姓名:张三 年龄: 15 学生 评语:
31、好姓名:李四年龄45 医生 评语:认真姓名:王五年龄: 35 司机 评语:优秀Stuent destructorPerson destructorDoctor destructorPerson destructorDriver destructorPerson destructorPress any key to continue exercise1613. 修改类 Figure ,将 Point 对象指针作为属性组合进来, 对 Figure 的子类也做相应的修改。#include<iostream.h>class Pointprivate:double x,y;public:Po
32、int(double i,double j):x(i),y(j)void print() const cout<<"("<<x<<","<<y<<")"Point()cout<<"Point析构 "<<endl;class Figureprivate:Point *str;Point center;public:Figure(double i=0,double j=0):center(i,j)Point& location(
33、)str=¢er;return *str;void move(Point p)str=¢er;str=&p;draw();virtual void draw() =0;virtual void rotate(double)=0;virtual Figure()cout<<"Figure 析构 "<<endl;class Circle:public Figureprivate:double radius;public:Circle(double i=0,double j=0,double r=0):Figure(
34、i,j),radius(r) void draw()cout<<"a circle with center" location().print();cout<<"and radius"<<radius<<endl;void rotate(double)cout<<"no effect"<<endl; Circle()cout<<"Circle 析构 "<<endl;class Square:public Figurepr
35、ivate: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()cout<<"a square with center" location().print();cout<<"side length"<<side<<endl<<"The angle between one side and the X-axis
36、is"<<angle<<endl;void rotate(double a)angle+=a; cout<<"The angle between one side and the X-axis is"<<angle<<endl;void vertices()cout<<"The vertices od the square are:"<<endl;Square()cout<<"Square 析构 "<<endl; ;v
37、oid main()Circle c(1,2,3);Square s(4,5,6);Figure* f=&c;Figure& g=s; f->draw(); f->move(Point(2,2); g.draw(); g.rotate(1); s.vertices();运行结果:a circle with center(1,2)and radius3a circle with center(1,2)and radius3Point 析构Point 析构a square with center(4,5)side length6The angle between one
38、 side and the X-axis is0The angle between one side and the X-axis is1The vertices od the square are:Square 析构Figure 析构Point 析构Circle 析构Figure 析构Point 析构Press any key to continue/* 在 Complex 类中实现:重载 = , !=*/ answer :#include<iostream.h>class Complexpublic:Complex(int i=0)x=i;friend int operator
39、 =(Complex c1,Complex c2);friend int operator !=(Complex c1,Complex c2); void display();private:int x;void Complex:display()cout<<x;int operator =(Complex c1,Complex c2)if(c1.x=c2.x)return 1;elsereturn 0;int operator !=(Complex c1,Complex c2)if(c1.x!=c2.x)return 1;elsereturn 0;void main()Compl
40、ex c1(6),c2(8);if(c1=c2)c1.display();cout<<"is equal to"c2.display(); cout<<endl;if(c1!=c2)c1.display();cout<<"isn't equal to"c2.display(); cout<<endl;run result :6isn't equal to8本帖最后由 陶亮 于 2013-11-30 16:31 编辑/*1. 实现一个字符串类 String ,功能包括:)复制赋值运算符1、 B
41、ig Three: 析构函数( Destructor )复制构造函数( copy constructor ( copy assignment operator )2、下标操作符重载 : 只能作为类的成员函数,不能作为类的友元函数。3、输入输出操作符重载4、= 操作符重载5、+ 操作符重载 参考 Vector 类*/ answer :#include<iostream.h>#include<string.h> class Stringpublic:String()str=NULL;String(int m):len(m)str=new charlen;String(cha
42、r *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& operator<<(ostream& os,String& c); friend int operator =(String &c1,String& c2);String() if(s
43、tr!=NULL) delete str;char& operator(int n)static char ch=0; if(n>len-1)cout<<" 整数下标越界 "<<endl; return ch;elsereturn *(str+n);void Disp() cout<<str<<endl;void print() cout<<len;private:int len;char *str;String& String: operator+(String& c)len +=c
44、.len;return *this;istream& operator>>(istream& is,String& c) is>>c.len; return is;ostream& operator<<(ostream& os,String& c) c.print();return os;int operator =(String &c1,String &c2)if(c1.len=c2.len)return 1;elsereturn 0;void main()String word("
45、this is a c+ program."); word.Disp();cout<<" 位置 0: "<<word0<<endl; cout<<" 位置 15: "<<word15<<endl; cout<<" 位置 25: "<<word25<<endl;word0='t'word.Disp();int f =10;String word2(f);for(int i=0;i<10;i+)wor
46、d2=word;word2.Disp();String s1(8),s2(5);cout<<" 输入两个整数值 "cin>>s1;cin>>s2;cout<<" 你输入的两个值分别为: " cout<<s1<<endl;cout<<s2<<endl;if(s1=s2)s1.print();cout<<"is equal to"s2.pri nt(); cout<<e ndl;(s1+s2).pri nt(); run
47、 result :this is a c+ program.位置0 : t位置15 : r整数下标越界位置25 :this is a c+ program.this is a输入两个整数值22你输入的两个值分别为:222is equal to24Press any key to con ti nue1. 在Complex类中实现:重载-=,*= , /=操作符。 answer :#in clude<iostream.h>class Complexpublic:Complex()Complex(double r ,double i)real = r; image = i;Comple
48、x operator -=(Complex & t);Complex operator *=(Complex & t);Complex operator /=(Complex & t);Prin t();private:double real;double image;;Complex Complex: operator -=(Complex& t) real-=t.real;image-=t.image;return *this;Complex Complex: operator *=(Complex & t) real*=t.real;image*=
49、t.image;return *this;Complex Complex: operator /=(Complex& t)real/=t.real;image/=t.image;return *this;Complex: Print()if(image<0)cout<<real<<image<<"i"<<e ndl;cout<<real<<"+"<<image<<"i"<<e ndl;cout<<e n
50、dl;void mai n()Complex c1(10.0,20.0),c2(5.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+18i1.02+0.28125iPress any key to con ti nue 8.实现一个字符串类 String,功能包括:1。Big Three2。下标操作符重载3。输入输出操作符重载4。=操作符重载 5。+操作符重载参考Vector类#in clude<iostream.h>#in clude
51、<stri ng.h>class MyStri ngpublic:MyStri ng(char *s)str=new charstrle n( s)+1; strcpy(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<<"整数下标越界"return ch; elsereturn *(str+ n);
52、friend ostrea m& operator<< (ostream& out,MyStri ng& a); friend istrea m& operator(istrea m& in,M yStri ng& x); MyStri ng operator+ (MyStri ng& x);friend bool operator=(MyStri ng& a,MyStri ng& b); private:in t le n;|char *str;istream& operator>> (i
53、strea m& in,M yStri ng& x)in> >x.str;return in;ostrea m& operator<< (ostream& out,MyStri ng& a)MyString MyString:"二丿 perator+ (MyString& x)int len 1=strle n( x.str);len=len+le n1;char *str1;str1= new charle n+1;strcpy(str1,this->str);return strcat(str1,x.str);delete str1;*str 仁NULL;bool operator=(MyStri ng& a,MyStri ng& b) bool yes=true;if(strle n(a.str)!=strle n(b.str)yes=false;else I int in dex=0; int s=strle n( a.str);while(i ndex<s&&ai ndex=bi ndex)retur
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 审计部门绩效制度汇编
- 山东省莒县2025-2026学年第二学期初三年级数学试题月考试卷含解析
- 江西省吉安市名校2026年初三第二学期第二次综合练习化学试题理试卷含解析
- 审计风险内控制度
- 2025-2030中国医疗废弃物处理技术升级与区域化处置中心建设报告
- 家装公司绩效考核制度
- 陕西省宝鸡岐山县联考2025-2026学年初三下学期教学质量检查(4月)物理试题试卷含解析
- 浙江省嘉兴市秀洲区、经开区七校联考2026年初三二诊模拟物理试题试卷含解析
- 江苏省盐城市景山中学2026年初三检测试题(二)(4月)数学试题试卷含解析
- 山西省运城市永济市重点名校2025-2026学年初三中考冲刺模拟考试(一)物理试题含解析
- 睾丸扭转超声诊断
- GB/T 16405-2025声学管道消声器无气流下插入损失测量实验室简易法
- QGDW11451-2015架空输电线路标识及安装规范
- 征兵心理测试题目及答案
- ASTM G154-23译本 中文版【后附英文官方原版 可复制可检索】
- 加装电梯可行性研究报告范文
- 十堰市茅箭区教育局所属学校招聘考试真题2024
- 《高速铁路客运组织(第3版)》课件 项目二 任务1 旅客进站组织
- 种养结合生态循环农业项目可行性研究报告
- 保密警示教育典型泄密案例教育学习
- 8.2重力 力的示意图(原卷版)-八年级物理下册同步-课堂(苏科版)
评论
0/150
提交评论