溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何用C++基于多態(tài)設(shè)計(jì)職工管理系統(tǒng)

發(fā)布時(shí)間:2020-05-22 14:29:37 來(lái)源:億速云 閱讀:249 作者:鴿子 欄目:編程語(yǔ)言

主模塊(職工管理系統(tǒng).cpp)

#include

using namespace std;

#include "workerManger.h"

#include "worker.h"

#include "employee.h"

#include "Boss.h"

#include "Manager.h"

int main()

{

//實(shí)例化一個(gè)管理者對(duì)象智匯代理申請(qǐng)

WorkerManger wm;

int choice = 0;

while (true)

{

//調(diào)用成員函數(shù)顯示菜單

wm.Show_Menu();

cout << "請(qǐng)輸入您的選擇:" << endl;

cin >> choice;

switch (choice)

{

case 0://退出

wm.ExitSystem();

break;

case 1://增加

wm.Add_Emp();

break;

case 2://顯示

wm.Show_Emp();

break;

case 3://刪除

wm.Del_Emp();

break;

case 4://修改

wm.Mod_Emp();

break;

case 5://查找

wm.Find_Emp();

break;

case 6://排序

wm.Sort_Emp();

break;

case 7://清空

wm.Clean_File();

break;

default:

system("cls");//清屏

break;

}

}

system("pause");

return 0;

}

workerManger.h的文件:

#pragma once //防止頭文件重復(fù)包含

#include

#include

#include "worker.h"

#include "employee.h"

#include "Boss.h"

#include "Manager.h"

#define FILENAME "empFile.txt"

using namespace std;

class WorkerManger

{

public:

//構(gòu)造函數(shù)

WorkerManger();

//展示菜單

void Show_Menu();

//退出系統(tǒng)

void ExitSystem();

//記錄職工人數(shù)

int m_EmpNum;

//職工數(shù)組指針

Worker ** m_EmpArray;

//添加職工

void Add_Emp();

//保存文件

void save();

//判斷文件是否為空的標(biāo)志

bool m_FileIsEmpty;

//統(tǒng)計(jì)文件中的人數(shù)

int get_EmpNum();

//初始化職工

void init_Emp();

//顯示職工

void Show_Emp();

//刪除職工

void Del_Emp();

//判斷職工是否存在

int IsExist(int id);

//修改職工

void Mod_Emp();

//查找職工

void Find_Emp();

//按編號(hào)排序

void Sort_Emp();

//清空文件

void Clean_File();

//析構(gòu)函數(shù)

~WorkerManger();

};

workerManger.cpp文件:

#include "workerManger.h"

WorkerManger::WorkerManger()

{

//1、文件不存在

ifstream ifs;

ifs.open(FILENAME, ios::in);

if (!ifs.is_open())

{

this->m_EmpNum = 0; //初始化記錄人數(shù)

this->m_EmpArray = NULL; //初始化數(shù)組指針

this->m_FileIsEmpty = true; //初始化文件是否為空

ifs.close();

return;

}

//文件存在,數(shù)據(jù)為空

char ch;

ifs >> ch;

if (ifs.eof())

{

this->m_EmpNum = 0; //初始化記錄人數(shù)

this->m_EmpArray = NULL; //初始化數(shù)組指針

this->m_FileIsEmpty = true; //初始化文件是否為空

ifs.close();

return;

}

//當(dāng)文件存在并且記錄有數(shù)據(jù)

int num = this->get_EmpNum();

this->m_EmpNum = num;

this->m_EmpArray = new Worker*[this->m_EmpNum];

this->init_Emp();

}

//展示菜單

void WorkerManger::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;

cout << endl;

}

//添加職工

void WorkerManger::Add_Emp()

{

cout << "請(qǐng)輸入添加的職工數(shù)量:" << endl;

int addNum = 0; //保存用戶的輸入數(shù)量

cin >> addNum;

if(addNum > 0)

{

//計(jì)算添加新空間的大小

int newSize = this->m_EmpNum + addNum; //新空間人數(shù) = 原來(lái)記錄人數(shù) + 新增人數(shù)

//開辟新空間

Worker * newSpace = new Worker[newSize];

//將原來(lái)空間下數(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; //職工姓名

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 + i] = 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 << "輸入有誤!" << endl;

}

//按任意鍵后 清屏回到上級(jí)目錄

system("pause");

system("cls");

}

//統(tǒng)計(jì)文件中的人數(shù)

int WorkerManger::get_EmpNum()

{

ifstream ifs;

ifs.open(FILENAME, ios::in); // 打開文件 讀文件

int id;

string name;

int dId;

int num = 0;

while (ifs >> id && ifs >> name && ifs >> dId)

{

//統(tǒng)計(jì)人數(shù)

num++;

}

return num;

}

//退出系統(tǒng)

void WorkerManger::ExitSystem()

{

cout << "歡迎下次使用" << endl;

system("pause");

exit(0);//退出程序

}

//保存文件

void WorkerManger::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();

}

//初始化職工

void WorkerManger::init_Emp()

{

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;

if (dId == 1) //普通職工

{

worker = new Employee(id, name, dId);

}

else if (dId == 2) // 經(jīng)理

{

worker = new Manager(id, name, dId);

}

else

{

worker = new Boss(id, name, dId);

}

this->m_EmpArray[index] = worker;

index++;

}

ifs.close();

}

//顯示職工

void WorkerManger::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 WorkerManger::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) //職工存在,并且要?jiǎng)h除index位置上的職工

{

//數(shù)據(jù)前移

for (int i = index; i < this->m_EmpNum - 1; i++)

{

this->m_EmpArray[i] = this->m_EmpArray[i + 1];

}

this->m_EmpNum--;//更新數(shù)組中記錄的人員個(gè)數(shù)

//同步更新到文件

this->save();

cout << "刪除成功!" << endl;

}

else

{

cout << "刪除失敗!未找到該職工!" << endl;

}

}

system("pause");

system("cls");

}

//判斷職工是否存在

int WorkerManger::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 WorkerManger::Mod_Emp()

{

if (this->m_FileIsEmpty)

{

cout << "文件不存在或記錄為空!" << endl;

}

else

{

cout << "請(qǐng)輸入要修改的職工編號(hào):" << 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 << " 號(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ù)

this->m_EmpArray[ret] = worker;

cout << "修改成功!" << endl;

this->save(); //保存到文件

}

else

{

cout << "修改失敗!查無(wú)此人!" << endl;

}

}

system("pause");

system("cls");

}

//查找職工

void WorkerManger::Find_Emp()

{

if (this->m_FileIsEmpty)

{

cout << "文件不存愛或者記錄為空!" << endl;

}

else

{

cout << "請(qǐng)輸入查找方式:" << endl;

cout << "1、按職工編號(hào)查找" << endl;

cout << "2、按職工姓名查找" << endl;

int select;

cin >> select;

if (select == 1)

{

//按編號(hào)

int id;

cout << "請(qǐng)輸入查找的職工編號(hào):" << endl;

cin >> id;

int ret = this->IsExist(id);

if (ret != -1)

{

//找到職工

cout << "查找成功!該職工的信息如下:" << endl;

this->m_EmpArray[ret]->showInfo();

}

else

{

cout << "查找失敗!查無(wú)此人!" << endl;

}

}

else if(select == 2)

{

//按姓名

string name;

cout << "請(qǐng)輸入查找的姓名:";

cin >> name;

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

<< "的職工信息如下:" << endl;

this->m_EmpArray[i]->showInfo();

flag = true;

}

}

if (flag == false)

{

cout << "查找失敗!查無(wú)此人!" << endl;

}

}

else

{

cout << "輸入的選項(xiàng)有誤!" << endl;

}

}

system("pause");

system("cls");

}

//排序

void WorkerManger::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;

cin >> select;

for (int i = 0; i < this->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)定的最小值或最大值是不是計(jì)算的最小值或最大值 如果不是 交換

if (i != MinOrMax)

{

Worker * temp = this->m_EmpArray[i];

this->m_EmpArray[i] = this->m_EmpArray[MinOrMax];

this->m_EmpArray[MinOrMax] = temp;

}

}

cout << "排序成功!排序后的結(jié)果為:" << endl;

this->save();//排序后結(jié)果保存到文件

this->Show_Emp();//展示所有職工

}

}

//清空文件

void WorkerManger::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)

{

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");

}

WorkerManger::~WorkerManger()

{

if (this->m_EmpArray != NULL)

{

delete[] this->m_EmpArray;

this->m_EmpArray = NULL;

}

}

worker.h文件:

#pragma once

#include

using namespace std;

#include

//職工抽象類

class Worker

{

public:

//顯示個(gè)人信息

virtual void showInfo() = 0;

//獲取崗位名稱

virtual string getDeptName() = 0;

int m_ID; //職工編號(hào)

string m_Name; //職工姓名

int m_DeptID; //部門編號(hào)

};

employee.h文件:

//普通員工文件

#pragma once

#include

#include

using namespace std;

#include "worker.h"

class Employee : public Worker

{

public:

//構(gòu)造函數(shù)

Employee(int id, string name, int dID);

//顯示個(gè)人信息

void showInfo();

//獲取崗位名稱

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

#include "worker.h"

using namespace std;

//經(jīng)理類

class Manager : public Worker

{

public:

//構(gòu)造函數(shù)

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)理");

}

Boss.h文件:

#pragma once

#include

#include "worker.h"

using namespace std;

//經(jīng)理類

class Boss : public Worker

{

public:

//構(gòu)造函數(shù)

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("老板");

}

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI