看板 NTUBIME101HW 關於我們 聯絡資訊
// program12.6.cpp : 定義主控台應用程式的進入點。 // #include "stdafx.h" #include "iostream" using namespace std; const int MAXFLOOR =15; //class declartion class Elevator { private: int currentFloor; public: Elevator(int =1); //constructor void request(int); }; //implementation section Elevator ::Elevator(int cfloor) { currentFloor = cfloor; } void Elevator ::request(int newfloor) { if (newfloor <1 || newfloor >MAXFLOOR || newfloor == currentFloor) ; //doing nothing else if (newfloor > currentFloor)// move elevator up { cout<<"\n Starting at floor " <<currentFloor <<endl; while(newfloor > currentFloor) { currentFloor++; //add one to current floor cout<< "Going UP - now at floor "<<currentFloor <<endl; } cout << "Stopping at floor "<<currentFloor<<endl; } else // move elevator down { cout<<"\n Starting at floor " <<currentFloor <<endl; while(newfloor < currentFloor) { currentFloor--; //add one to current floor cout<< "Going Down - now at floor "<<currentFloor <<endl; } cout << "Stopping at floor "<<currentFloor<<endl; } } int _tmain(int argc, _TCHAR* argv[]) { Elevator a; //declare 1 object of type Elevator a.request(6); a.request(3); system("pause"); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.168.35.142