Object Oriented Programming (2)

second job


c language flow chart


c++ class diagram


Comparison of the two

When facing the process, the written program needs to think about every step, set the elevator and the requested variables, and then use different functions to call and execute step by step. this program.
When using object-oriented design, the designed elevator class and the generated elevator object, each method is a specific function, a type of variable is integrated into a class, the method is similar to the function, the method is more for The object itself is handled, the problem is divided by function, and multiple elevators can be simulated by creating multiple elevator objects.


Elevator

#include <queue>
using namespace std;
class lift
{
    //实验性质 
    private:
        int time;//电梯的运行时间
        int pos;//电梯的位置
        int sta;//电梯当前运行状态
        queue<int> des;//目的地队列 
    public:
        lift();//构造函数
        ~lift();//析构函数 
        int get_pos();//获取位置函数 
        void add_des(int floor);//目的楼层加入队列 
}; 
#include<iostream>
#include<queue>
#include"lift.h"
using namespace std;

lift::lift()//构造函数 
{
    time = 0;
    pos = 1;//最底层为一层 
    sta = 0;//0为上行,1为下行。 
}

int lift::get_pos()
{
    return pos;
}

void lift::add_des(int floor)
{
    des.push(floor);
} 

Guess you like

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