案例来自黑马,C++基于多态的职工管理系统
主函数如下,采用switch来进行不同条件下的选择
用一个死循环来实现用户可以不退出实现各种功能的选择,同时只有输入0才可以退出。
#include <iostream>
using namespace std;
#include "workerManager.h"
#include "worker.h"
#include "employee.h"
#include "manager.h"
#include "boss.h"
int main()
{
测试代码
//Worker * worker = NULL;
//worker = new Employee(1,"张三",1);
//worker->showInfo();
//delete worker;
//worker = new Manager(2, "李四", 2);
//worker->showInfo();
//delete worker;
//worker = new Boss(3, "王五", 3);
//worker->showInfo();
//delete worker;
//实例化管理者对象
WorkerManager wm;
int choice = 0;//用来存储用户的选项
while (true)
{
//调用展示菜单的成员函数
wm.Show_Menu();
cout << "请输入您的选择:" << 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;
}
创建worker基类,确定基础属性但全为纯虚,以便给employee,manager,boss类继承
worker类头文件
#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_DepId;
};
普通职工类
头文件
//普通职工类
#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();
};
具体实现
#include "employee.h"
//构造函数
Employee::Employee(int id, string name, int did)
{
this->m_Id = id;
this->m_Name = name;
this->m_DepId = did;
}
//显示个人信息
void Employee::showInfo()
{
cout << "职工编号:" << this->m_Id
<< "\t职工名字:" << this->m_Name
<< "\t岗位:" << this->getDeptName()
<< "\t岗位职责:完成经理交给的任务 " << endl;
}
//获取岗位名称
string Employee::getDeptName()
{
return string("员工");
}
经理类
头文件
#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();
};
具体实现
#include "manager.h"
//构造函数
Manager::Manager(int id, string name, int did)
{
this->m_Id = id;
this->m_Name = name;
this->m_DepId = did;
}
//显示个人信息
void Manager::showInfo()
{
cout << "职工编号:" << this->m_Id
<< "\t职工名字:" << this->m_Name
<< "\t岗位:" << this->getDeptName()
<< "\t岗位职责:完成老板交给的任务,并下发给员工 " << endl;
}
//获取岗位名称
string Manager::getDeptName()
{
return string("经理");
}
老板类
头文件
#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();
};
具体实现
#include "boss.h"
//构造函数
Boss::Boss(int id, string name, int did)
{
this->m_Id = id;
this->m_Name = name;
this->m_DepId = did;
}
//显示个人信息
void Boss::showInfo()
{
cout << "职工编号:" << this->m_Id
<< "\t职工名字:" << this->m_Name
<< "\t岗位:" << this->getDeptName()
<< "\t岗位职责:管理公司所有事务 " << endl;
}
//获取岗位名称
string Boss::getDeptName()
{
return string("老板");
}
之后创建WoekerManager 头文件与cpp,在其中通过调用各个其他类,菜单中各个函数的声明与具体实现
WorkerManager.h 头文件
#pragma once //防止头文件重复包含
#include <iostream> //包含输入输出流头文件
using namespace std; //使用标准命名空间
#include "worker.h"
#include "employee.h"
#include "manager.h"
#include "boss.h"
#include <fstream>//文件流头文件
#define FILENAME "empFile.txt"
class WorkerManager
{
public:
//构造函数
WorkerManager();
//展示菜单
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();
//析构函数
~WorkerManager();
};
WorkerManager.cpp文件
#include "workerManager.h"
WorkerManager::WorkerManager()
{
//文件不存在
ifstream ifs;
ifs.open(FILENAME, ios::in);
//文件不存在情况
if (!ifs.is_open())
{
cout << "文件不存在" << endl;
//初始化属性
//初始化记录人数
this->m_EmpNum = 0;
//初始化数组指针
this->m_EmpArray = NULL;
//初始化文件是否为空
this->m_FileIsEmpty = true;
ifs.close();
return;
}
//文件存在,且没有记录
char ch;
ifs >> ch;
if (ifs.eof())
{
cout << "文件为空!" << endl;
this->m_EmpNum = 0;
this->m_FileIsEmpty = true;
this->m_EmpArray = NULL;
ifs.close();
return;
}
//文件存在,并且记录着数据
int num = this->get_EmpNum();
cout << "职工人数为:" << num << endl;
this->m_EmpNum = num;
//开辟空间
this->m_EmpArray = new Worker*[this->m_EmpNum];
//将文件中的数据,存到数组中
this->init_Emp();
//测试代码
//for (int i = 0; i < this->m_EmpNum; i++)
//{
// cout << "职工编号:" << this->m_EmpArray[i]->m_Id
// << "姓名:" << this->m_EmpArray[i]->m_Name
// << "部门编号:" << this->m_EmpArray[i]->m_DepId << endl;
//}
}
//展示菜单
void WorkerManager::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 WorkerManager::ExitSystem()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);//退出
}
//添加职工
void WorkerManager::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, name, 2);
break;
case 3:
worker = new Boss(id, name, 3);
break;
default:
break;
}
//将创建员工指针,保存到数组中
newSpace[this->m_EmpNum + i] = 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 WorkerManager::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_DepId << endl;
}
//关闭文件
ofs.close();
}
//统计文件中的人数
int WorkerManager::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;
//ifs.close();
}
//初始化员工
void WorkerManager::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)//经理
{
worker = new Manager(id, name, did);
}
else //老板
{
worker = new Boss(id, name, did);
}
this->m_EmpArray[index] = worker;
index++;
}
//关闭文件
ifs.close();
}
//显示职工
void WorkerManager::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 WorkerManager::Del_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
//按照职工编号删除
cout << "请输入想要删除的职工编号:" << endl;
int id = 0;
cin >> id;
int index = this->IsExist(id);
if (index != -1)//说明职工存在,并且要删除index位置的职工
{
for (int i = index; i < this->m_EmpNum - 1; 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 WorkerManager::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 WorkerManager::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, newName, dSelect);
break;
case 3:
worker = new Boss(newId, newName, dSelect);
break;
default:
break;
}
//更新数据 到数组中
this->m_EmpArray[ret] = worker;
cout << "修改成功!" << endl;
this->save();
}
else
{
cout << "修改失败:查无此人!" << endl;
}
}
system("pause");
system("cls");
}
//查找职工
void WorkerManager::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 WorkerManager::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 WorkerManager::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");
}
WorkerManager::~WorkerManager()//析构释放堆区内存
{
if (this->m_EmpArray != NULL)
{
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->m_EmpArray[i] != NULL)
{
delete this->m_EmpArray[i];
}
}
delete[] this->m_EmpArray;
this->m_EmpArray = NULL;
}
}