职工管理系统
C++的学习第二部分类和对象,这也是C++学习中的中之重。首先明白程序内存模型,即程序的四区(代码区、全局区、栈区、堆区)、学习对象的特新、函数重载、运算符重载(目前感觉还是不常用)、对象的特性、继承和多态以及文件操作、C++的文件操作。主要学习这些内容。
一、程序运行界面
如下图所示,该管理系统的功能是实现公司人员信息管理,程序中做了图片中的八个功能。初始文件内含有张三、李四、王五三个员工的信息。可以自行添加、修改、删除员工的信息。
二、程序源码
因为通过对象的继承与多态实现的职工管理,所以程序相对比较复杂通过多个.cpp文件和.h文件组成,这里就展示部分源文件代码,需要整个项目的源代码可以通过文章后面的链接自行下载。
1、main函数
通过在main函数中的switch里面选择不同的操作。
#include <iostream>
#include<string>
#include"WorkManage.h"
#include"Work.h"
#include"Boss.h"
#include"Manager.h"
#include"Employee.h"
using namespace std;
int main(int agrc , char** agrv)
{
WorkManage manage;
while (1)
{
int choice = 0;
manage.Staff_Show();
cout << "请输入你的选择: ";
cin >> choice;
cout << endl;
switch (choice)
{
case 1:
manage.add_worker(); //添加职工成员(保存到file.exe文件中去)
break;
case 2:
manage.ShowAll(); //显示file.exe中的所有人员信息
break;
case 3:
manage.Delete_staff(); //按照人员ID编号或者姓名来删除职工人员
break;
case 4:
manage.Modi_staff(); //按照编号查找人员并且修改人员信息
break;
case 5:
manage.Find_staff(); //按照编号或者姓名查找员工信息
break;
case 6:
manage.Sort_staff(); //按照成员编号进行排序
break;
case 7:
manage.clear_all(); //清空所有文件
break;
case 8:
manage.System_Exit(); //退出职工管理系统
break;
default:
return 0;
break;
}
}
system("pause");
return 0;
}
2、workmanager函数
通过这个函数实现八个具体操作。
#include"WorkManage.h"
WorkManage::WorkManage() //WorkManage构造函数
{
//初始化成员属性
this->InitEmptyRead();
InitRead(); //如若file.text文件有数据,则将数据初始化到WorkerArray和workernum里面去
}
WorkManage::~WorkManage() //WorkManage析构函数
{
if (this->WorkArray != NULL)
{
delete this->WorkArray;
this->WorkArray = NULL;
}
}
void WorkManage::Staff_Show() //人员显示函数
{
cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
cout << "$$$$$$$$ 欢迎使用职工管理系统 $$$$$$$" << endl;
cout << "$$$$$$$$$$ 1、添加职工信息 $$$$$$$$$$" << endl;
cout << "$$$$$$$$$$ 2、显示职工信息 $$$$$$$$$$" << endl;
cout << "$$$$$$$$$$ 3、删除职工信息 $$$$$$$$$$" << endl;
cout << "$$$$$$$$$$ 4、修改职工信息 $$$$$$$$$$" << endl;
cout << "$$$$$$$$$$ 5、查找职工信息 $$$$$$$$$$" << endl;
cout << "$$$$$$$$$$ 6、按照编号排序 $$$$$$$$$$" << endl;
cout << "$$$$$$$$$$ 7、清空所有文档 $$$$$$$$$$" << endl;
cout << "$$$$$$$$$$ 8、退出管理系统 $$$$$$$$$$" << endl;
cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
}
void WorkManage::System_Exit()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);
}
void WorkManage::add_worker() //添加职工函数
{
int AddNum = 0;
loop2:cout << "请输入你要添加职工的人数:" ;
cin >> AddNum;
cout << endl;
if (AddNum > 0)
{
int ToalWorker = this->WorkNum + AddNum;
Work** ToalArray = new Work*[ToalWorker]; //在堆区新开辟数组存放worker父类指针
//必须先开辟空间、必须先开辟空间、必须先开辟空间、
if (this->WorkArray != NULL)
{
for (int i = 0; i < WorkNum; i++) //将初始存储的值赋给ToalWork
{
ToalArray[i] = this->WorkArray[i];
}
}
Work* worker = NULL; //**创建一个父类worker指针来指向子类,并且存储到新开辟的堆区数组中去
for (int i = 0; i <AddNum; i++)
{
long long ID = 2020080401; //???????????????????????????????????????????????
cout << "请输入第" << i + 1 << "个人的ID:";
cin >> ID;
cout << endl;
for (int i = 0; i < this->WorkNum; i++) //判断文件中是否已有存在相同的id,如果存在相同的id,则只能重新录入信息
{
if (ID == this->WorkArray[i]->m_ID)
{
cout << "您输入编号有重复,请重新添加员工信息..." << endl;
system("pause");
system("cls");
return;
}
}
string name = "张三";
cout << "请输入第" << i + 1 << "个人的名字:";
cin >> name;
cout << endl;
int delect = 1; //选择工作人员的共属性(工人、经理、董事长)
cout << "输入人员的属性:" << endl;
cout << " 1、员 工" << endl;
cout << " 2、经 理" << endl;
cout << " 3、董事长" << endl;
loop1:cout << "请输入你的选择:";
cin >> delect;
cout << endl;
switch (delect) //根据用户的不同选择调用不同的子类存储数据并且放到数组中去
{
case 1:
worker = new Employee(ID, name, delect);
break;
case 2:
worker = new Manager(ID, name, delect);
break;
case 3:
worker = new Boss(ID, name, delect);
break;
default:
cout << "输入有误请重新输入..." << endl;
goto loop1;
break;
}
ToalArray[this->WorkNum + i] = worker;
}
for (int i = 0; i < AddNum; i++) //打印添加的人数
{
cout << "职工ID:" << ToalArray[this->WorkNum + i]->m_ID << "\t\t职工名字:" << ToalArray[this->WorkNum + i]->m_name << "\t\t职工部门:"
<< ToalArray[this->WorkNum + i]->get_department() << "\t职工职责:" << ToalArray[this->WorkNum + i]->get_task() << endl;
}
cout << "成功添加" << AddNum << "名人员" << endl;
this->EmptyFile = false; //添加人员后说明已经创建了文件文件不为空
this->WorkNum = ToalWorker;
delete[] this->WorkArray; //释放原有堆区空间
this->WorkArray = ToalArray;
this->Write(); //调用write函数,保存数据到文件当中去
}
else
{
cout << "输入有误,请重新输入..." << endl;
goto loop2;
}
system("pause");
system("cls");
}
void WorkManage::Write() //写文件
{
//写文件5大步骤,第一,创建文件的头文件。第二,创建输出流对象。第三,打开文件方式(ios::out写文件/ios::in读文件/二进制读写等)
//第四,读/写相对应的内容。第五,关闭文件
ofstream ofs; //创建对象
ofs.open(FILE,ios::out);
for (int i = 0; i < this->WorkNum; i++)
{
ofs << this->WorkArray[i]->m_ID << " " << this->WorkArray[i]->m_name << " " << this->WorkArray[i]->m_department_ID<< endl;
}
ofs.close();
}
void WorkManage::InitEmptyRead() //读文件之前判断是否存在文件并且进行初始化
{
ifstream ifs;
ifs.open(FILE, ios::in);
//文件不存在
if (!ifs.is_open()) //读文件时判断文件是否存在,若文件不存在,则初始化数组和员工人数和文件存在标志
{
//is_open()返回值是bool类型,所以需要判断返回值是否为真就可以判断文件是否打开成功
cout << "温馨提示:文件不存在" << endl;
this->WorkNum = 0;
this->WorkArray = NULL;
this->EmptyFile = true; //文件存在标志置位真
ifs.close();
return;
}
//文件存在,但是数据为空
char a;
ifs >> a;
if (ifs.eof()) //读取文件尾部判断是否为空
{
cout << "温馨提示:文件为空文件" << endl;
this->WorkNum = 0;
this->WorkArray = NULL;
this->EmptyFile = true; //文件存在标志置位真
ifs.close();
return;
}
}
int WorkManage::get_num() //wtf
{
ifstream ifs;
ifs.open(FILE, ios::in);
long long ID;
string name;
int depart_id;
int num = 0;
while (ifs >> ID && ifs >> name && ifs >> depart_id)
{
num++;
}
return num;
}
void WorkManage::InitRead() //读文件,将存有数据的文件数据读到数组中去
{
ifstream ifs;
ifs.open(FILE, ios::in);
long long ID;
string name;
int depart_id;
int num = 0;
Work* worker1 = NULL;
this->WorkArray = new Work*[this->get_num()]; //需要先给维数组开辟空间,需要先给维数组开辟空间,需要先给维数组开辟空间
while (ifs >> ID && ifs >> name && ifs >> depart_id)
{
if (depart_id == 1) //SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
{
worker1 = new Employee(ID,name,depart_id);
}
else if (depart_id == 2)
{
worker1 = new Manager(ID, name, depart_id);
}
else
{
worker1 = new Boss(ID, name, depart_id);
}
this->WorkArray[num] = worker1;
num++;
}
if (num != 0)
{
cout << "温馨提示:文件不为空。且含有" <<num<<"个员工数据。"<< endl;
this->WorkNum = num;
//for (int i = 0; i < this->WorkNum;i++) //测试代码
//{
// cout << this->WorkArray[i]->m_name << endl;
//}
}
ifs.close();
}
void WorkManage::ShowAll() //显示功能
{
if (this->EmptyFile) //先判断文件是否存在
{
cout << "文件不存在,暂无保存任何职工信息" << endl;
}
else
{
for (int i = 0; i < this->WorkNum; i++) //遍历数组,利用多态显示
{
this->WorkArray[i]->show_message();
}
}
system("pause");
system("cls");
}
void WorkManage::Delete_staff() //删除职工
{
if (this->EmptyFile) //先判断文件是否存在
{
cout << "文件不存在,暂无保存任何职工信息" << endl;
}
else
{
cout << "请选择你删除职工的方式:" << endl;
cout << "1、按照职工编号删除职工" << endl;
cout << "2、按照职工名字删除职工" << endl;
int choice = 0;
cout << "请输入你的选择:";
cin >> choice;
if (choice == 1)
{
cout << "请输入你将要删除的职工编号:";
long long ID;
int flag = -1; //用来记录查找到的数组位置
cin >> ID;
for (int i = 0; i < this->WorkNum; i++)
{
if (ID == this->WorkArray[i]->m_ID)
{
flag = i;
cout << "查找结果为:";
cout << "职工ID:" << this->WorkArray[i]->m_ID << "\t\t职工名字:" << this->WorkArray[i]->m_name << "\t\t职工部门:"
<< this->WorkArray[i]->get_department() << "\t职工职责:" << this->WorkArray[i]->get_task() << endl;
int delect = 0;
cout << "再次确定是否删除?" << endl;
cout << "1、确定" << endl;
cout << "2、我在想想..." << endl;
cout << "请输入你的选择:";
cin >> delect;
if (delect != 1)
{
return;
}
}
}
if (flag != -1)
{
for (int i = flag; i < this->WorkNum - 1; i++)
{
this->WorkArray[i] = this->WorkArray[i + 1];
}
this->WorkNum -= 1;
this->Write();
cout << "删除成功..." << endl;
}
else
{
cout << "查无此编号员工..." << endl;
}
}
else if (choice == 2)
{
cout << "请输入你将要删除的职工姓名:";
string name;
int flag = -1;
cin >> name;
for (int i = 0; i < this->WorkNum; i++)
{
if (name == this->WorkArray[i]->m_name)
{
flag = i;
cout << "查找结果为:";
cout << "职工ID:" << this->WorkArray[i]->m_ID << "\t\t职工名字:" << this->WorkArray[i]->m_name << "\t\t职工部门:"
<< this->WorkArray[i]->get_department() << "\t职工职责:" << this->WorkArray[i]->get_task() << endl;
int delect = 0;
cout << "再次确定是否删除?" << endl;
cout << "1、确定" << endl;
cout << "2、我在想想..." << endl;
cout << "请输入你的选择:";
cin >> delect;
if (delect != 1)
{
return;
system("pause");
system("cls");
}
}
}
if (flag != -1)
{
for (int i = flag; i < this->WorkNum - 1; i++)
{
this->WorkArray[i] = this->WorkArray[i + 1];
}
this->WorkNum -= 1;
this->Write();
cout << "删除成功..." << endl;
}
else
{
cout << "查无此姓名员工..." << endl;
}
system("pause");
system("cls");
}
else
{
cout << "输入有误" << endl;
return;
}
}
system("pause");
system("cls");
}
void WorkManage::Modi_staff() //修改职工
{
if (this->EmptyFile) //先判断文件是否存在
{
cout << "文件不存在,暂无保存任何职工信息" << endl;
system("pause");
system("cls");
return;
}
else
{
long long ID = 0;
cout << "请选择你需要修改职工的编号:";
cin >> ID;
int flag = -1;
for (int i = 0; i < this->WorkNum; i++)
{
if (ID == this->WorkArray[i]->m_ID)
{
flag = i;
cout << "查找结果为:";
cout << "职工ID:" << this->WorkArray[i]->m_ID << "\t\t职工名字:" << this->WorkArray[i]->m_name << "\t\t职工部门:"
<< this->WorkArray[i]->get_department() << "\t职工职责:" << this->WorkArray[i]->get_task() << endl;
int delect = 0;
cout << "再次确定修改?" << endl;
cout << "1、确定" << endl;
cout << "2、我在想想..." << endl;
cout << "请输入你的选择:";
cin >> delect;
if (delect != 1)
{
system("pause");
system("cls");
return;
}
}
}
if (flag != -1)
{
delete this->WorkArray[flag];
Work* worker = NULL;
cout << "将编号修改为:";
long long ID1 = 201604134065;
cin >> ID1;
cout << "将名字修改为:";
string name = "张三";
cin >> name;
cout << "他的岗位为:" << endl;
cout << "1、员工" << endl;
cout << "2、经理" << endl;
cout << "3、董事长" << endl;
cout << "你的选择为:";
int department_id = 3;
cin >> department_id;
switch (department_id)
{
case 1:
worker = new Employee(ID1, name, department_id);
break;
case 2:
worker = new Manager(ID1, name, department_id);
break;
case 3:
worker = new Boss(ID1, name, department_id);
break;
default:
cout << "岗位输入错误...";
system("pause");
system("cls");
return;
break;
}
this->WorkArray[flag] = worker;
cout << "信息成功修改..." << endl;
cout << "修改后信息为:" << endl;
cout << "职工ID:" << this->WorkArray[flag]->m_ID << "\t\t职工名字:" << this->WorkArray[flag]->m_name << "\t\t职工部门:"
<< this->WorkArray[flag]->get_department() << "\t职工职责:" << this->WorkArray[flag]->get_task() << endl;
this->Write();
system("pause");
system("cls");
}
else
{
cout << "查无此编号员工..." << endl;
system("pause");
system("cls");
return;
}
}
}
void WorkManage::Find_staff() //查找员工函数
{
if (this->EmptyFile)
{
cout << "文件不存在,暂无保存任何职工信息" << endl;
}
else
{
cout << "请选择你的查找方式:" << endl;
cout << "1、按照编号查找" << endl;
cout << "2、按照姓名查找" << endl;
cout << "请选择你的查找方式:";
int delect = 0;
cin >> delect;
if (delect == 1)
{
long long ID = 0;
cout << "请选择你需要查找职工的编号:";
cin >> ID;
int flag = -1;
for (int i = 0; i < this->WorkNum; i++)
{
if (ID == this->WorkArray[i]->m_ID)
{
flag = i;
cout << "职工ID:" << this->WorkArray[flag]->m_ID << "\t\t职工名字:" << this->WorkArray[flag]->m_name << "\t\t职工部门:"
<< this->WorkArray[flag]->get_department() << "\t职工职责:" << this->WorkArray[flag]->get_task() << endl;
}
}
if (flag != -1)
{
cout << "您查找的信息如上:" << endl;
}
else
{
cout << "查无此编号员工..." << endl;
system("pause");
system("cls");
return;
}
}
else if (delect == 2)
{
string name ;
cout << "请选择你需要查找职工的姓名:";
cin >> name;
int flag = -1;
for (int i = 0; i < this->WorkNum; i++)
{
if (name == this->WorkArray[i]->m_name)
{
flag = i;
cout << "职工ID:" << this->WorkArray[flag]->m_ID << "\t\t职工名字:" << this->WorkArray[flag]->m_name << "\t\t职工部门:"
<< this->WorkArray[flag]->get_department() << "\t职工职责:" << this->WorkArray[flag]->get_task() << endl;
}
}
if (flag != -1)
{
cout << "您查找的信息如上:" << endl;
}
else
{
cout << "查无此姓名的员工..." << endl;
system("pause");
system("cls");
return;
}
}
else
{
cout << "选择错误" << endl;
system("pause");
system("cls");
return;
}
}
system("pause");
system("cls");
return;
}
void WorkManage::Sort_staff() //排序函数
{
if (this->EmptyFile)
{
cout << "文件不存在,暂无保存任何职工信息" << endl;
}
else
{
cout << "请选择排序方式" << endl;
cout << "1、按照职工编号升序排序" << endl;
cout << "2、按照职工编号降序排序" << endl;
cout << "您选择的排序方式是:";
int delect;
cin >> delect;
Work* worker = NULL;
for (int i = 0; i < this->WorkNum - 1; i++)
{
for (int j = 0; j < this->WorkNum - 1-i; j++)
{
if (delect == 1) //按照升序进行排序
{
if (this->WorkArray[j]->m_ID > this->WorkArray[j + 1]->m_ID) //冒泡排序法
{
worker = this->WorkArray[j];
this->WorkArray[j] = this->WorkArray[j + 1];
WorkArray[j + 1] = worker;
}
}
else //按照降序进行排序
{
if (this->WorkArray[j]->m_ID < this->WorkArray[j + 1]->m_ID)
{
worker = this->WorkArray[j];
this->WorkArray[j] = this->WorkArray[j + 1];
WorkArray[j + 1] = worker;
}
}
}
}
this->Write();
cout << "已经成功排序..." << endl;
}
system("pause");
system("cls");
return;
}
void WorkManage::clear_all()
{
int delect = 0;
cout << "再次确定是否清空所有数据?" << endl;
cout << "1、确定" << endl;
cout << "2、我在想想..." << endl;
cout << "请输入你的选择:";
cin >> delect;
if (delect == 1)
{
ofstream ofs(FILE, ios::trunc); //删除文件后,重新创建
ofs.close();
if (this->WorkArray != NULL) //删除职工堆区每个对象
{
for (int i = 0; i < this->WorkNum; i++)
{
delete this->WorkArray[i];
this->WorkArray[i] = NULL;
}
delete[] this->WorkArray;
this->WorkArray = NULL;
this->WorkNum = 0;
this->EmptyFile = true;
}
cout << "清空成功,准备跑路..." << endl;
}
system("pause");
system("cls");
}
3、父类和子类函数
父类work函数。因为通过继承多态,所以父类只需要定义个头文件。
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Work //*** work类作为父类不需要具体实现,所以不需要创建cpp文件,在子类中具体实现。
{
public:
virtual void show_message() = 0; //显示职工信息
virtual string get_department() = 0; //获取部门信息
virtual string get_task() = 0; //获取人员工作任务
long long m_ID; //职工编号
string m_name; //职工姓名
int m_department_ID; //部门编号
};
子类中因为三个子类实现具体功能。
1、老板类
#include"Boss.h"
Boss::Boss(long long ID, string name, int departID) //构造函数初始化信息
{
this->m_ID = ID;
this->m_department_ID = departID;
this->m_name = name;
}
void Boss::show_message() //显示职工信息
{
cout << "职工编号:" << this->m_ID << "\t姓名:" << this->m_name << "\t职位:"
<< this->get_department() << "\t\t职责:" << this->get_task() <<endl;
}
string Boss::get_department() //获取部门信息
{
if (this->m_department_ID == 1)
{
return string("员 工");
}
else if (this->m_department_ID == 2)
{
return string("经 理");
}
else
{
return string("董事长");
}
}
string Boss::get_task() // //获取人员工作任务
{
return string("管理公司!");
}
2、管理类
#include"Manager.h"
Manager::Manager(long long ID, string name, int departID) //构造函数初始化信息
{
this->m_ID = ID;
this->m_department_ID = departID;
this->m_name = name;
}
void Manager::show_message() //显示职工信息
{
cout << "职工编号:" << this->m_ID << "\t姓名:" << this->m_name << "\t职位:"
<< this->get_department() << "\t\t职责:" << this->get_task() << endl;
}
string Manager::get_department() //获取部门信息
{
if (this->m_department_ID == 1)
{
return string("员 工");
}
else if (this->m_department_ID == 2)
{
return string("经 理");
}
else
{
return string("董事长");
}
}
string Manager::get_task() // //获取人员工作任务
{
return string("完成董事长的工作!");
}
3、员工类
#include"Employee.h"
Employee::Employee(long long ID,string name,int departID) //构造函数初始化信息
{
this->m_ID = ID;
this->m_department_ID = departID;
this->m_name = name;
}
void Employee::show_message() //显示职工信息
{
cout << "职工编号:" << this->m_ID << "\t姓名:" << this->m_name << "\t职位:"<<this->get_department()
<<"\t\t职责:"<< this->get_task()<<endl;
}
string Employee::get_department() //获取部门信息
{
if (this->m_department_ID == 1)
{
return string("员 工");
}
else if (this->m_department_ID == 2)
{
return string("经 理");
}
else
{
return string("董事长");
}
}
string Employee::get_task() // //获取人员工作任务
{
return string("完成董事长和经理的工作!");
}
三、总结
基本语法算是差不多学完了,后面将要进行C++的提高篇学习了,任重而道远。
四、工程文件的链接下载
对其感兴趣或者有帮助的小伙伴可以通过下面的百度网盘链接下载整个工程文件。
百度网盘链接:https://pan.baidu.com/s/1NyGKBPm41HAOHKtH4PE0YQ
提取码:xzdm