第十二周上机项目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

相关文章

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

SpringBoot通过main方法启动web项目实践

《SpringBoot通过main方法启动web项目实践》SpringBoot通过SpringApplication.run()启动Web项目,自动推断应用类型,加载初始化器与监听器,配置Spring... 目录1. 启动入口:SpringApplication.run()2. SpringApplicat

Springboot项目构建时各种依赖详细介绍与依赖关系说明详解

《Springboot项目构建时各种依赖详细介绍与依赖关系说明详解》SpringBoot通过spring-boot-dependencies统一依赖版本管理,spring-boot-starter-w... 目录一、spring-boot-dependencies1.简介2. 内容概览3.核心内容结构4.

在ASP.NET项目中如何使用C#生成二维码

《在ASP.NET项目中如何使用C#生成二维码》二维码(QRCode)已广泛应用于网址分享,支付链接等场景,本文将以ASP.NET为示例,演示如何实现输入文本/URL,生成二维码,在线显示与下载的完整... 目录创建前端页面(Index.cshtml)后端二维码生成逻辑(Index.cshtml.cs)总结

Spring Boot项目如何使用外部application.yml配置文件启动JAR包

《SpringBoot项目如何使用外部application.yml配置文件启动JAR包》文章介绍了SpringBoot项目通过指定外部application.yml配置文件启动JAR包的方法,包括... 目录Spring Boot项目中使用外部application.yml配置文件启动JAR包一、基本原理

Springboot项目登录校验功能实现

《Springboot项目登录校验功能实现》本文介绍了Web登录校验的重要性,对比了Cookie、Session和JWT三种会话技术,分析其优缺点,并讲解了过滤器与拦截器的统一拦截方案,推荐使用JWT... 目录引言一、登录校验的基本概念二、HTTP协议的无状态性三、会话跟android踪技术1. Cook

springboot项目中集成shiro+jwt完整实例代码

《springboot项目中集成shiro+jwt完整实例代码》本文详细介绍如何在项目中集成Shiro和JWT,实现用户登录校验、token携带及接口权限管理,涉及自定义Realm、ModularRe... 目录简介目的需要的jar集成过程1.配置shiro2.创建自定义Realm2.1 LoginReal

idea Maven Springboot多模块项目打包时90%的问题及解决方案

《ideaMavenSpringboot多模块项目打包时90%的问题及解决方案》:本文主要介绍ideaMavenSpringboot多模块项目打包时90%的问题及解决方案,具有很好的参考价值,... 目录1. 前言2. 问题3. 解决办法4. jar 包冲突总结1. 前言之所以写这篇文章是因为在使用Mav