注意:这篇文章上次更新于1836天前,文章内容可能已经过时。
This article was last updated1836 days ago, the content may be outdated.
管理系统需求
职工管理系统可以用来管理公司内所有员工的信息
本教程主要利用C++来实现一个基于多态的职工管理系统
公司中职工分为三类:普通员工、经理、老板,显示信息时,需要显示职工编号、职工姓名、职工岗位、以及职责
普通员工职责:完成经理交给的任务
经理职责:完成老板交给的任务,并下发任务给员工
老板职责:管理公司所有事务
管理系统中需要实现的功能如下:
- 退出管理程序:退出当前管理系统
- 增加职工信息:实现批量添加职工功能,将信息录入到文件中,职工信息为:职工编号、姓名、部门编号
- 显示职工信息:显示公司内部所有职工的信息
- 删除离职职工:按照编号删除指定的职工
- 修改职工信息:按照编号修改职工个人信息
- 查找职工信息:按照职工的编号或者职工的姓名进行查找相关的人员信息
- 按照编号排序:按照职工编号,进行排序,排序规则由用户指定
- 清空所有文档:清空文件中记录的所有职工信息 (清空前需要再次确认,防止误删)
系统界面效果图如下:

需根据用户不同的选择,完成不同的功能!
创建项目
创建项目步骤如下:
- 创建新项目
- 添加文件
创建项目
打开vs2017后,点击创建新项目,创建新的C++项目

填写项目名称以及项目路径,点击确定

添加文件
右键源文件,进行添加文件操作



至此,项目已创建完毕
创建管理类
管理类负责的内容如下:
- 与用户的沟通菜单界面
- 对职工增删改查的操作
- 与文件的读写交互
创建文件
在头文件和源文件的文件夹下分别创建workerManager.h 和 workerManager.cpp文件

头文件实现
在workerManager.h中设计管理类
代码如下:
System Management Requirements
The worker management system can be used to manage the information of all employees in the company
This tutorial mainly uses C++ to implement a worker management system based on polymorphism
The workers in the company are divided into three categories: ordinary employees, managers and bosses. When displaying information, the worker ID, worker name, worker position and responsibilities need to be shown
Responsibilities of ordinary employees: complete the tasks assigned by the manager
Responsibilities of the manager: complete the tasks assigned by the boss and assign tasks to employees
Responsibilities of the boss: manage all company affairs
The functions that need to be implemented in the management system are as follows:
- Exit the management program: exit the current management system
- Add worker information: implement batch adding of workers and record the information into a file. The worker information is: worker ID, name, department ID
- Display worker information: display the information of all workers in the company
- Delete resigned workers: delete the specified worker by ID
- Modify worker information: modify a worker’s personal information by ID
- Search worker information: search related personnel information by worker ID or worker name
- Sort by ID: sort by worker ID, and the sorting rule is specified by the user
- Clear all documents: clear all worker information recorded in the file (confirmation is required before clearing to prevent accidental deletion)
The system interface effect is shown below:

Different functions need to be completed according to different user selections!
Create the Project
The steps to create the project are as follows:
- Create a new project
- Add files
Create a Project
After opening vs2017, click Create New Project to create a new C++ project

Fill in the project name and project path, then click OK

Add Files
Right-click Source Files to add a file



At this point, the project has been created
Create the Management Class
The management class is responsible for the following:
- The menu interface for communicating with the user
- Operations for adding, deleting, modifying and searching workers
- Read/write interaction with files
Create the File
Create the workerManager.h and workerManager.cpp files in the header file folder and the source file folder respectively

Header File Implementation
Design the management class in workerManager.h
The code is as follows:
1 |
|
Source File Implementation
In workerManager.cpp, complete the empty implementations of the constructor and destructor
1 |
|
至此职工管理类已创建完毕
菜单功能
功能描述:与用户的沟通界面
添加成员函数
在管理类workerManager.h中添加成员函数 void Show_Menu();

菜单功能实现
在管理类workerManager.cpp中实现 Show_Menu()函数
At this point, the worker management class has been created
Menu Function
Function description: the interface for communicating with the user
Add a Member Function
Add the member function void Show_Menu(); to the management class workerManager.h

Menu Function Implementation
Implement the Show_Menu() function in workerManager.cpp
1 | void WorkerManager::Show_Menu() |
1 |
|
The running effect is shown below:

Exit Function
Provide the Function Interface
In the main function, provide branch selection and an interface for each function
Code:
1 | int main() { |
Implement the Exit Function
In workerManager.h, provide the member function exitSystem(); for exiting the system
In workerManager.cpp, provide the concrete implementation
1 | void WorkerManager::exitSystem() |
测试功能
在main函数分支 0 选项中,调用退出程序的接口

运行测试效果如图:

创建职工类
创建职工抽象类
职工的分类为:普通员工、经理、老板
将三种职工抽象到一个类(worker)中,利用多态管理不同职工种类
职工的属性为:职工编号、职工姓名、职工所在部门编号
职工的行为为:岗位职责信息描述,获取岗位名称
头文件文件夹下 创建文件worker.h 文件并且添加如下代码:
Test the Function
In option 0 of the main function branch, call the interface for exiting the program

The running test effect is shown below:

Create the Worker Class
Create the Worker Abstract Class
The workers are divided into: ordinary employees, managers and bosses
Abstract the three kinds of workers into one class (worker) and use polymorphism to manage different worker types
The attributes of a worker are: worker ID, worker name, and the ID of the department the worker belongs to
The behaviors of a worker are: description of position responsibilities and getting the position name
In the header file folder, create the file worker.h and add the following code:
1 |
|
Create the Ordinary Employee Class
The ordinary employee class inherits the worker abstract class and overrides the pure virtual functions of the parent class
Create the employee.h and employee.cpp files in the header file folder and the source file folder respectively
The code in employee.h is as follows:
1 |
|
employee.cpp中代码如下:
The code in employee.cpp is as follows:
1 |
|
Create the Manager Class
The manager class inherits the worker abstract class and overrides the pure virtual functions of the parent class, similar to the ordinary employee class
Create the manager.h and manager.cpp files in the header file folder and the source file folder respectively
The code in manager.h is as follows:
1 |
|
manager.cpp中代码如下:
The code in manager.cpp is as follows:
1 |
|
Create the Boss Class
The boss class inherits the worker abstract class and overrides the pure virtual functions of the parent class, similar to the ordinary employee class
Create the boss.h and boss.cpp files in the header file folder and the source file folder respectively
The code in boss.h is as follows:
1 |
|
boss.cpp中代码如下:
The code in boss.cpp is as follows:
1 |
|
Test Polymorphism
Add a test function in 职工管理系统.cpp and run it so that polymorphism is produced
The test code is as follows:
1 |
|
运行效果如图:

测试成功后,测试代码可以注释保留,或者选择删除
添加职工
功能描述:批量添加职工,并且保存到文件中
功能分析
分析:
用户在批量创建时,可能会创建不同种类的职工
如果想将所有不同种类的员工都放入到一个数组中,可以将所有员工的指针维护到一个数组里
如果想在程序中维护这个不定长度的数组,可以将数组创建到堆区,并利用Worker **的指针维护

功能实现
在WokerManager.h头文件中添加成员属性 代码:
The running effect is shown below:

After the test succeeds, the test code can be kept as comments or deleted
Add Workers
Function description: add workers in batches and save them to a file
Function Analysis
Analysis:
When creating workers in batches, the user may create different kinds of workers
If you want to put all kinds of employees into one array, you can maintain the pointers of all employees in an array
If you want to maintain this dynamic-length array in the program, you can create the array on the heap and maintain it with a Worker ** pointer

Function Implementation
In the WokerManager.h header file, add the member attribute. Code:
1 | //记录文件中的人数个数 |
在WorkerManager构造函数中初始化属性
Initialize the attributes in the WorkerManager constructor
1 | WorkerManager::WorkerManager() |
在workerManager.h中添加成员函数
Add the member function in workerManager.h
1 | //增加职工 |
workerManager.cpp中实现该函数
Implement this function in workerManager.cpp
1 | //增加职工 |
在WorkerManager.cpp的析构函数中,释放堆区数据
In the destructor of WorkerManager.cpp, release the heap data
1 | WorkerManager::~WorkerManager() |
测试添加
在main函数分支 1 选项中,调用添加职工接口

效果如图:

至此,添加职工到程序中功能实现完毕
文件交互 - 写文件
功能描述:对文件进行读写
在上一个添加功能中,我们只是将所有的数据添加到了内存中,一旦程序结束就无法保存了
因此文件管理类中需要一个与文件进行交互的功能,对于文件进行读写操作
设定文件路径
首先我们将文件路径,在workerManager.h中添加宏常量,并且包含头文件 fstream
Test Adding
In option 1 of the main function branch, call the interface for adding workers

The effect is shown below:

At this point, the function of adding workers to the program is fully implemented
File Interaction - Writing Files
Function description: read and write files
In the previous add function, we only added all the data to memory; once the program ends, it cannot be saved
Therefore, the file management class needs a function to interact with the file for reading and writing operations
Set the File Path
First, add a macro constant for the file path in workerManager.h and include the header file fstream
1 |
1 | //保存文件 |
1 | void WorkerManager::save() |
保存文件功能测试
在添加职工功能中添加成功后添加保存文件函数

再次运行代码,添加职工

同级目录下多出文件,并且保存了添加的信息

文件交互 - 读文件
功能描述:将文件中的内容读取到程序中
虽然我们实现了添加职工后保存到文件的操作,但是每次开始运行程序,并没有将文件中数据读取到程序中
而我们的程序功能中还有清空文件的需求
因此构造函数初始化数据的情况分为三种
- 第一次使用,文件未创建
- 文件存在,但是数据被用户清空
- 文件存在,并且保存职工的所有数据
文件未创建
在workerManager.h中添加新的成员属性 m_FileIsEmpty标志文件是否为空
Save File Function Test
After adding workers successfully in the add-worker function, add the save-file function

Run the code again and add workers

An extra file appears in the same directory, and the added information is saved in it

File Interaction - Reading Files
Function description: read the content of the file into the program
Although we implemented saving to a file after adding workers, every time the program starts running, the data in the file is not read into the program
And our program functions also include the need to clear the file
Therefore, the initialization of data in the constructor is divided into three cases
- First use: the file has not been created
- The file exists, but the data has been cleared by the user
- The file exists and contains all worker data
File Does Not Exist
In workerManager.h, add a new member attribute m_FileIsEmpty to mark whether the file is empty
1 | //标志文件是否为空 |
修改WorkerManager.cpp中构造函数代码
Modify the constructor code in WorkerManager.cpp
1 | WorkerManager::WorkerManager() |
After deleting the file, test the data initialization function when the file does not exist
File Exists and Data Is Empty
Append the following code to the constructor in WorkerManager.cpp:
1 | //文件存在,并且没有记录 |
追加代码位置如图:

将文件创建后清空文件内容,并测试该情况下初始化功能
我们发现文件不存在或者为空清空 m_FileIsEmpty 判断文件是否为空的标志都为真,那何时为假?
成功添加职工后,应该更改文件不为空的标志
在void WorkerManager::Add_Emp()成员函数中添加:
The position of the appended code is shown in the figure:

After creating the file, clear the file content and test the initialization function in this case
We found that when the file does not exist or is empty, the m_FileIsEmpty flag that determines whether the file is empty is always true. When does it become false?
After successfully adding workers, the flag that the file is not empty should be changed
In the void WorkerManager::Add_Emp() member function, add:
1 | //更新职工不为空标志 |

File Exists and Contains Worker Data
Get the Number of Workers Recorded
In workerManager.h, add the member function int get_EmpNum();
1 | //统计人数 |
workerManager.cpp中实现
Implement it in workerManager.cpp
1 | int WorkerManager::get_EmpNum() |
在workerManager.cpp构造函数中继续追加代码:
Continue to append code in the constructor of WorkerManager.cpp:
1 | int num = this->get_EmpNum(); |
手动添加一些职工数据,测试获取职工数量函数


初始化数组
根据职工的数据以及职工数据,初始化workerManager中的Worker ** m_EmpArray 指针
在WorkerManager.h中添加成员函数 void init_Emp();
Manually add some worker data and test the function for getting the number of workers


Initialize the Array
According to the worker data, initialize the Worker ** m_EmpArray pointer in workerManager
In WorkerManager.h, add the member function void init_Emp();
1 | //初始化员工 |
在WorkerManager.cpp中实现
Implement it in WorkerManager.cpp
1 | void WorkerManager::init_Emp() |
在workerManager.cpp构造函数中追加代码
Append code in the constructor of workerManager.cpp
1 | //根据职工数创建数组 |
运行程序,测试从文件中获取的数据

至此初始化数据功能完毕,测试代码可以注释或删除掉!
显示职工
功能描述:显示当前所有职工信息
显示职工函数声明
在workerManager.h中添加成员函数 void Show_Emp();
Run the program and test the data read from the file

At this point, the data initialization function is complete. The test code can be commented out or deleted!
Display Workers
Function description: display the information of all current workers
Display Worker Function Declaration
In workerManager.h, add the member function void Show_Emp();
1 | //显示职工 |
Display Worker Function Implementation
Implement the member function void Show_Emp(); in workerManager.cpp
1 | //显示职工 |
测试显示职工
在main函数分支 2 选项中,调用显示职工接口

测试时分别测试 文件为空和文件不为空两种情况
测试效果:
测试1-文件不存在或者为空情况

测试2 - 文件存在且有记录情况

测试完毕,至此,显示所有职工信息功能实现
删除职工
功能描述:按照职工的编号进行删除职工操作
删除职工函数声明
在workerManager.h中添加成员函数 void Del_Emp();
Test Displaying Workers
In option 2 of the main function branch, call the interface for displaying workers

When testing, test the two cases of an empty file and a non-empty file separately
Test effect:
Test 1 - the file does not exist or is empty

Test 2 - the file exists and has records

The test is complete. At this point, the function of displaying all worker information is implemented
Delete Workers
Function description: delete workers by their IDs
Delete Worker Function Declaration
In workerManager.h, add the member function void Del_Emp();
1 | //删除职工 |
职工是否存在函数声明
很多功能都需要用到根据职工是否存在来进行操作如:删除职工、修改职工、查找职工
因此添加该公告函数,以便后续调用
在workerManager.h中添加成员函数 int IsExist(int id);
Worker Existence Function Declaration
Many functions need to operate based on whether a worker exists, such as deleting workers, modifying workers and searching workers
Therefore, add this common function for later calls
In workerManager.h, add the member function int IsExist(int id);
1 | //按照职工编号判断职工是否存在,若存在返回职工在数组中位置,不存在返回-1 |
Worker Existence Function Implementation
Implement the member function int IsExist(int id); in workerManager.cpp
1 | int WorkerManager::IsExist(int id) |
Delete Worker Function Implementation
Implement the member function void Del_Emp(); in workerManager.cpp
1 | //删除职工 |
测试删除职工
在main函数分支 3 选项中,调用删除职工接口

测试1 - 删除不存在职工情况

测试2 - 删除存在的职工情况
删除成功提示图:

再次显示所有职工信息,确保已经删除

查看文件中信息,再次核实员工已被完全删除

至此,删除职工功能实现完毕!
修改职工
功能描述:能够按照职工的编号对职工信息进行修改并保存
修改职工函数声明
在workerManager.h中添加成员函数 void Mod_Emp();
Test Deleting Workers
In option 3 of the main function branch, call the interface for deleting workers

Test 1 - delete a non-existent worker

Test 2 - delete an existing worker
Success prompt:

Display all worker information again to make sure it has been deleted

Check the information in the file again to verify that the employee has been completely deleted

At this point, the delete worker function is fully implemented!
Modify Workers
Function description: modify worker information by worker ID and save it
Modify Worker Function Declaration
In workerManager.h, add the member function void Mod_Emp();
1 | //修改职工 |
Modify Worker Function Implementation
Implement the member function void Mod_Emp(); in workerManager.cpp
1 | //修改职工 |
测试修改职工
在main函数分支 4 选项中,调用修改职工接口

测试1 - 修改不存在职工情况

测试2 - 修改存在职工情况,例如将职工 “李四” 改为 “赵四”

修改后再次查看所有职工信息,并确认修改成功

再次确认文件中信息也同步更新

至此,修改职工功能已实现!
查找职工
功能描述:提供两种查找职工方式,一种按照职工编号,一种按照职工姓名
查找职工函数声明
在workerManager.h中添加成员函数 void Find_Emp();
Test Modifying Workers
In option 4 of the main function branch, call the interface for modifying workers

Test 1 - modify a non-existent worker

Test 2 - modify an existing worker, for example change the worker “李四” to “赵四”

After modification, view all worker information again and confirm the modification is successful

Confirm again that the information in the file is also updated synchronously

At this point, the modify worker function is implemented!
Search Workers
Function description: provide two ways to search workers, one by worker ID and one by worker name
Search Worker Function Declaration
In workerManager.h, add the member function void Find_Emp();
1 | //查找职工 |
Search Worker Function Implementation
Implement the member function void Find_Emp(); in workerManager.cpp
1 | //查找职工 |
测试查找职工
在main函数分支 5 选项中,调用查找职工接口

测试1 - 按照职工编号查找 - 查找不存在职工

测试2 - 按照职工编号查找 - 查找存在职工

测试3 - 按照职工姓名查找 - 查找不存在职工

测试4 - 按照职工姓名查找 - 查找存在职工(如果出现重名,也一并显示,在文件中可以添加重名职工)
例如 添加两个王五的职工,然后按照姓名查找王五


至此,查找职工功能实现完毕!
排序
功能描述:按照职工编号进行排序,排序的顺序由用户指定
排序函数声明
在workerManager.h中添加成员函数 void Sort_Emp();
Test Searching Workers
In option 5 of the main function branch, call the interface for searching workers

Test 1 - search by worker ID - search for a non-existent worker

Test 2 - search by worker ID - search for an existing worker

Test 3 - search by worker name - search for a non-existent worker

Test 4 - search by worker name - search for an existing worker (if there are duplicate names, they are all displayed; workers with duplicate names can be added in the file)
For example, add two workers named 王五, then search by name for 王五


At this point, the search worker function is fully implemented!
Sort
Function description: sort by worker ID, and the sorting order is specified by the user
Sort Function Declaration
In workerManager.h, add the member function void Sort_Emp();
1 | //排序职工 |
1 | //排序职工 |
测试排序功能
在main函数分支 6 选项中,调用排序职工接口

测试:
首先我们添加一些职工,序号是无序的,例如:

测试 - 升序排序

文件同步更新

测试 - 降序排序

文件同步更新

至此,职工按照编号排序的功能实现完毕!
清空文件
功能描述:将文件中记录数据清空
清空函数声明
在workerManager.h中添加成员函数 void Clean_File();
Test the Sort Function
In option 6 of the main function branch, call the interface for sorting workers

Test:
First we add some workers with unordered IDs, for example:

Test - ascending sort

The file is updated synchronously

Test - descending sort

The file is updated synchronously

At this point, the function of sorting workers by ID is fully implemented!
Clear the File
Function description: clear the data recorded in the file
Clear Function Declaration
In workerManager.h, add the member function void Clean_File();
1 | //清空文件 |
1 | //清空文件 |
测试清空文件
在main函数分支 7 选项中,调用清空文件接口

测试:确认清空文件

再次查看文件中数据,记录已为空

打开文件,里面数据已确保清空,该功能需要慎用!

随着清空文件功能实现,本案例制作完毕 ^ _ ^
我的实现
头文件
管理类头文件–workerManager.h:
Test Clearing the File
In option 7 of the main function branch, call the interface for clearing the file

Test: confirm clearing the file

Check the data in the file again; the records are empty now

Open the file; the data is confirmed to be cleared. This function should be used with caution!

With the clear-file function implemented, this case study is complete ^ _ ^
My Implementation
Header Files
Management class header file – workerManager.h:
1 |
|
职工抽象类头文件–Worker.h:
Worker abstract class header file – Worker.h:
1 |
|
老板类头文件–Boss.h:
Boss class header file – Boss.h:
1 |
|
经理类头文件–Manager.h:
Manager class header file – Manager.h:
1 |
|
职工类头文件–Employee.h:
Employee class header file – Employee.h:
1 |
|
1 |
|
老板类源文件–Boss.cpp:
Boss class source file – Boss.cpp:
1 |
|
经理类源文件–Manager.cpp:
Manager class source file – Manager.cpp:
1 |
|
职工类源文件–Employee.cpp:
Employee class source file – Employee.cpp:
1 |
|
主函数文件–职工管理系统.cpp:
Main function file – 职工管理系统.cpp:
1 |
|


