📄 档.txt
字号:
// ElevatorClass.h
// ------------第一个文件-----------
#ifndef ELEVATORCLASS_H
#define ELEVATORCLASS_H
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::cerr;
//---- 宣告类别 Elevator--------
class Elevator
{
public:
Elevator(); // 预设建构函数
Elevator(int); // 建构函数(可设定初值)
~Elevator(); // 解构函数
void Call(int);
void Select(int);
static int Count; // static 变数
private:
int CurrentFloor;
void Move(int);
};
// --设定 static 变数初始值---------
int Elevator::Count = 0;
#endif
//-----------------第二个文件-----------------------
// ElevatorMain.cpp
// --------------------------------------
#include "ElevatorClass.h"
//-------主程式-------------------
main()
{
// (1)
cout << "(1)" << endl;
Elevator A; // 定义电梯 A
cout << "从 5 楼呼叫:" << endl;
A.Call(5); // 呼叫电梯 A
// 设定到 2 楼
A.Select(2); // 设定电梯 A
cout << "目前 Count的值是: "
<< A.Count << endl;
// (2)
cout << "(2)" << endl;
Elevator B(3); // 定义电梯 B
cout << "从 4 楼呼叫:" << endl;
B.Call(4); // 呼叫电梯 B
// 设定到 8 楼
B.Select(8); // 设定电梯 B
cout << "目前 Count 的值是: "
<< B.Count << endl;
}
//---------------第三个文件-----------------------
// ElevatorDef.cpp
// --------------------------------------
#include "ElevatorClass.h"
// 定义预设建构函数 Account()
Elevator::Elevator()
{
cout << "建构函数被呼叫" << endl;
CurrentFloor = 1;
Count++;
}
// 定义建构函数 Account() (可设定初值)
Elevator::Elevator(int N)
{
cout << "建构函数被呼叫" << endl;
CurrentFloor = N;
Count++;
}
// 定义解构函数 ~Elevator()
Elevator::~Elevator()
{ cout << "解构函数被呼叫" << endl;
Count--;
}
// --定义成员函数 Call()--------
void Elevator::Call(int N)
{
Move (N);
cout << "电梯到了, 请进." << endl;
return;
}
// --定义成员函数 Select()--------
void Elevator::Select(int N)
{
cout << "载您到 " << N << " 楼."
<< endl;
if (N > CurrentFloor)
cout << "电梯向上." << endl;
else
cout << "电梯向下." << endl;
Move (N);
cout << "电梯已到 " << CurrentFloor
<< " 楼, 谢谢光临." << endl;
}
void Elevator::Move(int Target)
{
cout << "电梯门要关了, 请小心." << endl;
int Start = CurrentFloor;
cout << "电梯目前在 " << CurrentFloor
<< " 楼" << endl;
if (Target >= CurrentFloor)
{
for (CurrentFloor = Start;
CurrentFloor <= Target; CurrentFloor++)
cout << " 灯号: " << CurrentFloor
<< " 楼" << endl;
CurrentFloor--;
}
else
{
for (CurrentFloor = Start;
CurrentFloor >= Target; CurrentFloor--)
cout << " 灯号: " << CurrentFloor
<< " 楼" << endl;
CurrentFloor++;
}
cout << "电梯门要开了, 请小心." << endl;
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -