实现:职工管理系统
实现员工类:Boss、Employee、Manager,都实现抽象类Worker
worker.h头文件:
#pragma once #include<iostream> #include<string> using namespace std; //职工抽象基类 class Worker { public: //显示个人信息 virtual void showInfo() = 0; //获取岗位名称 virtual string getDeptName() = 0; int m_Id; //职工编号 string m_Name; //职工姓名 int m_DeptId; //职工所在部门名称编号 };
老板类boss.h头文件
#pragma once #include<iostream> using namespace std; #include "worker.h" //老板类 class Boss :public Worker { public: Boss(int id, string name, int dId); //显示个人信息 void showInfo(); //获取职工岗位名称 string getDeptName(); };
Boss.cpp文件的实现
#include "boss.h" Boss::Boss(int id, string name, int dId) { this->m_Id = id; this->m_Name = name; this->m_DeptId = dId; } void Boss::showInfo() { cout << "职工编号: " << this->m_Id << " \t职工姓名: " << this->m_Name << " \t岗位:" << this->getDeptName() << " \t岗位职责:管理公司所有事务" << endl; } string Boss::getDeptName() { return string("总裁"); }
员工类employee.h头文件
#pragma once #include<iostream> using namespace std; #include "worker.h" //员工类 class Employee :public Worker { public: //构造函数 Employee(int id, string name, int dId); //显示个人信息 void showInfo(); //获取职工岗位名称 string getDeptName(); };
employee.cpp文件的实现:
#include "employee.h" Employee::Employee(int id, string name, int dId) { this->m_Id = id; this->m_Name = name; this->m_DeptId = dId; } void Employee::showInfo() { cout << "职工编号: " << this->m_Id << " \t职工姓名: " << this->m_Name << " \t岗位:" << this->getDeptName() << " \t岗位职责:完成经理交给的任务" << endl; } string Employee::getDeptName() { return string("员工"); }
经理类manager.h头文件
#pragma once #include<iostream> using namespace std; #include "worker.h" //经理类 class Manager :public Worker { public: Manager(int id, string name, int dId); //显示个人信息 void showInfo(); //获取职工岗位名称 string getDeptName(); };
manager.cpp文件的实现:
#include "manager.h" Manager::Manager(int id, string name, int dId) { this->m_Id = id; this->m_Name = name; this->m_DeptId = dId; } void Manager::showInfo() { cout << "职工编号: " << this->m_Id << " \t职工姓名: " << this->m_Name << " \t岗位:" << this->getDeptName() << " \t岗位职责:完成老板交给的任务,并下发任务给员工" << endl; } string Manager::getDeptName() { return string("经理"); }
workManager.h头文件:进行函数的申明
#pragma once #include <iostream> #include "worker.h" #include "employee.h" #include "manager.h" #include "boss.h" using namespace std; // .h文件进行声明,相应的cpp文件进行定义 class workManager { public: int m_Num; //记录文件的人数 Worker ** worker_array; //放置对象的指针 bool fileExist; //判断文件是否存在 workManager(); //构造函数 ~workManager(); //析构函数 void Show_Menu(); //展示菜单 void Show_Exit(); //关闭程序功能 void Add_Worker(); //添加员工职位 void fileSave(); //用来保存员工 int getNum(); //统计文本中人数的个数 void to_init(); //如果文本中有内容就需要相应的进行读取文本初始化操作 void to_show(); //展示文本中的信息 void to_del(); //删除职工信息 int to_exist(int bh);//判断相应的职工编号是否存在 返回值为int void to_modify(); //修改指定员工编号操作 void to_find(); //查找指定员工编号职工信息 void to_sort(); //进行员工的排序,升序或者降序 void to_clear(); //清空文本 };
workManager.cpp进行.h文件中函数的实现
#include "workManager.h" //直接包含一个头文件,在workManager.h的头文件中已经包含了另外三个员工类的头文件 #include<fstream> #define FILENAME "worker.txt" workManager::workManager() { this->m_Num = 0; //初始化数量为0 this->worker_array = NULL; //初始化指针为NULL //初始化进行读取文件 //1、第一种情况,文件不存在 ifstream ifs; ifs.open(FILENAME, ios::in); if (!ifs.is_open()) { cout << "文件不存在" << endl; this->fileExist = true; //文件存在的标识符 this->m_Num = 0; this->worker_array = NULL; ifs.close(); return; //跳出构造函数 } //2、第二种情况,文件存在,但是内容为空 char ch; ifs >> ch; //读取一个字节来进行判断是否为空 if (ifs.eof()) { cout << "文件为空" << endl; this->fileExist = true; //文件存在的标识符 this->m_Num = 0; this->worker_array = NULL; ifs.close(); return; //跳出构造函数 } //3、第三种情况,文件存在,内容存在,那么我们就需要读取出来并且生成相应的空间写入到程序中 int num = this->getNum(); //获取初始化的时候文本中的个数 cout << "当前职工人数有 " << num << "人" << endl; this->m_Num = num; // 对m_Num进行更新人数 this->worker_array = new Worker*[m_Num];//在初始化之前我们需要分配给worker_array相应的内存空间进行储存,因为worker_array是指向指针的指针所以我们生成的空间为指针类型 this->to_init(); //进行相应的初始化操作 } void workManager::to_show() { //实现显示职工信息的功能 if (this->fileExist == true) { // //如果文件不存在或者文本中无内容则提示,这里我们可以利用到标识符fileExist cout << "文件不存在或者内容为空" << endl; }else { //如果文件存在并且存在内容的情况下 for (int i = 0; i < this->m_Num; i++) { this->worker_array[i]->showInfo(); } } system("pause"); system("cls"); } void workManager::to_del() { int bh; cout << "请输入你要删除的职工编号" << endl; cin >> bh; int ret = this->to_exist(bh); if (ret != -1) { //职工编号存在的处理 for (; ret < this->m_Num-1; ret++) { //m_Num编号是从0开始的 所以总共是m_Num-1数量 this->worker_array[ret] = this->worker_array[ret + 1]; //进行覆盖操作 } this->m_Num--; //覆盖完 少了一个人数,所以要-1操作 this->fileSave(); //重新保存文件 } else { //职工编号不存在的处理 cout << "职工编号不存在" << endl; } system("pause"); system("cls"); } void workManager::to_modify() { //修改指定员工编号操作 int bh; string name; int dId; cout << "请出你要修改的职工编号" << endl; cin >> bh; int ret = this->to_exist(bh); if (ret != -1) { //先把之前的内存空间给释放掉再重新创建对象 delete this->worker_array[ret]; cout << "输入你要重新修改的职工编号" << endl; cin >> bh; cout << "输入你要重新修改的职工姓名" << endl; cin >> name; cout << "输入你要重新修改的职工岗位" << endl; cout << "1、普通职工" << endl; cout << "2、经理" << endl; cout << "3、老板" << endl; cin >> dId; Worker * worker = NULL; switch (dId) { case 1: worker = new Employee(bh, name, dId); break; case 2: worker = new Manager(bh, name, dId); break; case 3: worker = new Boss(bh, name, dId); break; } this->worker_array[ret] = worker; this->fileSave(); cout << "修改成功" << endl; }else { cout << "修改失败,查无此人" << endl; } system("pause"); system("cls"); } void workManager::to_clear(){ //清空文本 cout << "你确定要清空文本吗?" << endl; int choose; cin >> choose; if (choose == 1) { //cout << "清空" << endl; ofstream ofs(FILENAME, ios::trunc); //打开模式 ios::trunc 如果存在删除文件并重新创建 ofs.close(); //进行关闭文件流的操作 if (this->worker_array != NULL) { for (int i = 0; i < this->m_Num; i++) { delete this->worker_array[i]; //释放worker_array指针中指向的地址的指针的内存空间 } //this->m_Num = 0;可有可无 delete[] this->worker_array; //释放worker_array本身数组指针的内存空间 this->worker_array = NULL; } cout << "清空完成" << endl; } else { cout << "清空操作被取消" << endl; return; } system("pause"); system("cls"); } //排序的操作 void workManager::to_sort() { cout << "请选择排序的顺序:" << endl; int choose; cout << "1、升序排序 2、降序排序" << endl; cin >> choose; if (choose == 1) { //升序排序,利用的排序方法是冒泡排序 for (int i = 0; i < this->m_Num - 1; i++) { Worker * temp = NULL; //作为一个temp的存在 for (int j = i + 1; j < this->m_Num; j++) { if (this->worker_array[i]->m_Id < this->worker_array[j]->m_Id) { temp = this->worker_array[i]; this->worker_array[i] = this->worker_array[j]; this->worker_array[j] = temp; } } } }else { //降序排序 for (int i = 0; i < this->m_Num - 1; i++) { Worker * temp = NULL; //作为一个temp的存在 for (int j = i + 1; j < this->m_Num; j++) { if (this->worker_array[i]->m_Id > this->worker_array[j]->m_Id) { temp = this->worker_array[i]; this->worker_array[i] = this->worker_array[j]; this->worker_array[j] = temp; } } } } system("pause"); system("cls"); } void workManager::to_find() { cout << "请出你要查找的职工编号" << endl; int bh; cin >> bh; int ret = this->to_exist(bh); if (ret != -1) { for (int i = 0; i < this->m_Num; i++) { if (this->worker_array[i]->m_Id == bh) { this->worker_array[i]->showInfo(); break; //如果自己还要添加一个以姓名查找的话,break就不能用了,因为要循环完才行 } } }else { cout << "查找失败,查无此人" << endl; } system("pause"); system("cls"); } void workManager::to_init() { //对文本进行读取然后再加进程序中的函数to_init封装 ifstream ifs; ifs.open(FILENAME,ios::in); int id, dId; string name; this->fileExist = false; int index = 0;//计数 while (ifs >> id && ifs >> name && ifs >> dId) { //获取文本的内容 Worker * worker = NULL; //指针清空初始化 if (dId == 1) { worker = new Employee(id, name, dId); } else if (dId == 2) { worker = new Manager(id, name, dId); } else if (dId == 3) { worker = new Boss(id, name, dId); } this->worker_array[index] = worker; //worker_array为指向worker指针的指针 index++; } } int workManager::getNum() { ifstream ifs; ifs.open(FILENAME, ios::in); int id, dId; string name; int num = 0; while (ifs >> id && ifs >> name && ifs >> dId) { num++; } ifs.close(); return num; } workManager::~workManager() { } void workManager::Show_Menu() { cout << "********************************************" << endl; 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 workManager::Show_Exit() { cout << "欢迎下次使用" << endl; system("pause"); exit(0); } //判断相应的职工编号是否存在 返回值为int int workManager::to_exist(int bh) { //传入的参数为编号 for (int i = 0; i < this->m_Num; i++) { if (this->worker_array[i]->m_Id == bh) { return i; break;//既然已经找到了 那么直接break跳出循环 } } return -1; //如果没有找到就返回为-1 } //写入文件操作 void workManager::fileSave() { //当添加完成之后需要进行保存文件的操作 ofstream ofs;//创建写入的流对象 ofs.open(FILENAME, ios::out); for (int i = 0; i < this->m_Num; i++) { //用m_Num来进行判断,因为m_Num中是存储的职工数量,然后进行遍历写入操作 ofs << this->worker_array[i]->m_Id << " " << this->worker_array[i]->m_Name << " " << this->worker_array[i]->m_DeptId << endl; //ofs进行写入 } ofs.close(); } void workManager::Add_Worker() { //这个地方最重要 也是最要学习的地方,这里的双重指针也需要理解 int add_Num; cout << "请输入你要添加的人数:"<< endl; cin >> add_Num; if (add_Num > 0) { int all = this->m_Num + add_Num; //统计一共要生成的新空间的大小 Worker ** NewSpace = new Worker*[all]; //生成相应的地址空间 if (this->worker_array != NULL) { //如果不为空的话,那要把原来的数据都要先重新转移到NewSpace中,为空的话那么就直接转移 for (int i = 0; i < this->m_Num; i++) { NewSpace[i] = this->worker_array[i]; } } for (int i = 0; i < add_Num; i++) { int id, dId; string name; cout << "请输入第 " << i + 1 << " 个新职工编号:" << endl; cin >> id; cout << "请输入第 " << i + 1 << " 个新职工姓名:" << endl; cin >> name; cout << "请选择该职工的岗位:" << endl; cout << "1、普通职工" << endl; cout << "2、经理" << endl; cout << "3、老板" << endl; cin >> dId; Worker * worker = NULL; if (dId == 1) { //普通职工创建指针 NewSpace[this->m_Num + i] = new Employee(id, name, dId); }else if (dId == 2) { //经理创建指针 NewSpace[this->m_Num + i] = new Manager(id, name, dId); }else if (dId == 3) { //老板创建指针 NewSpace[this->m_Num + i] = new Boss(id, name, dId); } } delete[] this->worker_array; //进行释放之前储存的老空间 this->worker_array = NewSpace; //把之前的旧的空间释放然后指向最新保存的地址空间 this->m_Num = all; //修改最新总共人数 cout << "添加成功" << add_Num << "新职工!" << endl; } else { cout << "输入有误" << endl; } this->fileSave(); system("pause"); //任意键 system("cls"); }
入口文件
职工管理系统.cpp
#include<iostream> #include<string> #include "workManager.h" using namespace std; int main() { workManager w1; //实例化对象 while (true) { w1.Show_Menu(); int choose; cout << "输入你要选择的选项:" << endl; cin >> choose; switch (choose) { case 0: //0.退出管理程序 w1.Show_Exit(); break; case 1: //1.增加职工信息 w1.Add_Worker(); break; case 2: //2.显示职工信息 w1.to_show(); break; case 3: //3.删除离职职工 w1.to_del(); break; case 4: //4.修改职工信息 w1.to_modify(); break; case 5: //5.查找职工信息 w1.to_find(); break; case 6: //6.按照编号排序 w1.to_sort(); break; case 7: //7.清空所有文档 w1.to_clear(); break; } } system("pause"); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY