这个项目分为头文件和CPP文件和一个.txt文件
实现功能如图所示
我一一列出来(头文件,cpp文件,txt文件)
按着这三个复制粘贴就可以用
boss.h
#pragma once
//普通员工头文件子类继承父类
#include"worker.h"
#include<iostream>
#include<string>
using namespace std;
class boss :public worker {
public:
//构造函数
boss(int id, string name, int did);
//显示个人信息
virtual void showinfo() ;
//获取岗位名称
virtual string getdepainfo() ;
};
employee.h
#pragma once
//普通员工头文件子类继承父类
#include"worker.h"
#include<iostream>
#include<string>
using namespace std;
class Employee:public worker
{
public://构造函数
Employee(int id, string name,int did);
//显示个人信息
void showinfo();//虚函数关键字可删可不删
//获取岗位名称
string getdepainfo();
};
manager.h
#pragma once
#include<iostream>
#include<string>
#include"worker.h"
using namespace std;
class manager :public worker {
public:
//构造函数
manager(int id, string name, int did);
//显示个人信息
void showinfo() ;
//获取岗位名称
string getdepainfo() ;
};
worker.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
//抽象员工子类
class worker {
public:
//显示个人信息
virtual void showinfo() = 0;
//获取岗位名称
virtual string getdepainfo() = 0;
int m_id;//职工编号
string m_name;//职工姓名
int m_depaid;//职工所在部门编号
};
workerManager.h
#pragma once//防止头文件重复包含
#include<iostream>//包含输入输出流头文件
#include"worker.h"
using namespace std;//使用标准命名空间
class workermanager {
public:
workermanager();//构造函数
void showmenu();//菜单函数
void exitsystem();//退出系统函数
//增加职工信息,员工,经理,老板
//职工的属性有:职工编号。,姓名,所在部门编号
//职工行为有:岗位职责信息描述,获取岗位名称
int m_empnum;//记录文件中的人数
worker ** m_emparray;//职工数组指针
//添加员工
void addemp();
//保存文件
void save();
void show();//打印职工信息
bool m_fileisemp;//判断文件是否为空
//统计文件中人数
int getnum();
void init();
void delemp();//删除职工信息
int isexist(int id);
void modify();
void search();
void sort();
void clean();
~workermanager();//析构函数
};
接下来是.cpp文件
boss.h
#pragma once
//普通员工头文件子类继承父类
#include"worker.h"
#include<iostream>
#include<string>
using namespace std;
#include"boss.h"
boss::boss(int id, string name, int did)
{
this->m_id = id;
this->m_name = name;
this->m_depaid = did;
}
void boss::showinfo()
{
cout << "职工编号为:" << this->m_id
<< "\t职工姓名:" << this->m_name
<< "\t职工岗位为:" << this->getdepainfo()
<< "\t岗位职责:管理公司" << endl;
}
string boss::getdepainfo()
{
return "老板";
}
employee.cpp
#pragma once;
#include<iostream>
#include<string>
using namespace std;
#include"employee.h"
//构造函数
Employee::Employee(int id, string name,int did)
{
this->m_id = id;
this->m_name = name;
this->m_depaid = did;
}
//显示个人信息
void Employee::showinfo()
{
cout << "职工编号为:" << this->m_id
<< "\t职工姓名:" << this->m_name
<< "\t职工岗位为:" << this->getdepainfo()
<< "\t岗位职责:完成经理分配的任务" << endl;
}
//获取岗位名称
string Employee::getdepainfo()
{
return "员工";
}
manager.cpp
#include<iostream>
#pragma once
#include<string>
#include"worker.h"
#include"manager.h"
manager::manager(int id,string name,int did)
{
this->m_id = id;
this->m_name = name;
this->m_depaid = did;
}
void manager::showinfo()
{
cout << "职工编号为:" << this->m_id
<< "\t职工姓名:" << this->m_name
<< "\t职工岗位为:" << this->getdepainfo()
<< "\t岗位职责:完成老板分配的任务" << endl;
}
string manager::getdepainfo()
{
return "经理";
}
workerManager.cpp
#include"workerManger.h"
#include"boss.h"
#include"employee.h"
#include"manager.h"
#include<fstream>
#define FILENAME "EMPFILE.txt"
workermanager::workermanager()//构造函数类外实现
{
//文件不存在
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (!ifs.is_open())
{
cout << "文件不存在" << endl;
this->m_empnum = 0;
this->m_emparray = NULL;
this->m_fileisemp = true;
ifs.close();
return;
}
//文件存在,但是没有信息
char ch;
ifs >> ch;
if (ifs.eof())
{
cout << "文件夹为空" << endl;
this->m_empnum = 0;
this->m_emparray = NULL;
this->m_fileisemp = true;
ifs.close();
return;
}
//当文件存在并且有数据
int num = this->getnum();
cout << "职工人数为:" << num << endl;
this->m_empnum = num;
this->m_emparray = new worker * [this->m_empnum];
this->init();
//先开辟空间将文件中的数据存到数组中
for (int i = 0; i < this->m_empnum; i++)
{
cout << "职工编号为" << this->m_emparray[i]->m_id
<< "姓名为:" << this->m_emparray[i]->m_name
<< "部门编号:" << this->m_emparray[i]->m_depaid << endl;
}
}
void workermanager::delemp()
{
if (this->m_fileisemp)
{
cout << "文件不存在或者为空" << endl;
}
else {
cout << "请输入要删除的职工编号" << endl;
int id = 0;
cin >> id;
int index = this->isexist(id);
if (index != -1)
{
for (int i = index; i < this->m_empnum-1; i++)
{
this->m_emparray[i] = this->m_emparray[i + 1];
}
this->m_empnum--;
this->save();
cout << "删除成功" << endl;
}
else
cout << "未找到该职工" << endl;
}
system("pause");
system("cls");
}
void workermanager::modify()
{
if (this->m_fileisemp)
{
cout << "文件不存在或者为空" << endl;
}
cout << "请输入要修改职工的编号" << endl;
int id;
cin >> id;
int t = this->isexist(id);
if (t != -1)
{
delete this->m_emparray[t];
int newid = 0;
string newname = "";
int ds = 0;
cout << "查询到id为" << t << "号职工,请输入新职工号:" << endl;
cin >> newid;
cout << "请输入新姓名" << endl;
cin >> newname;
cout << "请输入岗位:" << endl << "1,普通员工\n2.经理\n3.老板\n";
cin >> ds;
worker* worker = NULL;
switch (ds)
{
case 1:worker = new Employee(newid, newname, ds);
break;
case 2:
worker = new manager(newid, newname, ds);
break;
case 3:worker = new boss(newid, newname, ds);
break;
default:
break;
}
this->m_emparray[t] = worker;
cout << "修改成功" << endl;
this->save();
}
else
cout << "查无此人" << endl;
system("pause");
system("cls");
}
int workermanager::isexist(int id)
{
int index = -1;
for (int i = 0; i < this->m_empnum; i++)
{
if (this->m_emparray[i]->m_id == id)
{
index = i;
break;
}
}
return index;
}
void workermanager::show()
{
if (this->m_fileisemp)
{
cout << "文件不存在或者记录为空" << endl;
}
else
{
for (int i = 0; i < m_empnum; i++)
{
//利用多态调用接口
this->m_emparray[i]->showinfo();
}
}
system("pause");
system("cls");
}
int workermanager::getnum()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int did;
int num = 0;
while (ifs >> id && ifs >> name && ifs >> did)
{
num++;
}
return num;
}
void workermanager::showmenu()//菜单函数类外实现
{
cout << "******** 欢迎使用职工管理系统!********" << endl;
cout << "********0:退出职工管理系统 ***********" << endl;
cout << "********1.增加职工个信息 **************" << endl;
cout << "********2.打印职工信息 ****************" << endl;
cout << "********3.删除离职职工 ****************" << endl;
cout << "********4.修改职工信息 ****************" << endl;
cout << "********5.查找职工信息 ****************" << endl;
cout << "********6.按照职工号排序 **************" << endl;
cout << "********7.清空所有文档 ****************" << endl;
cout << "***************************************" << endl;
cout << endl;
}
void workermanager::exitsystem()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);
}
void workermanager::addemp()
{
cout << "请输入添加职工人数:\n";
int num;
cin >> num;
if (num > 0)
{
//计算添加新空间大小
int newsize = this->m_empnum + num;//原来的加上新的
//开辟新空间
worker **newspace= new worker* [newsize];
//原来空间中的内容拷贝到新空间
if (this->m_emparray != NULL)
{
for (int i = 0; i < this->m_empnum; i++)
{
newspace[i] = this->m_emparray[i];
}
}
//批量添加新数据
for (int i = 0; i < num; i++)
{
int id;
string name;
int ds;
cout << "请输入第" << i + 1 << "个新职工编号:\n";
cin >> id;
cout << "请输入第" << i + 1 << "个新职工姓名:\n";
cin >> name;
cout << "请选择该职工岗位:\n";
cout << "1,普通员工" << endl;
cout << "2,经理" << endl;
cout << "3,老板" << endl;
cin >> ds;
worker* worker = NULL;
switch (ds)
{
case 1:
worker = new Employee(id, name, 1);
break;
case 2:
worker = new manager(id, name, 2);
break;
case 3:
worker = new boss(id, name, 3);
break;
default:
break;
}
//将创建的职工职责,保存到数组中
newspace[this->m_empnum + i] = worker;
//释放原有空间
delete[]this->m_emparray;
//更改新空间指向
this->m_emparray = newspace;
//更新新的职工人数
this->m_empnum = newsize;
//更新职工不为空的标志
this->m_fileisemp = false;
//提示成功添加
cout << "成功添加" << num << "个新职工" << endl;
this->save();
}
}
else
cout << "输入数据有误\n";
system("pause");
system("cls");
}
void workermanager::search()
{
if (this->m_fileisemp)
cout << "文件为空或者不存在" << endl;
int id;
int did;
cout << "请输入查询者的职工号\n";
cin >> id;
int t = this->isexist(id);
if (t != -1)
{
cout << "职工号为" << id << "的信息\n";
cout << "职工号为:" << this->m_emparray[t]->m_id << "职工名为:" << this->m_emparray[t]->m_name << "部门编号为:" << this->m_emparray[t]->m_depaid << endl;
}
else
cout << "查无此人\n";
}
void workermanager::save()
{
ofstream ofs;
ofs.open(FILENAME, ios::out);//用写方式打开文件
for (int i = 0; i < this->m_empnum; i++)
{
ofs << this->m_emparray[i]->m_id << " "
<< this->m_emparray[i]->m_name << " "
<< this->m_emparray[i]->m_depaid << endl;
}
//关闭文件
ofs.close();
}
void workermanager::sort()
{
if (this->m_fileisemp)
{
cout << "文件不存在或者为空,无法排序" << endl;
system("pause");
system("cls");
}
int id;
string name;
int did;
for (int i = 0; i < this->m_empnum; i++)
{
for (int j = 0; j < i; j++)
{
if (this->m_emparray[i]->m_id > this->m_emparray[j]->m_id)
{
id = this->m_emparray[j]->m_id;
name = this->m_emparray[j]->m_name;
did = this->m_emparray[j]->m_depaid;
this->m_emparray[j]->m_id = this->m_emparray[i]->m_id;
this->m_emparray[j]->m_name = this->m_emparray[i]->m_name;
this->m_emparray[j]->m_depaid = this->m_emparray[i]->m_depaid;
this->m_emparray[i]->m_id = id;
this->m_emparray[i]->m_name = name;
this->m_emparray[i]->m_depaid =did;
}
}
}
system("pause");
system("cls");
}
void workermanager::clean()
{
ofstream ofs(FILENAME, ios::trunc);
ofs.close();
if (this->m_emparray != NULL)
{
for (int i = 0; i < this->m_empnum; i++)
{
delete this->m_emparray[i];
this->m_emparray[i] = NULL;
}
//删除堆区数组指针
delete[]this->m_emparray;
this->m_empnum = 0;
this->m_fileisemp = 1;
}
cout << "清空成功\n";
system("pause");
system("cls");
}
void workermanager::init()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int did;
int index = 0;
while (ifs >> id && ifs >> name && ifs >> did)
{
worker* worker = NULL;
switch (did)
{
case 1:
worker = new Employee(id, name, did);
break;
case 2:
worker = new manager(id, name, did);
break;
case 3:
worker = new boss(id, name, did);
break;
default:
break;
}
this->m_emparray[index] = worker;
index++;
}
ifs.close();
}
workermanager::~workermanager() //析构函数
{
if (this->m_emparray != NULL)
{
delete[] this->m_emparray;
this->m_emparray = NULL;
}//堆区数据手动开辟,手动释放
}
职工管理系统.cpp
#pragma once
#include<iostream>
#include"workerManger.h"
#include"worker.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"
#include<string>
#include<fstream>
#define FILENAME "EMPFILE.txt"
using namespace std;
int main()
{ //实例化管理者对象
workermanager wss;//管理员王顺顺
int choice = 0;
while (true)
{
wss.showmenu();
cout << "请输入您的选择" << endl;
cin >> choice;
switch (choice)
{
case 0:wss.exitsystem();//推出系统
break;
case 1://添加职工
wss.addemp();
break;
case 2://打印职工信息
wss.show();
break;
case 3://删除职工信息
wss.delemp();
break;
case 4:
wss.modify();//修改职工信息
break;
case 5:
wss.search();//查找职工信息
break;
case 6:
wss.sort();//排序职工信息
break;
case 7:
wss.clean();//清空文件
break;
default:
system("cls");
break;
}
}
system("pause");
return 0;
}
.txt文件:
1 张三 2
2 李四 3
4 王五 1
把这三行数据复制粘贴到一个txt文件中(保存这个文件的时候用ANSI编码格式)
以上的文件都粘贴到一个项目中,就可以运行了。