第十二周上机项目2摩托车继承自行车和机动车

2023-10-20 01:20

本文主要是介绍第十二周上机项目2摩托车继承自行车和机动车,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

/*
*Copyright (c) 2015, 烟台大学计算机学院
*All rights reserved.
*文件名称:text.cpp
*作者:陈栋梁
*完成日期:2015年 5月 20日
*版本号:v1.0
*
*/
#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; 
public:
Vehicle(int maxS, int w); 
void start();  
void stop(); 
void speed_up(); 
void slow_down(); 
};
Vehicle::Vehicle(int maxS,int w)
{
maxSpeed=maxS;
currentSpeed=0;
weight=w;
status=rest;
}
void Vehicle::start()
{
if (status==rest)
{
status=running;
currentSpeed=1;
}
else
cout<<"车辆已经行驶!"<<endl;
}
void Vehicle::stop()
{
if (status==running)
if(currentSpeed<5)
{
status=rest;
currentSpeed=0;
}
else
cout<<"车速太快!先减速再停车……"<<endl;
else
cout<<"车辆未启动!"<<endl;
}
void Vehicle::speed_up()
{
if (status==running)
if(currentSpeed<maxSpeed)
++currentSpeed;
else
cout<<"请不要超速行驶……"<<endl;
else
cout<<"车辆未启动!"<<endl;
}
void Vehicle::slow_down()
{
if (status==running)
{
if(currentSpeed>0)
--currentSpeed;
}
else
cout<<"车辆未启动!"<<endl;
if(currentSpeed==0)
status=rest;
}
class Bicycle :virtual Vehicle
{
protected:
double height;
public:
Bicycle(int maxS=10, int w=50, int h=0.7);  
};
Bicycle::Bicycle(int maxS, int w, int h):Vehicle(maxS, w),height(h) {}
class Motorcar : virtual Vehicle
{
protected:
int seatNum; 
int passengerNum; 
public:
Motorcar(int maxS=150, int w=1500, int s=5, int p=1);   
void addPassenger(int p=1);  
};
Motorcar::Motorcar(int maxS, int w, int s, int p): Vehicle(maxS, w),seatNum(s),passengerNum(p) {}
void Motorcar::addPassenger(int p)
{
if (status==running)
{
cout<<"车辆正在行驶,停车后再上下车!"<<endl;
}
else
{
passengerNum+=p;
if(passengerNum>seatNum)
{
passengerNum=seatNum;
cout<<"涉嫌超员,已清理后达到满员!"<<endl;
}
else if (passengerNum<1)
{
passengerNum=1;
cout<<"请司机不要离开岗位!"<<endl;
}
}
}
class Motorcycle: public Bicycle, public Motorcar
{
public:    
Motorcycle(int maxS=90, int w=100, int s=3, int p=1, int h=0.7);
void show();
};
Motorcycle::Motorcycle(int maxS, int w, int s, int p, int h):Vehicle(maxS, w),Bicycle(maxS, w, h),Motorcar(maxS, w, s, p) {}
void Motorcycle::show()
{
cout<<"状态:";
if(status==rest)
cout<<"泊车;\t";
else
cout<<"行进;\t";
cout<<"车速:"<<currentSpeed<<" / "<< maxSpeed <<"\t当前乘员:"<<passengerNum<<" / "<< seatNum << endl;
}
int main( )
{
Motorcycle m;
bool end=false;
while (!end)
{
cout<<"请操作:1-启动  2-加速  3-减速  4-有人上车  5-有人下车  6-停车 0-结束"<<endl;
char keydown= _getch();
switch(keydown)
{
case '1':
cout<<"选中的操作是1-启动\t";
m.start();
break;
case '2':
cout<<"选中的操作是2-加速\t";
m.speed_up();
break;
case '3':
cout<<"选中的操作是3-减速\t";
m.slow_down();
break;
case '4':
cout<<"选中的操作是4-有人上车\t";
m.addPassenger();
break;
case '5':
cout<<"选中的操作是5-有人下车\t";
m.addPassenger(-1);
break;
case '6':
cout<<"选中的操作是6-停车\t";
m.stop();
break;
case '0':
end=true;
break;
}
m.show();
cout<<endl;
Sleep(200); 
}
return 0;
}


运行结果:

这篇关于第十二周上机项目2摩托车继承自行车和机动车的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/243679

相关文章

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基

springboot项目如何开启https服务

《springboot项目如何开启https服务》:本文主要介绍springboot项目如何开启https服务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录springboot项目开启https服务1. 生成SSL证书密钥库使用keytool生成自签名证书将

将Java项目提交到云服务器的流程步骤

《将Java项目提交到云服务器的流程步骤》所谓将项目提交到云服务器即将你的项目打成一个jar包然后提交到云服务器即可,因此我们需要准备服务器环境为:Linux+JDK+MariDB(MySQL)+Gi... 目录1. 安装 jdk1.1 查看 jdk 版本1.2 下载 jdk2. 安装 mariadb(my

Node.js 数据库 CRUD 项目示例详解(完美解决方案)

《Node.js数据库CRUD项目示例详解(完美解决方案)》:本文主要介绍Node.js数据库CRUD项目示例详解(完美解决方案),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考... 目录项目结构1. 初始化项目2. 配置数据库连接 (config/db.js)3. 创建模型 (models/

springboot项目中常用的工具类和api详解

《springboot项目中常用的工具类和api详解》在SpringBoot项目中,开发者通常会依赖一些工具类和API来简化开发、提高效率,以下是一些常用的工具类及其典型应用场景,涵盖Spring原生... 目录1. Spring Framework 自带工具类(1) StringUtils(2) Coll

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.

一文教你如何将maven项目转成web项目

《一文教你如何将maven项目转成web项目》在软件开发过程中,有时我们需要将一个普通的Maven项目转换为Web项目,以便能够部署到Web容器中运行,本文将详细介绍如何通过简单的步骤完成这一转换过程... 目录准备工作步骤一:修改​​pom.XML​​1.1 添加​​packaging​​标签1.2 添加

tomcat多实例部署的项目实践

《tomcat多实例部署的项目实践》Tomcat多实例是指在一台设备上运行多个Tomcat服务,这些Tomcat相互独立,本文主要介绍了tomcat多实例部署的项目实践,具有一定的参考价值,感兴趣的可... 目录1.创建项目目录,测试文China编程件2js.创建实例的安装目录3.准备实例的配置文件4.编辑实例的