1. 程式人生 > 程式設計 >C++利用多型實現職工管理系統(專案開發)

C++利用多型實現職工管理系統(專案開發)

分析

首先看一下這個專案的檔案:

C++利用多型實現職工管理系統(專案開發)

主要分為兩部分:

主體部分:
main.cpp和workManager.h,workManager.cpp職工部分(這裡採用多型的方式編寫):
主要是worker.h和worker.cpp
三種職位:boss,employee,manager

經過分析是否大概知道了其中各部分的意思呢?

看起來這裡面有很多,但是正是這種多個檔案編寫才時程式碼更加簡潔。

所以在正式寫專案之前一定要先考慮好整體架構,在進行編寫。

專案整體架構:

這個專案的難度並不大,主要是要學會這個專案整體架構是怎樣安排的。

C++利用多型實現職工管理系統(專案開發)

左邊是主體部分。

主要是整個專案的功能,將主介面的所有功能通過程式碼和右邊的不同人之間進行實現。

包含了上面幾個功能函式,還有一些沒有寫出來

右邊則是三種職工的資訊。

每個人都有自己的姓名,編號,職位,然後包含兩個函式,顯示個人資訊和獲取崗位資訊。

主要是通過陣列進行儲存的

其它的也沒什麼需要過多介紹的,主要是要理解上面的整體架構圖,詳細的程式碼編寫並不算複雜

部分執行截圖

C++利用多型實現職工管理系統(專案開發)

C++利用多型實現職工管理系統(專案開發)

C++利用多型實現職工管理系統(專案開發)

程式碼

main.cpp

#include "workManager.h"
 
//建構函式
WorkerManage::WorkerManage()
{
	ifstream ifs;
	ifs.open(FILENAME,ios::in); //讀檔案
 
	//1,檔案不存在
	if (!ifs.is_open())
	{
		cout << "檔案不存在" << endl;
 
		//初始化屬性
		//初始化記錄人數
		this->m_EmpNum = 0;
		//初始化陣列指標
		this->m_EmpArray = NULL;
		//初始化檔案是否為空
		this->m_FileIsEmpty = true;
		ifs.close();
		return;
	}
 
	//2,檔案存在,資料為空
	char ch;
	ifs >> ch;
	if (ifs.eof())
	{
		//檔案為空
		cout << "檔案為空!" << endl;
		//初始化屬性
		//初始化記錄人數
		this->m_EmpNum = 0;
		//初始化陣列指標
		this->m_EmpArray = NULL;
		//初始化檔案是否為空
		this->m_FileIsEmpty = true;
		ifs.close();
		return;
	}
 
	//3,檔案存在,並且記錄資料
	int num = this->get_EmpNum();
	//cout << "職工人數為:" << num << endl;
	this->m_EmpNum = num;
 
	//開闢空間
	this->m_EmpArray = new Worker *[this->m_EmpNum];
	//將檔案中資料,存到陣列中
	this->init_Emp();
 
}
 
//展示選單
void WorkerManage::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;
}
 
//退出系統
void WorkerManage::ExitSystem()
{
	cout << "歡迎下次使用" << endl;
	system("pause");
	exit(0); //退出程式
}
 
//解構函式
WorkerManage::~WorkerManage()
{
 
}
 
//新增職工
void WorkerManage::Add_Emp()
{
	cout << "請輸入新增職工數量:" << endl;
	int addNum = 0;
	cin >> addNum;
 
	if (addNum > 0)
	{
		//新增
		//計算新增新空間大小
		int newSize = this->m_EmpNum + addNum; //新空間人數 = 原來記錄人數+新增人數
 
		//開闢新空間
		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 < 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,經理" << 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,2);
				break;
			case 3:
				worker = new Boss(id,3);
				break;
			default:
				break;
			}
 
			//將建立職工職責,儲存到陣列中
			newSpace[this->m_EmpNum++] = worker;
		}
 
		//釋放原有的空間
		delete[] this->m_EmpArray;
 
		//更改新空間指向
		this->m_EmpArray = newSpace;
 
		//更新新的職工人數
		this->m_EmpNum = newSize;
 
		//更新職工不為空標誌
		this->m_FileIsEmpty = false;
 
		//新增成功
		cout << "成功新增" << addNum << "名新職工" << endl;
 
		//儲存資料到檔案中
		this->save();
	}
	else
	{
		cout << "輸入資料有誤" << endl;
	}
 
	system("pause");
	system("cls");
}
 
//儲存檔案
void WorkerManage::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_DeptId << endl;
	}
 
	//關閉檔案
	ofs.close();
}
 
//統計檔案人數
int WorkerManage::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)
	{
		num++;
	}
 
	return num;
}
 
//初始化職工
void WorkerManage::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;
 
		switch (dId)
		{
			case 1:
				worker = new Employee(id,dId);
				break;
			case 2:
				worker = new Manager(id,dId);
				break;
			case 3:
				worker = new Boss(id,dId);
				break;
			default:
				break;
		}
 
		this->m_EmpArray[index] = worker;
		index++;
	}
 
	this->m_FileIsEmpty = false;
	//關閉檔案
	ifs.close();
}
 
//顯示職工
void WorkerManage::show_Emp()
{
	//判斷檔案是否為空
	if (this->m_FileIsEmpty)
	{
		cout << "檔案不存在或者記錄為空" << endl;
	}
	else
	{
		for (int i = 0; i < m_EmpNum; i++)
		{
			//利用多型呼叫程式埠
			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)
		{
			//資料前移
			for (int i = index; i < this->m_EmpNum; i++)
			{
				this->m_EmpArray[i] = this->m_EmpArray[i + 1];
			}
			this->m_EmpNum--; //更新陣列中記錄人員個數
 
			//資料更新到檔案中
			this->save();
 
			cout << "刪除成功" << endl;
		}
		else
		{
			cout << "刪除失敗,未找到該職工" << endl;
		}
	}
	system("pause");
	system("cls");
}
 
//判斷職工是否存在,如果存在返回職工所在陣列中的位置,不存在返回-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,經理" << 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,dSelect);
				break;
			case 3:
				worker = new Boss(newId,dSelect);
				break;
			default:
				break;
			}
 
			//更新資料 到陣列中
			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(); //排序結果儲存
		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); //刪除檔案後重新建立
		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_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:
	//建構函式
	WorkerManage();
	
	//展示選單
	void Show_Menu();
 
	//退出系統
	void ExitSystem();
 
	//記錄職工人數
	int m_EmpNum;
	
	//職工陣列指標
	Worker ** m_EmpArray;
 
	//新增職工
	void Add_Emp();
 
	//儲存檔案
	void save();
 
	//判斷檔案是否為空
	bool m_FileIsEmpty;
 
	//統計檔案人數
	int get_EmpNum();
 
	//初始化職工
	void init_Emp();
 
	//顯示職工
	void show_Emp();
 
	//刪除職工
	void Del_Emp();
 
	//判斷職工是否存在,如果存在返回職工所在陣列中的位置,不存在返回-1
	int IsExist(int id);
 
	//修改職工
	void Mod_Emp();
 
	//查詢職工
	void Find_Emp();
 
	//按照編號排序
	void Sort_Emp();
 
	//清空檔案
	void Clean_File();
 
	//解構函式
	~WorkerManage();
};

workManager.cpp

#include "workManager.h"
 
//建構函式
WorkerManage::WorkerManage()
{
	ifstream ifs;
	ifs.open(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_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"
 
//建構函式
Boss::Boss(int id,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>
#include"worker.h"
using namespace std;
 
class Employee:public Worker
{
public:
	Employee(int id,int dId);
 
	//顯示個人資訊
	virtual void showInfo();
 
	//獲取崗位名稱
	virtual string getDeptName();
};

employee.cpp

#include"employee.h"
 
//建構函式
Employee::Employee(int id,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,int dId);
 
	//顯示個人資訊
	virtual void showInfo();
 
	//獲取崗位名稱
	virtual string getDeptName();
};

manager.cpp

#include"manager.h"
 
//建構函式
Manager::Manager(int id,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("經理");
}

到此這篇關於C++利用多型實現職工管理系統(專案開發)的文章就介紹到這了,更多相關C++職工管理系統內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!