对于电梯,属性之一就是位置,所以要实现这一程序,要设置电梯的初始位置和按下电梯按钮改变的电梯的位置。
代码如下:
#include <iostream>
#include <cstdlib>
#include <windows.h>
using namespace std;
class Elevator
{
private:
int currentFloor; //电梯所处位置
public:
Elevator(int cfloor=1); //构造函数
void request(int newfloor);
};
Elevator::Elevator(int cfloor)
{
currentFloor=cfloor;
}
void Elevator::request(int newfloor)
{
if(newfloor==currentFloor)
{
cout<<"You have on the floor!!!"<<endl;
return;
}
else if(newfloor>currentFloor) //电梯向上移动
{
cout<<endl<<"Starting at floor......"<<currentFloor<<endl;
while(newfloor>currentFloor)
{
currentFloor++;
Sleep(2000);
cout<<"Going up-now at floor.."<<currentFloor<<endl;
}
cout<<"Stopping at fl