C++利用多態(tài)實(shí)現(xiàn)職工管理系統(tǒng)(項(xiàng)目開發(fā))
分析
首先看一下這個(gè)項(xiàng)目的文件:
主要分為兩部分:
主體部分:
main.cpp和workManager.h,workManager.cpp職工部分(這里采用多態(tài)的方式編寫):
主要是worker.h和worker.cpp
三種職位:boss,employee,manager
經(jīng)過分析是否大概知道了其中各部分的意思呢?
看起來這里面有很多,但是正是這種多個(gè)文件編寫才時(shí)代碼更加簡(jiǎn)潔。
所以在正式寫項(xiàng)目之前一定要先考慮好整體架構(gòu),在進(jìn)行編寫。
項(xiàng)目整體架構(gòu):
這個(gè)項(xiàng)目的難度并不大,主要是要學(xué)會(huì)這個(gè)項(xiàng)目整體架構(gòu)是怎樣安排的。
左邊是主體部分。
主要是整個(gè)項(xiàng)目的功能,將主界面的所有功能通過代碼和右邊的不同人之間進(jìn)行實(shí)現(xiàn)。
包含了上面幾個(gè)功能函數(shù),還有一些沒有寫出來
右邊則是三種職工的信息。
每個(gè)人都有自己的姓名,編號(hào),職位,然后包含兩個(gè)函數(shù),顯示個(gè)人信息和獲取崗位信息。
主要是通過數(shù)組進(jìn)行存儲(chǔ)的
其它的也沒什么需要過多介紹的,主要是要理解上面的整體架構(gòu)圖,詳細(xì)的代碼編寫并不算復(fù)雜
部分運(yùn)行截圖
代碼
main.cpp
#include "workManager.h" //構(gòu)造函數(shù) WorkerManage::WorkerManage() { ifstream ifs; ifs.open(FILENAME, ios::in); //讀文件 //1,文件不存在 if (!ifs.is_open()) { cout << "文件不存在" << endl; //初始化屬性 //初始化記錄人數(shù) this->m_EmpNum = 0; //初始化數(shù)組指針 this->m_EmpArray = NULL; //初始化文件是否為空 this->m_FileIsEmpty = true; ifs.close(); return; } //2,文件存在,數(shù)據(jù)為空 char ch; ifs >> ch; if (ifs.eof()) { //文件為空 cout << "文件為空!" << endl; //初始化屬性 //初始化記錄人數(shù) this->m_EmpNum = 0; //初始化數(shù)組指針 this->m_EmpArray = NULL; //初始化文件是否為空 this->m_FileIsEmpty = true; ifs.close(); return; } //3,文件存在,并且記錄數(shù)據(jù) int num = this->get_EmpNum(); //cout << "職工人數(shù)為:" << num << endl; this->m_EmpNum = num; //開辟空間 this->m_EmpArray = new Worker *[this->m_EmpNum]; //將文件中數(shù)據(jù),存到數(shù)組中 this->init_Emp(); } //展示菜單 void WorkerManage::Show_Menu() { cout << "*********************************" << endl; cout << "*** 歡迎使用教職工管理系統(tǒng)! ***" << endl; cout << "******* 0,退出管理系統(tǒng) *******" << endl; cout << "******* 1,添加職工信息 *******" << endl; cout << "******* 2,顯示職工信息 *******" << endl; cout << "******* 3,刪除離職職工 *******" << endl; cout << "******* 4,修改職工信息 *******" << endl; cout << "******* 5,查找職工信息 *******" << endl; cout << "******* 6,按照編號(hào)排序 *******" << endl; cout << "******* 7,清空所有文檔 *******" << endl; cout << "*********************************" << endl; } //退出系統(tǒng) void WorkerManage::ExitSystem() { cout << "歡迎下次使用" << endl; system("pause"); exit(0); //退出程序 } //析構(gòu)函數(shù) WorkerManage::~WorkerManage() { } //添加職工 void WorkerManage::Add_Emp() { cout << "請(qǐng)輸入添加職工數(shù)量:" << endl; int addNum = 0; cin >> addNum; if (addNum > 0) { //添加 //計(jì)算添加新空間大小 int newSize = this->m_EmpNum + addNum; //新空間人數(shù) = 原來記錄人數(shù)+新增人數(shù) //開辟新空間 Worker ** newSpace = new Worker*[newSize]; //將原來空間下數(shù)據(jù),拷貝到新空間下 if (this->m_EmpArray != NULL) { for (int i = 0; i < this->m_EmpNum; i++) { newSpace[i] = this->m_EmpArray[i]; } } //添加新數(shù)據(jù) for (int i = 0; i < addNum; i++) { int id; //職工編號(hào) string name; //職工編號(hào) int dSelect; //部門選擇 cout << "請(qǐng)輸入第" << i + 1 << "個(gè)新職工編號(hào):" << endl; cin >> id; cout << "請(qǐng)輸入第" << i + 1 << "個(gè)新職工姓名:" << endl; cin >> name; cout << "請(qǐng)選擇該職工崗位:" << endl; cout << "1,普通職工" << endl; cout << "2,經(jīng)理" << endl; cout << "3,老板" << endl; cin >> dSelect; Worker *worker = NULL; switch (dSelect) { 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; } //將創(chuàng)建職工職責(zé),保存到數(shù)組中 newSpace[this->m_EmpNum++] = worker; } //釋放原有的空間 delete[] this->m_EmpArray; //更改新空間指向 this->m_EmpArray = newSpace; //更新新的職工人數(shù) this->m_EmpNum = newSize; //更新職工不為空標(biāo)志 this->m_FileIsEmpty = false; //添加成功 cout << "成功添加" << addNum << "名新職工" << endl; //保存數(shù)據(jù)到文件中 this->save(); } else { cout << "輸入數(shù)據(jù)有誤" << endl; } system("pause"); system("cls"); } //保存文件 void WorkerManage::save() { ofstream ofs; ofs.open(FILENAME, ios::out); //用輸入的方式打開文件 -- 寫文件 //將每個(gè)人數(shù)據(jù)寫入到文件中 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_DeptId << endl; } //關(guān)閉文件 ofs.close(); } //統(tǒng)計(jì)文件人數(shù) int WorkerManage::get_EmpNum() { ifstream ifs; ifs.open(FILENAME, ios::in); //打開文件 讀 int id; string name; int dId; //人數(shù) int num = 0; while (ifs >> id&& ifs >> name && ifs >> dId) { num++; } return num; } //初始化職工 void WorkerManage::init_Emp() { ifstream ifs; ifs.open(FILENAME, ios::in); //打開文件 讀 int id; string name; int dId; int index = 0; //讀數(shù)據(jù) 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++; } this->m_FileIsEmpty = false; //關(guān)閉文件 ifs.close(); } //顯示職工 void WorkerManage::show_Emp() { //判斷文件是否為空 if (this->m_FileIsEmpty) { cout << "文件不存在或者記錄為空" << endl; } else { for (int i = 0; i < m_EmpNum; i++) { //利用多態(tài)調(diào)用程序端口 this->m_EmpArray[i]->showInfo(); } } //按任意鍵后清屏 system("pause"); system("cls"); } //刪除職工 void WorkerManage::Del_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或者記錄為空" << endl; } else { //按照職工編號(hào)刪除 cout << "請(qǐng)輸入想要?jiǎng)h除職工編號(hào):" << endl; int id = 0; cin >> id; int index = this->IsExist(id); if (index != -1) { //數(shù)據(jù)前移 for (int i = index; i < this->m_EmpNum; i++) { this->m_EmpArray[i] = this->m_EmpArray[i + 1]; } this->m_EmpNum--; //更新數(shù)組中記錄人員個(gè)數(shù) //數(shù)據(jù)更新到文件中 this->save(); cout << "刪除成功" << endl; } else { cout << "刪除失敗,未找到該職工" << endl; } } system("pause"); system("cls"); } //判斷職工是否存在,如果存在返回職工所在數(shù)組中的位置,不存在返回-1 int WorkerManage::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 WorkerManage::Mod_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或記錄為空" << endl; } else { cout << "修改職工編號(hào):" << endl; int id; cin >> id; int ret = this->IsExist(id); if (ret != -1) { //查找到編號(hào)的職工 delete this->m_EmpArray[ret]; int newId = 0; string newName = ""; int dSelect = 0; cout << "查到:" << id << "號(hào)職工,請(qǐng)輸入新職工號(hào):" << endl; cin >> newId; cout << "請(qǐng)輸入新名字:" << endl; cin >> newName; cout << "請(qǐng)輸入崗位:" << endl; cout << "1,普通職工" << endl; cout << "2,經(jīng)理" << endl; cout << "3,老板" << endl; cin >> dSelect; Worker *worker = NULL; switch (dSelect) { case 1: worker = new Employee(newId, newName, dSelect); break; case 2: worker = new Manager(newId, newName, dSelect); break; case 3: worker = new Boss(newId, newName, dSelect); break; default: break; } //更新數(shù)據(jù) 到數(shù)組中 this->m_EmpArray[ret] = worker; cout << "修改成功!" << endl; //保存到文件中 this->save(); } else { cout << "修改失敗,查無此人" << endl; } } system("pause"); system("cls"); } //查找職工 void WorkerManage::Find_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或記錄為空!" << endl; } else { cout << "請(qǐng)輸入查找的方式:" << endl; cout << "1,按照職工的編號(hào)查找" << endl; cout << "2,按照職工的姓名查找" << endl; int select = 0; cin >> select; if (select == 1) { //按照編號(hào)查 int id; cout << "請(qǐng)輸入查找的職工編號(hào):" << endl; cin >> id; int ret = IsExist(id); if (ret != -1) { //找到職工 cout << "查找成功!該職工信息如下!" << endl; this->m_EmpArray[ret]->showInfo(); } else { cout << "查無此人" << endl; } } else if (select == 2) { //按照姓名查 string name; cout << "請(qǐng)輸入查找的姓名:" << endl; cin >> name; //加入是否查到標(biāo)志 bool flag = false; //默認(rèn)未找到 for (int i = 0; i < m_EmpNum; i++) { if (this->m_EmpArray[i]->m_Name == name) { cout << "查找成功,職工編號(hào)為:" << this->m_EmpArray[i]->m_Id << "號(hào)職工信息如下:" << endl; flag = true; this->m_EmpArray[i]->showInfo(); } } if (flag == false) { cout << "查無此人" << endl; } } else { cout << "輸入選項(xiàng)有誤!" << endl; } } system("pause"); system("cls"); } //按照編號(hào)排序 void WorkerManage::Sort_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或記錄為空!" << endl; system("pause"); system("cls"); } else { cout << "請(qǐng)輸入排序的方式:" << endl; cout << "1,按照職工的編號(hào)升序" << endl; cout << "2,按照職工的編號(hào)降序" << endl; int select = 0; cin >> select; for (int i = 0; i < m_EmpNum; i++) { int minOrMax = i; //聲明最小值 for (int j = i + 1; j < this->m_EmpNum; j++) { if (select == 1) //升序 { if (this->m_EmpArray[minOrMax]->m_Id > this->m_EmpArray[j]->m_Id) { minOrMax = j; } } else //降序 { if (this->m_EmpArray[minOrMax]->m_Id < this->m_EmpArray[j]->m_Id) { minOrMax = j; } } } //判斷一開始認(rèn)定的最大值或最小值 if (i != minOrMax) { Worker * temp = this->m_EmpArray[i]; this->m_EmpArray[i] = this->m_EmpArray[minOrMax]; this->m_EmpArray[minOrMax] = temp; } } cout << "排序成功!" << endl; this->save(); //排序結(jié)果保存 this->show_Emp(); } } //清空文件 void WorkerManage::Clean_File() { cout << "確定清空?" << endl; cout << "1,確定" << endl; cout << "2,返回" << endl; int select = 0; cin >> select; if (select == 1) { //清空文件 ofstream ofs(FILENAME, ios::trunc); //刪除文件后重新創(chuàng)建 ofs.close(); if (this->m_EmpArray != NULL) { //刪除堆區(qū)的每個(gè)職工對(duì)象 for (int i = 0; i < this->m_EmpNum; i++) { delete this->m_EmpArray[i]; this->m_EmpArray[i] = NULL; } //刪除堆區(qū)數(shù)組指針 delete[] this->m_EmpArray; this->m_EmpArray = NULL; this->m_EmpNum = 0; this->m_FileIsEmpty = true; } cout << "清空成功" << endl; } system("pause"); system("cls"); }
workManager.h
#include<iostream> #include<fstream> using namespace std; #include"worker.h" #include"employee.h" #include"manager.h" #include"boss.h" #define FILENAME "empFile.txt" class WorkerManage { public: //構(gòu)造函數(shù) WorkerManage(); //展示菜單 void Show_Menu(); //退出系統(tǒng) void ExitSystem(); //記錄職工人數(shù) int m_EmpNum; //職工數(shù)組指針 Worker ** m_EmpArray; //添加職工 void Add_Emp(); //保存文件 void save(); //判斷文件是否為空 bool m_FileIsEmpty; //統(tǒng)計(jì)文件人數(shù) int get_EmpNum(); //初始化職工 void init_Emp(); //顯示職工 void show_Emp(); //刪除職工 void Del_Emp(); //判斷職工是否存在,如果存在返回職工所在數(shù)組中的位置,不存在返回-1 int IsExist(int id); //修改職工 void Mod_Emp(); //查找職工 void Find_Emp(); //按照編號(hào)排序 void Sort_Emp(); //清空文件 void Clean_File(); //析構(gòu)函數(shù) ~WorkerManage(); };
workManager.cpp
#include "workManager.h" //構(gòu)造函數(shù) WorkerManage::WorkerManage() { ifstream ifs; ifs.open(FILENAME, ios::in); //讀文件 //1,文件不存在 if (!ifs.is_open()) { cout << "文件不存在" << endl; //初始化屬性 //初始化記錄人數(shù) this->m_EmpNum = 0; //初始化數(shù)組指針 this->m_EmpArray = NULL; //初始化文件是否為空 this->m_FileIsEmpty = true; ifs.close(); return; } //2,文件存在,數(shù)據(jù)為空 char ch; ifs >> ch; if (ifs.eof()) { //文件為空 cout << "文件為空!" << endl; //初始化屬性 //初始化記錄人數(shù) this->m_EmpNum = 0; //初始化數(shù)組指針 this->m_EmpArray = NULL; //初始化文件是否為空 this->m_FileIsEmpty = true; ifs.close(); return; } //3,文件存在,并且記錄數(shù)據(jù) int num = this->get_EmpNum(); //cout << "職工人數(shù)為:" << num << endl; this->m_EmpNum = num; //開辟空間 this->m_EmpArray = new Worker *[this->m_EmpNum]; //將文件中數(shù)據(jù),存到數(shù)組中 this->init_Emp(); } //展示菜單 void WorkerManage::Show_Menu() { cout << "*********************************" << endl; cout << "*** 歡迎使用教職工管理系統(tǒng)! ***" << endl; cout << "******* 0,退出管理系統(tǒng) *******" << endl; cout << "******* 1,添加職工信息 *******" << endl; cout << "******* 2,顯示職工信息 *******" << endl; cout << "******* 3,刪除離職職工 *******" << endl; cout << "******* 4,修改職工信息 *******" << endl; cout << "******* 5,查找職工信息 *******" << endl; cout << "******* 6,按照編號(hào)排序 *******" << endl; cout << "******* 7,清空所有文檔 *******" << endl; cout << "*********************************" << endl; } //退出系統(tǒng) void WorkerManage::ExitSystem() { cout << "歡迎下次使用" << endl; system("pause"); exit(0); //退出程序 } //析構(gòu)函數(shù) WorkerManage::~WorkerManage() { } //添加職工 void WorkerManage::Add_Emp() { cout << "請(qǐng)輸入添加職工數(shù)量:" << endl; int addNum = 0; cin >> addNum; if (addNum > 0) { //添加 //計(jì)算添加新空間大小 int newSize = this->m_EmpNum + addNum; //新空間人數(shù) = 原來記錄人數(shù)+新增人數(shù) //開辟新空間 Worker ** newSpace = new Worker*[newSize]; //將原來空間下數(shù)據(jù),拷貝到新空間下 if (this->m_EmpArray != NULL) { for (int i = 0; i < this->m_EmpNum; i++) { newSpace[i] = this->m_EmpArray[i]; } } //添加新數(shù)據(jù) for (int i = 0; i < addNum; i++) { int id; //職工編號(hào) string name; //職工編號(hào) int dSelect; //部門選擇 cout << "請(qǐng)輸入第" << i + 1 << "個(gè)新職工編號(hào):" << endl; cin >> id; cout << "請(qǐng)輸入第" << i + 1 << "個(gè)新職工姓名:" << endl; cin >> name; cout << "請(qǐng)選擇該職工崗位:" << endl; cout << "1,普通職工" << endl; cout << "2,經(jīng)理" << endl; cout << "3,老板" << endl; cin >> dSelect; Worker *worker = NULL; switch (dSelect) { 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; } //將創(chuàng)建職工職責(zé),保存到數(shù)組中 newSpace[this->m_EmpNum++] = worker; } //釋放原有的空間 delete[] this->m_EmpArray; //更改新空間指向 this->m_EmpArray = newSpace; //更新新的職工人數(shù) this->m_EmpNum = newSize; //更新職工不為空標(biāo)志 this->m_FileIsEmpty = false; //添加成功 cout << "成功添加" << addNum << "名新職工" << endl; //保存數(shù)據(jù)到文件中 this->save(); } else { cout << "輸入數(shù)據(jù)有誤" << endl; } system("pause"); system("cls"); } //保存文件 void WorkerManage::save() { ofstream ofs; ofs.open(FILENAME, ios::out); //用輸入的方式打開文件 -- 寫文件 //將每個(gè)人數(shù)據(jù)寫入到文件中 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_DeptId << endl; } //關(guān)閉文件 ofs.close(); } //統(tǒng)計(jì)文件人數(shù) int WorkerManage::get_EmpNum() { ifstream ifs; ifs.open(FILENAME, ios::in); //打開文件 讀 int id; string name; int dId; //人數(shù) int num = 0; while (ifs >> id&& ifs >> name && ifs >> dId) { num++; } return num; } //初始化職工 void WorkerManage::init_Emp() { ifstream ifs; ifs.open(FILENAME, ios::in); //打開文件 讀 int id; string name; int dId; int index = 0; //讀數(shù)據(jù) 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++; } this->m_FileIsEmpty = false; //關(guān)閉文件 ifs.close(); } //顯示職工 void WorkerManage::show_Emp() { //判斷文件是否為空 if (this->m_FileIsEmpty) { cout << "文件不存在或者記錄為空" << endl; } else { for (int i = 0; i < m_EmpNum; i++) { //利用多態(tài)調(diào)用程序端口 this->m_EmpArray[i]->showInfo(); } } //按任意鍵后清屏 system("pause"); system("cls"); } //刪除職工 void WorkerManage::Del_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或者記錄為空" << endl; } else { //按照職工編號(hào)刪除 cout << "請(qǐng)輸入想要?jiǎng)h除職工編號(hào):" << endl; int id = 0; cin >> id; int index = this->IsExist(id); if (index != -1) { //數(shù)據(jù)前移 for (int i = index; i < this->m_EmpNum; i++) { this->m_EmpArray[i] = this->m_EmpArray[i + 1]; } this->m_EmpNum--; //更新數(shù)組中記錄人員個(gè)數(shù) //數(shù)據(jù)更新到文件中 this->save(); cout << "刪除成功" << endl; } else { cout << "刪除失敗,未找到該職工" << endl; } } system("pause"); system("cls"); } //判斷職工是否存在,如果存在返回職工所在數(shù)組中的位置,不存在返回-1 int WorkerManage::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 WorkerManage::Mod_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或記錄為空" << endl; } else { cout << "修改職工編號(hào):" << endl; int id; cin >> id; int ret = this->IsExist(id); if (ret != -1) { //查找到編號(hào)的職工 delete this->m_EmpArray[ret]; int newId = 0; string newName = ""; int dSelect = 0; cout << "查到:" << id << "號(hào)職工,請(qǐng)輸入新職工號(hào):" << endl; cin >> newId; cout << "請(qǐng)輸入新名字:" << endl; cin >> newName; cout << "請(qǐng)輸入崗位:" << endl; cout << "1,普通職工" << endl; cout << "2,經(jīng)理" << endl; cout << "3,老板" << endl; cin >> dSelect; Worker *worker = NULL; switch (dSelect) { case 1: worker = new Employee(newId, newName, dSelect); break; case 2: worker = new Manager(newId, newName, dSelect); break; case 3: worker = new Boss(newId, newName, dSelect); break; default: break; } //更新數(shù)據(jù) 到數(shù)組中 this->m_EmpArray[ret] = worker; cout << "修改成功!" << endl; //保存到文件中 this->save(); } else { cout << "修改失敗,查無此人" << endl; } } system("pause"); system("cls"); } //查找職工 void WorkerManage::Find_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或記錄為空!" << endl; } else { cout << "請(qǐng)輸入查找的方式:" << endl; cout << "1,按照職工的編號(hào)查找" << endl; cout << "2,按照職工的姓名查找" << endl; int select = 0; cin >> select; if (select == 1) { //按照編號(hào)查 int id; cout << "請(qǐng)輸入查找的職工編號(hào):" << endl; cin >> id; int ret = IsExist(id); if (ret != -1) { //找到職工 cout << "查找成功!該職工信息如下!" << endl; this->m_EmpArray[ret]->showInfo(); } else { cout << "查無此人" << endl; } } else if (select == 2) { //按照姓名查 string name; cout << "請(qǐng)輸入查找的姓名:" << endl; cin >> name; //加入是否查到標(biāo)志 bool flag = false; //默認(rèn)未找到 for (int i = 0; i < m_EmpNum; i++) { if (this->m_EmpArray[i]->m_Name == name) { cout << "查找成功,職工編號(hào)為:" << this->m_EmpArray[i]->m_Id << "號(hào)職工信息如下:" << endl; flag = true; this->m_EmpArray[i]->showInfo(); } } if (flag == false) { cout << "查無此人" << endl; } } else { cout << "輸入選項(xiàng)有誤!" << endl; } } system("pause"); system("cls"); } //按照編號(hào)排序 void WorkerManage::Sort_Emp() { if (this->m_FileIsEmpty) { cout << "文件不存在或記錄為空!" << endl; system("pause"); system("cls"); } else { cout << "請(qǐng)輸入排序的方式:" << endl; cout << "1,按照職工的編號(hào)升序" << endl; cout << "2,按照職工的編號(hào)降序" << endl; int select = 0; cin >> select; for (int i = 0; i < m_EmpNum; i++) { int minOrMax = i; //聲明最小值 for (int j = i + 1; j < this->m_EmpNum; j++) { if (select == 1) //升序 { if (this->m_EmpArray[minOrMax]->m_Id > this->m_EmpArray[j]->m_Id) { minOrMax = j; } } else //降序 { if (this->m_EmpArray[minOrMax]->m_Id < this->m_EmpArray[j]->m_Id) { minOrMax = j; } } } //判斷一開始認(rèn)定的最大值或最小值 if (i != minOrMax) { Worker * temp = this->m_EmpArray[i]; this->m_EmpArray[i] = this->m_EmpArray[minOrMax]; this->m_EmpArray[minOrMax] = temp; } } cout << "排序成功!" << endl; this->save(); //排序結(jié)果保存 this->show_Emp(); } } //清空文件 void WorkerManage::Clean_File() { cout << "確定清空?" << endl; cout << "1,確定" << endl; cout << "2,返回" << endl; int select = 0; cin >> select; if (select == 1) { //清空文件 ofstream ofs(FILENAME, ios::trunc); //刪除文件后重新創(chuàng)建 ofs.close(); if (this->m_EmpArray != NULL) { //刪除堆區(qū)的每個(gè)職工對(duì)象 for (int i = 0; i < this->m_EmpNum; i++) { delete this->m_EmpArray[i]; this->m_EmpArray[i] = NULL; } //刪除堆區(qū)數(shù)組指針 delete[] this->m_EmpArray; this->m_EmpArray = NULL; this->m_EmpNum = 0; this->m_FileIsEmpty = true; } cout << "清空成功" << endl; } system("pause"); system("cls"); }
worker.h
#pragma once #include<iostream> using namespace std; #include<string> //職工抽象類 class Worker { public: //顯示個(gè)人信息 virtual void showInfo() = 0; //獲取崗位名稱 virtual string getDeptName() = 0; //職工編號(hào) int m_Id; //職工姓名 string m_Name; //部門編號(hào) 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); //顯示個(gè)人信息 virtual void showInfo(); //獲取崗位名稱 virtual string getDeptName(); };
boss.cpp
#include"boss.h" //構(gòu)造函數(shù) Boss::Boss(int id, string name, int dId) { this->m_Id = id; this->m_Name = name; this->m_DeptId = dId; } //顯示個(gè)人信息 void Boss::showInfo() { cout << "職工編號(hào):" << this->m_Id << "\t職工姓名:" << this->m_Name << "\t崗位:" << this->getDeptName() << "\t崗位職責(zé):管理公司所有事物" << endl; } //獲取崗位名稱 string Boss::getDeptName() { return string("總裁"); }
employee.h
//普通員工文件 #pragma once #include<iostream> #include"worker.h" using namespace std; class Employee:public Worker { public: Employee(int id,string name,int dId); //顯示個(gè)人信息 virtual void showInfo(); //獲取崗位名稱 virtual string getDeptName(); };
employee.cpp
#include"employee.h" //構(gòu)造函數(shù) Employee::Employee(int id, string name, int dId) { this->m_Id = id; this->m_Name = name; this->m_DeptId = dId; } //顯示個(gè)人信息 void Employee::showInfo() { cout << "職工編號(hào):" << this->m_Id << "\t職工姓名:" << this->m_Name << "\t崗位:" << this->getDeptName() <<"\t崗位職責(zé):完成經(jīng)理交給的任務(wù)"<< 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); //顯示個(gè)人信息 virtual void showInfo(); //獲取崗位名稱 virtual string getDeptName(); };
manager.cpp
#include"manager.h" //構(gòu)造函數(shù) Manager::Manager(int id, string name, int dId) { this->m_Id = id; this->m_Name = name; this->m_DeptId = dId; } //顯示個(gè)人信息 void Manager::showInfo() { cout << "職工編號(hào):" << this->m_Id << "\t職工姓名:" << this->m_Name << "\t崗位:" << this->getDeptName() << "\t崗位職責(zé):完成老板交給的任務(wù),并且發(fā)任務(wù)給員工" << endl; } //獲取崗位名稱 string Manager::getDeptName() { return string("經(jīng)理"); }
到此這篇關(guān)于C++利用多態(tài)實(shí)現(xiàn)職工管理系統(tǒng)(項(xiàng)目開發(fā))的文章就介紹到這了,更多相關(guān)C++職工管理系統(tǒng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言指針變量作為函數(shù)參數(shù)的實(shí)現(xiàn)步驟詳解
這篇文章主要介紹了C語言指針變量作為函數(shù)參數(shù)的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02C++代碼和可執(zhí)行程序在x86和arm上的區(qū)別介紹
這篇文章主要介紹了C++代碼和可執(zhí)行程序在x86和arm上的區(qū)別,X86和ARM是占據(jù)CPU市場(chǎng)的兩大處理器,各有優(yōu)劣,本文給大家詳細(xì)介紹了兩者的區(qū)別,需要的朋友可以參考下2022-07-07關(guān)于C++智能指針shared_ptr和unique_ptr能否互轉(zhuǎn)問題
C++中的智能指針最常用的是shared_ptr和unique_ptr,C++新手最常問的問題是我從一個(gè)函數(shù)中拿到unique_ptr,但要轉(zhuǎn)成shared_ptr才能使用,要怎么轉(zhuǎn)換?同理是否能將shared_ptr轉(zhuǎn)換成unique_ptr,面對(duì)這些問題,跟隨小編一起看看吧2022-05-05c語言字符串函數(shù)strstr,strtok,strerror的使用和實(shí)現(xiàn)
C語言中的字符串處理函數(shù)如strtok、strstr和strerror對(duì)于字符串的處理有著重要的作用,strtok函數(shù)用于分割字符串,它通過sep參數(shù)指定的分隔符來分割str參數(shù)指定的字符串,并返回分割后的每個(gè)子字符串2024-10-10json error: Use of overloaded operator [] is ambiguous錯(cuò)誤的解決方
今天小編就為大家分享一篇關(guān)于json error: Use of overloaded operator [] is ambiguous錯(cuò)誤的解決方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-04-04C語言實(shí)現(xiàn)簡(jiǎn)單的貪吃蛇游戲的示例代碼
這篇文章主要為大家詳細(xì)介紹了C語言如何實(shí)現(xiàn)經(jīng)典貪吃蛇游戲,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C語言有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2023-01-01