面向对象程序设计(2)

第二次作业


c语言流程图


c++类图


二者对比

面向过程的时候,所写的程序需要思考好每一个步骤,设定好电梯以及请求的各个变量,然后用不同的函数一步步依次调用去执行,更多的感觉是依照运行的步骤去设计执行这个程序。
而利用面向对象去设计的时候,设计出的电梯类,产生的电梯对象,每一个方法都是一个特定的功能,一个类型的变量整合在一个类中,方法和函数类似,方法更多的针对对象本身作处理,以功能划分问题,还能通过创建多个电梯对象来模拟多个电梯。


电梯类

#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);
} 

猜你喜欢

转载自www.cnblogs.com/pullself/p/8994187.html