第13週プロジェクト3——車両類(運用虚基数実現)

5142 ワード

/*
*             
* Copyright (c)2012,            
* All rightsreserved.
*     : object.cpp
*   :  
*     : 2013 5    24 
*    : 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; //rest-    ;running-    
public:
	vehicle(int maxS, int w); //    ,   ,      0       
	void start();  // rest   running,    1
	void stop(); // running   rest,       5 ,     
	void speed_up();  //  ,  1 ,   1
	void slow_down(); //  ,  1 ,   1,   0 ,  
};

//    ,   ,      0       
vehicle::vehicle(int maxS, int w):maxSpeed(maxS), currentSpeed(0),weight(w), status(rest){}

//  : rest   running,    1
void vehicle::start()
{
	if (status==rest)
	{
		status=running;
		currentSpeed=1;
	}
	else
		cout<<"      !"<<endl;
}
// running   rest,       5 ,     
void vehicle::stop()
{
	if (status==running)
		if(currentSpeed<5)
		{
			status=rest;
			currentSpeed=0;
		}
		else
			cout<<"    !      ……"<<endl;
	else
		cout<<"     !"<<endl;
}

//  ,  1 ,   1
void vehicle::speed_up()
{
	if (status==running)
		if(currentSpeed<maxSpeed)
			++currentSpeed;
		else
			cout<<"       ……"<<endl;
	else
		cout<<"     !"<<endl;
}
//  ,  1 ,   1,   0 ,  
void vehicle::slow_down()
{
	if (status==running)
	{
		if(currentSpeed>0)
			--currentSpeed;
	}
	else
		cout<<"     !"<<endl;
	if(currentSpeed==0)
		status=rest;
}

class bicycle :virtual public 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 public 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);   //    ,     ,     ,p   。         1 (  )。         ……
};

//      
motorcar::motorcar(int maxS, int w, int s, int p): vehicle(maxS, w),seatNum(s),passengerNum(p){}

//    ,     ,     ,p   。         1 (  )。         ……
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(); //_getch()          ,      <conio.h>
		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;
}



出力結果: