- * 程序的版权和版本声明部分
- * Copyright (c) 2011, 烟台大学计算机学院学生
- * All rights reserved.
- * 文件名称:
- * 作者:李君凯
- * 完成日期: 2012年 5月 8日
- * 版本号:
- *对任务及求解方法的描述部分
17.#include <iostream>
18.#include <conio.h>
19.#include <windows.h>
20.
21.using namespace std;
22.
23.enum vehicleStaus {rest, running}; //车辆状态:泊车、行进
24.
25.class vehicle //车辆类
26.{
27.protected:
28. int maxSpeed; //最大车速
29. int currentSpeed; //当前速度
30. int weight; //车重
31. vehicleStaus status; //rest-泊车状态;running-行进状态
32.public:
33. vehicle(int maxS, int w); //构造函数,初始时,当前速度总为0且处在停车状态
34. void start(); //由rest状态到running, 初速为1
35. void stop(); //由running状态到rest, 当前速度小于5时,才允许停车
36. void speed_up(); //加速,调用1次,速度加1
37. void slow_down(); //减速,调用1次,速度减1,速度为0时,停车
38.};
39.vehicle::vehicle(int maxS, int w)
40.{
41. maxSpeed = maxS, weight = w;
42. currentSpeed = 0, status = rest;
43.}
44.void vehicle::start()
45.{
46. if (status == running)
47. {
48. cout << "车辆已经行驶......" << endl;
49. return;
50. }
51. currentSpeed = 1, status = running;
52.}
53.void vehicle::stop()
54.{
55. if (currentSpeed >= 5)
56. {
57. cout << "速度太快... ...请减速后停车" << endl;
58. return;
59. }
60. currentSpeed = 0, status = rest;
61.}
62.void vehicle::speed_up()
63.{
64. if (currentSpeed >= maxSpeed)
65. {
66. cout << "请不要超速行驶......" << endl;
67. return;
68. }
69. if (status == rest)
70. {
71. cout << "车辆未启动!" << endl;
72. return;
73. }
74. currentSpeed++;
75.}
76.void vehicle::slow_down()
77.{
78. if (currentSpeed <= 0)
79. {
80. cout << "车辆未启动!" << endl;
81. return;
82. }
83. currentSpeed--;
84. if (currentSpeed == 0)
85. status = rest;
86.}
87.
88.class bicycle : virtual public vehicle//(1)自行车类的虚基类为车辆类
89.{
90.protected:
91. double height; //车高
92.public:
93. bicycle(int maxS=10, int w=50, int h=0.7); //定义构造函数
94.};
95.bicycle::bicycle(int maxS, int w, int h):vehicle(maxS, w)
96.{
97. height = h;
98.}
99.
100.class motorcar : virtual public vehicle//(2)机动车类的虚基类也为车辆类
101.{
102.protected:
103. int seatNum; //座位数
104. int passengerNum; //乘客人数
105.public:
106. motorcar(int maxS=150, int w=1500, int s=5, int p=1); //定义构造函数
107. void addPassenger(int p=1); //搭载乘客,超员要拒载,有人下车时,p为负数。当然车上乘客至少有1个(司机)。上下车时要保证安全……
108.};
109.motorcar::motorcar(int maxS, int w, int s, int p):vehicle(maxS, w)
110.{
111. seatNum = s, passengerNum = p;
112.}
113.void motorcar::addPassenger(int p)
114.{
115. if (p > 0)
116. {
117. if (status == running)
118. {
119. cout << "车辆正在行驶,停车后再上下车!" << endl;
120. return;
121. }
122. if (passengerNum >= seatNum)
123. {
124. cout << "涉嫌超员,已清理后达到满员!" << endl;
125. return;
126. }
127. passengerNum++;
128. }
129. else
130. {
131. if (passengerNum <= 1)
132. {
133. cout << "请司机不要离开岗位!" << endl;
134. return;
135. }
136. passengerNum--;
137. }
138.}
139.
140.class motorcycle: public bicycle, public motorcar//(3)摩托车类的基类为自行车类和机动车类
141.{
142.public:
143. motorcycle(int maxS=90, int w=100, int s=3, int p=1, int h=0.7); //定义构造函数
144. void show(); //显示摩托车的运行状态
145.};
146.motorcycle::motorcycle(int maxS, int w, int s, int p, int h):bicycle(maxS, w, h), motorcar(maxS, w, s, p), vehicle(maxS, w)
147.{}
148.void motorcycle::show()
149.{
150. cout << "状态:" << (status == rest? "泊车。":"行进。") <<'\t'<< "车速:" <<currentSpeed<<"/"<<maxSpeed <<'\t'<< "当前成员:" <<passengerNum<<"/"<<seatNum << endl;
151.}
152.
153.int main( )
154.{
155. motorcycle m;
156. bool end=false;
157. while (!end){
158. cout<<"请操作:1-启动 2-加速 3-减速 4-有人上车 5-有人下车 6-停车 0-结束"<<endl;
159. char keydown= _getch(); //_getch()返回键盘上读取的字符,应包含头文件<conio.h>
160. switch(keydown)
161. {
162. case '1':
163. cout<<"操作(启动)\t"; m.start(); break;
164. case '2':
165. cout<<"操作(加速)\t"; m.speed_up(); break;
166. case '3':
167. cout<<"操作(减速)\t"; m.slow_down(); break;
168. case '4':
169. cout<<"操作(有人上车)\t"; m.addPassenger(); break;
170. case '5':
171. cout<<"操作(有人下车)\t"; m.addPassenger(-1); break;
172. case '6':
173. cout<<"操作(停车)\t"; m.stop(); break;
174. case '0':
175. end=true; break;
176. }
177. m.show();
178. cout<<endl;
179. Sleep(200); //要包含头文件<windows.h>
180. }
181. system("pause");
182. return 0;
183.}