2017 class object-oriented programming assignment two

The following are all taking the scanning method as an example, that is, the elevator will only choose to make a U-turn at the bottom and top floors, and a passenger with the same path direction will be brought to the elevator on the way.

A textual description of the steps in a process-oriented implementation:

1. Define variables related to the elevator, such as: 1. The current floor of the elevator. 2. The number of people in the elevator, 3. The current direction of the elevator, and so on. Then define the variables related to passengers, such as: request time, floor, going to floor, and can also include its current state - request state, ride state, end state.
2. When the first request is received, the elevator starts to work, starting from the ground floor.
1. Go up one level.
2. Determine whether to reach the top or bottom layer, if so, change the direction.
3. Determine whether someone meets the conditions for getting on the elevator or getting off the elevator. If yes, pull him up, otherwise continue to move forward.
4. Determine whether the request has been completed, if so, end, otherwise continue the cycle.

object class diagram


Process-oriented I think it is necessary to analyze the steps required to solve the problem as a whole, and then implement it step by step. As far as the elevator is concerned, the elevator first accepts the request signal, then opens the door, takes the passenger, closes the door, climbs up, reaches the destination, opens the door, the passenger gets off the elevator, and closes the door. A request ends. The process-oriented programming method requires the programmer to design the entire program structure from the beginning, and therefore requires the programmer to have a comprehensive understanding of the problem domain. This is not suitable for multiple programmers working together.
Object-oriented I think it is to divide an event into multiple objects to achieve. An object is a thing. When coding, first abstract the object, and then construct the solution environment you need, in which the object and the solution are linked. Still take the elevator as an example, including: 1. Passengers, get all input requests. 2. Elevator, update the elevator status. 3. Judgment to end the system.
It can be seen that object orientation divides the whole process according to function. Object-oriented division of objects is convenient for multiple programmers to make changes to various parts at the same time, which is more in line with the efficiency people pursue.

class Elevator
{
private:
    int currentFloor;   // 电梯当前楼层 
    int direction;    // 1 代表向上  ,0 代表向下
public:
    Elevator(int dir = 1, int floor = 0 ,int pp= 0);  // 电梯初始向上走 ,且电梯在第0层
    ~Elevator();
    int population;
    void changeDirection(int dir)   // 改变电梯运行方向
    {
        direction = dir;
    }
    void move(int dir)  // 电梯的移动
    {
        if (dir)  currentFloor++;
        else currentFloor--;
    }
    int getCurrentFloor()  // 电梯当前楼层
    {
        return currentFloor;
    }
    int getDirection()
    {
        return direction;
    }
};
Elevator::Elevator(int dir, int floor, int pp)
{
    direction = dir;
    currentFloor = floor;
    population = pp;
}
Elevator::~Elevator()
{
    cout << "析构函数"<<endl;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325390571&siteId=291194637