本次系统实现的功能有
1.退出系统
2.添加员工
3.将所有员工信息显示在屏幕上
4.删除员工
5.修改员工信息
6.查找员工信息
7.按照员工工号进行排序
8.格式化文件的数据
下图代码为本次管理系统的主函数部分
#include"workermanager.h"
using namespace std;
#include<iostream>
#include"worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
#include<string>
int main()
{
WorkerManager tt;
int choice = 0;
while(1)
{
tt.showmenu();
cout << "please input your choice " << endl;
cin >> choice;
switch(choice)
{
case 0:
tt.exitsystem(); // exit 退出系统
break;
case 1:
tt.ADD_Emp(); //add 添加成员
break;
case 2:
tt.show_emp(); //show 显示成员信息
break;
case 3:
tt.del_emp(); //delete 删除想要删除的成员信息
break;
case 4:
tt.mod_emp(); // change 修改成员信息
break;
case 5:
tt.select_emp(); //selete 查找成员
break;
case 6:
tt.sort_emp(); //sort 按序号对成员进行排序
break;
case 7:
tt.clean_emp(); //format 格式化文件
break;
default:
system("clear");
break;
}
}
return 0;
}
第一个类 Worker,Worker类中有两个虚构函数,分别是显示成员信息和,得到成员的职责;
Worker类 (文件名为worker.h)
#pragma once
#include<iostream>
using namespace std;
#include<string>
class Worker
{
public:
virtual void showInfo() = 0; //纯虚函数,申明却不实现
virtual string getDeptName() = 0; //纯虚函数,声明却不实现
public:
int m_ID;
string m_Name;
int m_DepId;
};
在本系统中,共有三种成员,分别是职工,经理,老板,他们的部门ID分别为1,2,3
对应着三种不同的职责;
我们来看职工类,(文件名为employee.h)代码如下图所示,职工类继承了Worker类,在publi作用域下,有三个函数,分别是”构造函数“,“展示信息函数”,以及“得到自己的职责函数”,由于在.h文件一般仅作申明,所以具体的实现在employee.cpp文件中
employee类如下图所示 (文件名 employee.h)
#pragma once //仅定义一次的标志
#include<iostream>
using namespace std;
#include"worker.h" //由于继承了Worker类,所以需要包含头文件
#include<string>
class Employee : public Worker
{
public:
Employee(int id, string name, int Did);
virtual void showInfo();
virtual string getDeptName();
};
接下来我们看employee.cpp 文件,该文件作用是对employee.h中的函数做一个具体的实现。
#include"employee.h"
#include<string>
Employee::Employee (int id, string name, int Did)
{
this->m_ID = id;
this->m_Name = name;
this->m_DepId = Did;
}
void Employee::showInfo()
{
cout << "employee ID " << this->m_ID
<< "\t employee name " << this->m_Name
<< "\t position " << (this->getDeptName())
<< "\t finish the work " << endl;
}
string Employee::getDeptName()
{
return string (" worker ");
}
然后分别是其他两个类,Manager类和Boss类,他们也都需要继承Worker类
Manager类(文件名为manager.h)
#pragma once
#include <iostream>
#include "worker.h"
using namespace std;
class Manager : public Worker
{
public:
Manager(int id,string name, int pid);
virtual void showInfo();
virtual string getDeptName();
};
Manager类的具体实现在manager.cpp 文件中
manager.cpp文件如下
#include"manager.h"
#include<string>
void Manager::showInfo()
{
cout << "employee ID " << this->m_ID
<< "\t employee name " << this->m_Name
<< "\t position " << (this->getDeptName())
<< "\t finish the work for the boss" << endl;
}
string Manager::getDeptName()
{
return string("manager ");
}
接下来我们看Boss类
#pragma once
#include<iostream>
#include"worker.h"
using namespace std;
class Boss : public Worker
{
public:
Boss(int id, string name , int Pid);
virtual void showInfo();
virtual string getDeptName();
};
Boss类的具体实现在boss.cpp 文件中
#include "boss.h"
Boss::Boss(int id, string name , int pid)
{
this->m_ID = id;
this->m_Name = name;
this->m_DepId = pid;
}
void Boss::showInfo()
{
cout << "employee ID " << this->m_ID
<< "\t employee name " << this->m_Name
<< "\t position " << (this->getDeptName())
<< "\t tell the worker" << endl;
}
string Boss::getDeptName()
{
return string (" boss ");
}
这几个类介绍完了之后我们来看我们我们系统需要实现的功能首先需要有一个主界面
那么这些类都建立好了之后,我们还需要建立一个类来协助main函数完成相应的功能,我们需要实现运行程序时,首先进入主界面,然后做出选择,来实现不同的功能;
所以我们来创建一个职工管理的类,在这个类中去声明这些必要的功能函数,当然也需要一个职工管理类对象的cpp 文件来做具体的实现。
WorkerManger 类
#pragma once
#include<iostream>
#include<string>
#include"worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
#include<fstream>
using namespace std;
class WorkerManager
{
public:
WorkerManager(); // 构造函数,作为初始化操作
public:
void showmenu();
void exitsystem(); // 退出系统函数
int m_empnum;
Worker ** m_EmpArray;
void ADD_Emp(); //添加成员函数
void save(); //将数据保存到文件的函数
bool m_fileisempty; //一个布尔变量,来判断文件是否为空
int get_empnum();
void init_emp(); //初始化函数
void show_emp(); //将文件中的内容显示在屏幕上
void del_emp(); //删除函数
int isexit(int findnum); //判断函数,引入想要查找的序号,判断在不在文件中
void select_emp(); //根据序号查找,找到的话,显示其信息
void mod_emp(); //修改信息函数
void sort_emp(); //排序函数
void clean_emp(); //格式化文件函数
~WorkerManager(); //析构函数作为对堆区的空间做释放操作,防止内存泄漏
};