The difference between C and C++ in elevator handling
(Note: personal understanding)
Comparison and difference:
C language programs formulate specific processes, and proceed step by step according to the process.
C++ programs structure the process, and use the interface to access and call the class structure structure of different functions when needed.
elevator code
Elevator class definition
#pragma once
namespace myelevator
{
class Elevator {
private:
enum Status {UP = 1, DOWN = -1, STOP = 0};
int direction;//方向
int now_floor;//当前楼层
int targer_floor;//目标楼层
Elevator();
public:
int floor();//显示当前楼层
void to(int);//接受楼层指令
~Elevator() { }
};//电梯对象
}
Elevator class implementation
#include "elevator.h"
using nemespace myelevator;
Elevator::Elevator(){
now_floor = 1;
}
int Elevator::floor(){
return now_floor;
}
void Elevator::to(int floor){
targer_floor = floor;
if(now_floor > target_floor)
direction = DOWN;
else
direction = UP;
return;
}