C++利用多態(tài)實現(xiàn)職工管理系統(tǒng)(項目開發(fā))
分析
首先看一下這個項目的文件:

主要分為兩部分:
主體部分:
main.cpp和workManager.h,workManager.cpp職工部分(這里采用多態(tài)的方式編寫):
主要是worker.h和worker.cpp
三種職位:boss,employee,manager
經(jīng)過分析是否大概知道了其中各部分的意思呢?
看起來這里面有很多,但是正是這種多個文件編寫才時代碼更加簡潔。
所以在正式寫項目之前一定要先考慮好整體架構(gòu),在進行編寫。
項目整體架構(gòu):
這個項目的難度并不大,主要是要學(xué)會這個項目整體架構(gòu)是怎樣安排的。

左邊是主體部分。
主要是整個項目的功能,將主界面的所有功能通過代碼和右邊的不同人之間進行實現(xiàn)。
包含了上面幾個功能函數(shù),還有一些沒有寫出來
右邊則是三種職工的信息。
每個人都有自己的姓名,編號,職位,然后包含兩個函數(shù),顯示個人信息和獲取崗位信息。
主要是通過數(shù)組進行存儲的
其它的也沒什么需要過多介紹的,主要是要理解上面的整體架構(gòu)圖,詳細的代碼編寫并不算復(fù)雜
部分運行截圖



代碼
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,按照編號排序 *******" << 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 << "請輸入添加職工數(shù)量:" << endl;
int addNum = 0;
cin >> addNum;
if (addNum > 0)
{
//添加
//計算添加新空間大小
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; //職工編號
string name; //職工編號
int dSelect; //部門選擇
cout << "請輸入第" << i + 1 << "個新職工編號:" << endl;
cin >> id;
cout << "請輸入第" << i + 1 << "個新職工姓名:" << endl;
cin >> name;
cout << "請選擇該職工崗位:" << 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;
//更新職工不為空標志
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); //用輸入的方式打開文件 -- 寫文件
//將每個人數(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)計文件人數(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
{
//按照職工編號刪除
cout << "請輸入想要刪除職工編號:" << 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ù)組中記錄人員個數(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 << "修改職工編號:" << endl;
int id;
cin >> id;
int ret = this->IsExist(id);
if (ret != -1)
{
//查找到編號的職工
delete this->m_EmpArray[ret];
int newId = 0;
string newName = "";
int dSelect = 0;
cout << "查到:" << id << "號職工,請輸入新職工號:" << endl;
cin >> newId;
cout << "請輸入新名字:" << endl;
cin >> newName;
cout << "請輸入崗位:" << 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 << "請輸入查找的方式:" << endl;
cout << "1,按照職工的編號查找" << endl;
cout << "2,按照職工的姓名查找" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
//按照編號查
int id;
cout << "請輸入查找的職工編號:" << 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 << "請輸入查找的姓名:" << endl;
cin >> name;
//加入是否查到標志
bool flag = false; //默認未找到
for (int i = 0; i < m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_Name == name)
{
cout << "查找成功,職工編號為:"
<< this->m_EmpArray[i]->m_Id
<< "號職工信息如下:" << endl;
flag = true;
this->m_EmpArray[i]->showInfo();
}
}
if (flag == false)
{
cout << "查無此人" << endl;
}
}
else
{
cout << "輸入選項有誤!" << endl;
}
}
system("pause");
system("cls");
}
//按照編號排序
void WorkerManage::Sort_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或記錄為空!" << endl;
system("pause");
system("cls");
}
else
{
cout << "請輸入排序的方式:" << endl;
cout << "1,按照職工的編號升序" << endl;
cout << "2,按照職工的編號降序" << 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;
}
}
}
//判斷一開始認定的最大值或最小值
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ū)的每個職工對象
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)計文件人數(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();
//按照編號排序
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,按照編號排序 *******" << 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 << "請輸入添加職工數(shù)量:" << endl;
int addNum = 0;
cin >> addNum;
if (addNum > 0)
{
//添加
//計算添加新空間大小
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; //職工編號
string name; //職工編號
int dSelect; //部門選擇
cout << "請輸入第" << i + 1 << "個新職工編號:" << endl;
cin >> id;
cout << "請輸入第" << i + 1 << "個新職工姓名:" << endl;
cin >> name;
cout << "請選擇該職工崗位:" << 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;
//更新職工不為空標志
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); //用輸入的方式打開文件 -- 寫文件
//將每個人數(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)計文件人數(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
{
//按照職工編號刪除
cout << "請輸入想要刪除職工編號:" << 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ù)組中記錄人員個數(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 << "修改職工編號:" << endl;
int id;
cin >> id;
int ret = this->IsExist(id);
if (ret != -1)
{
//查找到編號的職工
delete this->m_EmpArray[ret];
int newId = 0;
string newName = "";
int dSelect = 0;
cout << "查到:" << id << "號職工,請輸入新職工號:" << endl;
cin >> newId;
cout << "請輸入新名字:" << endl;
cin >> newName;
cout << "請輸入崗位:" << 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 << "請輸入查找的方式:" << endl;
cout << "1,按照職工的編號查找" << endl;
cout << "2,按照職工的姓名查找" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
//按照編號查
int id;
cout << "請輸入查找的職工編號:" << 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 << "請輸入查找的姓名:" << endl;
cin >> name;
//加入是否查到標志
bool flag = false; //默認未找到
for (int i = 0; i < m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_Name == name)
{
cout << "查找成功,職工編號為:"
<< this->m_EmpArray[i]->m_Id
<< "號職工信息如下:" << endl;
flag = true;
this->m_EmpArray[i]->showInfo();
}
}
if (flag == false)
{
cout << "查無此人" << endl;
}
}
else
{
cout << "輸入選項有誤!" << endl;
}
}
system("pause");
system("cls");
}
//按照編號排序
void WorkerManage::Sort_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或記錄為空!" << endl;
system("pause");
system("cls");
}
else
{
cout << "請輸入排序的方式:" << endl;
cout << "1,按照職工的編號升序" << endl;
cout << "2,按照職工的編號降序" << 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;
}
}
}
//判斷一開始認定的最大值或最小值
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ū)的每個職工對象
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:
//顯示個人信息
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);
//顯示個人信息
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;
}
//顯示個人信息
void Boss::showInfo()
{
cout << "職工編號:" << 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);
//顯示個人信息
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;
}
//顯示個人信息
void Employee::showInfo()
{
cout << "職工編號:" << 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);
//顯示個人信息
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;
}
//顯示個人信息
void Manager::showInfo()
{
cout << "職工編號:" << 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)實現(xiàn)職工管理系統(tǒng)(項目開發(fā))的文章就介紹到這了,更多相關(guān)C++職工管理系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++利用easyx圖形庫實現(xiàn)創(chuàng)意天天酷跑小游戲
這篇文章主要為大家詳細介紹了C++如何利用easyx圖形庫實現(xiàn)創(chuàng)意小游戲——天天酷跑,文中的示例代碼講解詳細,快跟隨小編一起了解一下吧2023-03-03
C/C++ 運用Npcap發(fā)送UDP數(shù)據(jù)包的完美過程
UDP 是一種無連接、輕量級的傳輸層協(xié)議,與 TCP 相比,它不提供可靠性、流控制和錯誤恢復(fù)機制,但卻更加簡單且具有較低的開銷,這篇文章主要介紹了C/C++ 運用Npcap發(fā)送UDP數(shù)據(jù)包,需要的朋友可以參考下2023-11-11
C++關(guān)鍵字thread_local學(xué)習(xí)筆記
這篇文章主要為大家介紹了C++關(guān)鍵字thread_local學(xué)習(xí)筆記,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10

