本文纯属个人见解,是对前面学习的总结,如有描述不正确的地方还请高手指正~
/* *【项目3】在下面一段类的定义中,自行车类的虚基类为车辆类,机动车类的虚基类也为车辆类,摩托车类的基类为自行车类和机动车类,类之间均为公有继承。 * 程序的版权和版本声明部分 * Copyright (c)2012, 烟台大学计算机学院学生 * All rightsreserved. * 文件名称: object.cpp * 攻城菜鸟:蛋蛋 * 完成日期:2013年 5 月 25 日 * 版本号: Code Blocks10.05 * 输入描述:车行驶记录数据 * 问题描述:要求: (1)根据下面各类间关系的描述,补全下面程序段中空白的代码 (2)实现程序中声明的成员函数,注意响应操纵中的动作产生的条件不能满意时应给出提示。 (3)运行程序,享受开摩托的进程。(请下载资源中一个可执行文件,可以先运行再编程。不必请求驾照,这个摩托车很安全。) (4)在报告中回答问题:本题中使用虚基类的好处是什么? * 程序输出:车的行驶日记 */ #include <iostream> #include<conio.h> #include <windows.h> using namespace std; enum vehicleStaus {rest, running}; //车辆状态:泊车、前进 class vehicle //车辆类 { protected: int maxSpeed; //最大车速 int currentSpeed; //当前速度 int weight; //车重 vehicleStaus status; //rest-泊车状态;running-前进状态 public: vehicle(int maxS, int w):maxSpeed(maxS),weight(w),currentSpeed(0),status(rest){} //构造函数,初始时,当前速度总为0且处在泊车状态 void start(){if(currentSpeed==0){status=running;currentSpeed=1;}} //由rest状态到running, 初速为1 void stop(){if(currentSpeed<5){currentSpeed=0;status=rest;}else cout<<"车速大,不答应泊车"<<endl;} //由running状态到rest, 当前速度小于5时,才答应泊车 void speed_up(){if(status==running&¤tSpeed>=0)currentSpeed+=1;else cout<<"wrong operation"<<endl;} //减速,调用1次,速度加1 void slow_down(){if(status==running&¤tSpeed>=0)currentSpeed-=1;else cout<<"wrong operation"<<endl;} //减速,调用1次,速度减1,速度为0时,泊车 }; class bicycle :virtual public vehicle//(1)自行车类的虚基类为车辆类 { protected: double height; //车高 public: bicycle(int maxS=10, int w=50, int h=0.7):vehicle(maxS,w),height(h){} //定义构造函数 }; class motorcar : virtual public vehicle//(2)机动车类的虚基类也为车辆类 { protected: int seatNum; //座位数 int passengerNum; //搭客人数 public: motorcar(int maxS=150, int w=1500, int s=5, int p=1):vehicle(maxS,w),seatNum(s),passengerNum(p){} //定义构造函数 void addPassenger(int p=1){if(currentSpeed==0&&p+passengerNum<=seatNum&&p+passengerNum>=1) passengerNum+=p;else if(currentSpeed>=5)cout<<"车速过大,没法泊车 "<<endl;else cout<<"车已满员或司机师傅,您不能下车"<<endl;} //增加搭载的搭客,超员要拒载,有人下车时,p为正数。当然车上搭客至少有1个(司机)。只有车停稳后才能上下客。 }; class motorcycle: public bicycle,public motorcar //(3)摩托车类的基类为自行车类和机动车类 { public: //定义构造函数 motorcycle(int maxS=90, int w=100, int s=3, int p=1, int h=0.7):vehicle(maxS,w),bicycle(maxS,w,h),motorcar(maxS,w,s,p){} void show(){if(status==rest)cout<<"泊车"<<endl;else cout<<"行驶"<<endl; cout<<"当前时速"<<currentSpeed<<endl; cout<<"最大时速"<<maxSpeed<<endl; cout<<"搭客人数"<<passengerNum<<endl; cout<<"座位数"<<seatNum<<endl; }//表现摩托车的运行状态 }; int main( ) { motorcycle m; bool end=false; while (!end){ cout<<"请操纵:1-启动 2-减速 3-减速 4-有人上车 5-有人下车 6-泊车 0-结束"<<endl; char keydown= _getch(); //_getch()返回键盘上读取的字符 switch(keydown) { case '1': cout<<"操纵(启动)\t"; m.start(); break; case '2': cout<<"操纵(减速)\t"; m.speed_up(); break; case '3': cout<<"操纵(减速)\t"; m.slow_down(); break; case '4': cout<<"操纵(有人上车)\t"; m.addPassenger(); break; case '5': cout<<"操纵(有人下车)\t"; m.addPassenger(-1); break; case '6': cout<<"操纵(泊车)\t"; m.stop(); break; case '0': end=true; break; } m.show(); cout<<endl; Sleep(200); //要包含头文件<windows.h> } return 0; }
文章结束给大家分享下程序员的一些笑话语录: 问答
Q:你是怎么区分一个内向的程序员和一个外向的程序员的? A:外向的程序员会看着你的鞋和你说话时。
Q:为什么程序员不能区分万圣节和圣诞节? A:这是因为 Oct 31 == Dec 25!(八进制的 31==十进制的 25)
--------------------------------- 原创文章 By
类和虚基类
---------------------------------