C++封装、继承、多态的应用—职工管理系统
文章目录
- C++封装、继承、多态的应用---职工管理系统
- 1.需求分析
- 2.抽象类的建立
- 2.1抽象基类
- 2.2员工类
- 2.3经理类
- 2.4老板类
- 2.5存储类
- 3.抽象类的实现
- 4.功能函数的实现
- 4.1菜单功能的实现
- 4.2增加职工功能函数实现
- 4.2显示职工功能函数实现
- 4.3删除职工功能函数实现
- 4.4修改信息功能函数实现
- 4.5查找职工功能函数实现
- 4.6职工排序功能函数实现
- 4.7清除信息功能函数实现
- 5.主函数
- 6.总结
1.需求分析
可以实现不同职工(老板、经理、员工)的增、删、改、查,以及对员工按职工编号进行排序,同时实现增删改查功能和文件的交互。
在主菜单界面显示:增加职工、显示、删除、修改、查找、排序、清空和退出系统的功能。如下图:
2.抽象类的建立
2.1抽象基类
class Base {
public:
int num;
std::string name;
std::string job;
virtual void addPerson(int &num, std::string &name, std::string &job) = 0;
virtual void deletePerson(int &num) = 0;
};
基类中定义了老板类、经理类、员工类的共同属性,将增加操作的函数声明为纯虚函数,使基类无法实例化对象,同时需要在派生类中重写基类的纯虚函数,否则派生类也为抽象类,无法实例化对象。
2.2员工类
class Staff : public Base {
public:
std::string duty = "完成经理交给的任务";
void addPerson(int &num, std::string &name, std::string &job) override ;
void deletePerson(int &num) override ;
};
员工类继承基类的属性,同时也拥有自己独一无二的职责属性duty
。
2.3经理类
class Manager : public Base {
public:
std::string duty = "完成老板交给的任务,并下发给员工";
void addPerson(int &num, std::string &name, std::string &job) override ;
void deletePerson(int &num) override ;
};
2.4老板类
class Boss : public Base {
public:
std::string duty = "管理公司所有事务";
void addPerson(int &num, std::string &name, std::string &job) override ;
void deletePerson(int &num) override ;
};
员工、经理和老板类都继承了基类中共同的属性,同时重写了基类的纯虚函数,使用了继承和多态的技术。
2.5存储类
class SaveList {
public:
int num{};
Staff staff[100];
Manager manager[100];
Boss boss[100];
};
存储类来实现不同职工信息的存储。
3.抽象类的实现
#include "myClass.h"
void Staff::addPerson(int &num, std::string &name, std::string &job) {
this->num = num;
this->name = name;
this->job = job;
}
void Staff::deletePerson(int &num) {
num = 0;
}
void Manager::addPerson(int &num, std::string &name, std::string &job) {
this->num = num;
this->name = name;
this->job = job;
}
void Manager::deletePerson(int &num) {
num = 0;
}
void Boss::addPerson(int &num, std::string &name, std::string &job) {
this->num = num;
this->name = name;
this->job = job;
}
void Boss::deletePerson(int &num) {
num = 0;
}
通过在头文件中声明类的成员函数,在源文件实现成员函数,提高代码的可读性和易维护性。
4.功能函数的实现
4.1菜单功能的实现
void printMode()
{
cout << "1.增加职工信息" << endl;
cout << "2.显示职工信息" << endl;
cout << "3.删除职工信息" << endl;
cout << "4.修改职工信息" << endl;
cout << "5.查找职工信息" << endl;
cout << "6.排序职工信息" << endl;
cout << "7.清空职工信息" << endl;
cout << "0.退出系统" << endl;
cout << "请选择(0-7): ";
}
通过cout
实现简易的菜单界面。
4.2增加职工功能函数实现
void addEmp(SaveList &saveList)
{
auto *staff = new Staff();
auto *manager = new Manager();
auto *boss = new Boss();
int num = 0;
string name;
string job;
cout << "请输入职工信息:" << endl;
cout << "请输入职工编号:";
cin >> num;
cout << "请输入职工姓名:";
cin >> name;
cout << "请输入职工职位:";
cin >> job;
if (job == "经理")
{
manager->addPerson(num, name, job);
saveList.manager[saveList.num] = *manager;
saveList.num++;
file.open("data.txt", ios::app);
for (int i = 0; i < saveList.num; i++) {
if (saveList.manager[i].num != 0) {
file << saveList.manager[i].num << " " << saveList.manager[i].name << " " << saveList.manager[i].job
<< " " << saveList.manager[i].duty << endl;
}
}
file.close();
}
else if (job == "老板")
{
boss->addPerson(num, name, job);
saveList.boss[saveList.num] = *boss;
saveList.num++;
file.open("data.txt", ios::app);
for (int i = 0; i < saveList.num; i++) {
if (saveList.boss[i].num != 0) {
file << saveList.boss[i].num << " " << saveList.boss[i].name << " " << saveList.boss[i].job
<< " " << saveList.boss[i].duty << endl;
}
}
file.close();
}
else if (job == "员工")
{
staff->addPerson(num, name, job);
saveList.staff[saveList.num] = *staff;
saveList.num++;
file.open("data.txt", ios::app);
for (int i = 0; i < saveList.num; i++)
{
if (saveList.staff[i].num != 0){
file << saveList.staff[i].num << " " << saveList.staff[i].name << " " << saveList.staff[i].job << " " << saveList.staff[i].duty << endl;
}
}
file.close();
}
}
增加职工通过调用不同类的成员函数,增加不同岗位的职工,同时将不同岗位的职工数据存入各自的存储数组中,同时打开文件,将写文件方式设置为app
(追加模式)解决每次写文件时对上次内容覆盖的问题。
4.2显示职工功能函数实现
void showEmp(SaveList &saveList)
{
if (saveList.num == 0)
{
cout << "没有职工信息" << endl;
}
else
{
for (int i = 0; i < saveList.num; i++)
{
if (saveList.manager[i].num != 0){
cout << "职工编号:" << saveList.manager[i].num << "\t\t";
cout << "职工姓名:" << saveList.manager[i].name << "\t\t";
cout << "职工职位:" << saveList.manager[i].job << "\t\t";
cout << "职工职责:" << saveList.manager[i].duty << endl;
}
}
for (int i = 0; i < saveList.num; i++)
{
if (saveList.boss[i].num != 0){
cout << "职工编号:" << saveList.boss[i].num << "\t\t";
cout << "职工姓名:" << saveList.boss[i].name << "\t\t";
cout << "职工职位:" << saveList.boss[i].job << "\t\t";
cout << "职工职责:" << saveList.boss[i].duty << endl;
}
}
for (int i = 0; i < saveList.num; i++)
{
if (saveList.staff[i].num != 0){
cout << "职工编号:" << saveList.staff[i].num << "\t\t";
cout << "职工姓名:" << saveList.staff[i].name << "\t\t";
cout << "职工职位:" << saveList.staff[i].job << "\t\t";
cout << "职工职责:" << saveList.staff[i].duty << endl;
}
}
}
}
对不同存储数组进行遍历,将内容输出。
4.3删除职工功能函数实现
void deleteEmp(SaveList &saveList)
{
if (saveList.num == 0)
{
cout << "没有职工信息" << endl;
}
else
{
int num;
cout << "请输入要删除的职工编号:";
cin >> num;
for (int i = 0; i < saveList.num; i++)
{
if (saveList.manager[i].num == num)
{
saveList.manager[i].num = 0;
saveList.manager[i].name = "";
saveList.num--;
}
if (saveList.boss[i].num == num)
{
saveList.boss[i].num = 0;
saveList.boss[i].name = "";
saveList.num--;
}
if (saveList.staff[i].num == num)
{
saveList.staff[i].num = 0;
saveList.staff[i].name = "";
saveList.num--;
}
}
}
}
从显示函数部分知道,遍历每个数组时,会剔除编号为0的职工信息,故只需将职工的编号置为0实现这部分职工不显示,从侧面实现职工删除。
4.4修改信息功能函数实现
void modifyEmp(SaveList &saveList)
{
auto *boss = new Boss();
auto *staff = new Staff();
auto *manager = new Manager();
if (saveList.num == 0)
{
cout << "没有职工信息" << endl;
}
else
{
int num;
cout << "请输入要修改的职工编号:";
cin >> num;
for (int i = 0; i < saveList.num; i++)
{
if (saveList.manager[i].num == num)
{
cout << "请输入职工姓名:";
cin >> saveList.manager[i].name;
cout << "请输入职工职位:";
cin >> saveList.manager[i].job;
if (saveList.manager[i].job == "老板"){
num = saveList.manager[i].num;
boss->addPerson(num, saveList.manager[i].name, saveList.manager[i].job);
saveList.boss[saveList.num] = *boss;
saveList.manager[i].num = 0;
saveList.num++;
return;
}
if (saveList.manager[i].job == "员工"){
staff->addPerson(num, saveList.manager[i].name, saveList.manager[i].job);
saveList.staff[saveList.num] = *staff;
saveList.manager[i].num = 0;
saveList.num++;
return;
}
}
if (saveList.boss[i].num == num)
{
cout << "请输入职工姓名:";
cin >> saveList.boss[i].name;
cout << "请输入职工职位:";
cin >> saveList.boss[i].job;
if (saveList.boss[i].job == "经理"){
manager->addPerson(num, saveList.boss[i].name, saveList.boss[i].job);
saveList.manager[saveList.num] = *manager;
saveList.num++;
return;
}
if (saveList.boss[i].job == "员工"){
staff->addPerson(num, saveList.boss[i].name, saveList.boss[i].job);
saveList.staff[saveList.num] = *staff;
saveList.num++;
return;
}
}
if (saveList.staff[i].num == num)
{
cout << "请输入职工姓名:";
cin >> saveList.staff[i].name;
cout << "请输入职工职位:";
cin >> saveList.staff[i].job;
if (saveList.staff[i].job == "经理"){
manager->addPerson(num, saveList.staff[i].name, saveList.staff[i].job);
saveList.manager[saveList.num]= *manager;
saveList.num++;
return;
}
if (saveList.staff[i].job == "老板"){
boss->addPerson(num, saveList.staff[i].name, saveList.staff[i].job);
saveList.boss[saveList.num] = *boss;
saveList.num++;
return;
}
}
}
}
}
在修改信息的过程中,如果修改的内容不包括岗位时,只需要对原始数据进行覆盖操作,但修改岗位时,事实上此时应该将数据更改存储位置,以保证默认值duty
可以正确输出,所以可以认为修改岗位时相当于为其他岗位增加了一个成员,使本岗位减少了一个成员。
4.5查找职工功能函数实现
void findEmp(SaveList &saveList)
{
if (saveList.num == 0)
{
cout << "没有职工信息" << endl;
}
else
{
int num;
cout << "请输入要查找的职工编号:";
cin >> num;
for (int i = 0; i < saveList.num; i++)
{
if (saveList.manager[i].num == num)
{
cout << "职工编号:" << saveList.manager[i].num << "\t\t";
cout << "职工姓名:" << saveList.manager[i].name << "\t\t";
cout << "职工职位:" << saveList.manager[i].job << "\t\t";
cout << "职工职责:" << saveList.manager[i].duty << endl;
}
if (saveList.boss[i].num == num)
{
cout << "职工编号:" << saveList.boss[i].num << "\t\t";
cout << "职工姓名:" << saveList.boss[i].name << "\t\t";
cout << "职工职位:" << saveList.boss[i].job << "\t\t";
cout << "职工职责:" << saveList.boss[i].duty << endl;
}
if (saveList.staff[i].num == num)
{
cout << "职工编号:" << saveList.staff[i].num << "\t\t";
cout << "职工姓名:" << saveList.staff[i].name << "\t\t";
cout << "职工职位:" << saveList.staff[i].job << "\t\t";
cout << "职工职责:" << saveList.staff[i].duty << endl;
}
}
}
}
通过遍历每个存储数组,将存储内容输出。
4.6职工排序功能函数实现
void sortEmp(SaveList &saveList)
{
if (saveList.num == 0)
{
cout << "没有职工信息" << endl;
}
else
{
for (int i = 0; i < saveList.num; i++)
{
for (int j = 0; j < saveList.num - i - 1; j++)
{
if (saveList.manager[j].num > saveList.manager[j + 1].num)
{
swap(saveList.manager[j], saveList.manager[j + 1]);
}
if (saveList.boss[j].num > saveList.boss[j + 1].num)
{
swap(saveList.boss[j], saveList.boss[j + 1]);
}
if (saveList.staff[j].num > saveList.staff[j + 1].num)
{
swap(saveList.staff[j], saveList.staff[j + 1]);
}
}
}
}
}
通过冒泡排序将职工按编号进行升序排列(编号小的先显示)。
4.7清除信息功能函数实现
void deleteAll(SaveList &saveList)
{
if (saveList.num == 0)
{
cout << "没有职工信息" << endl;
}
else
{
for (int i = 0; i < saveList.num; i++)
{
saveList.manager[i].num = 0;
saveList.boss[i].num = 0;
saveList.staff[i].num = 0;
}
saveList.num = 0;
file.open("data.txt", ios::out);
file << " " << endl;
file.close();
}
}
参考删除职工的操作,将所有信息“清除”,同时以普通模式向文件中写入空格,覆盖原来的信息。
5.主函数
#include <iostream>
#include "myFunc.h"
#include "myClass.h"
using namespace std;
int mode = 1;
string str;
int main() {
SaveList saveList;
while (mode != 0){
printMode();
cin >> mode;
switch (mode) {
case 1:
label:
addEmp(saveList);
cout << "是否继续添加:"
<< "1.继续添加"
<< "2.退出" << endl;
cin >> str;
if (str == "2") {
break;
}
if (str == "1"){
goto label;
}
case 2:
label2:
showEmp(saveList);
cout << "是否继续查看:"
<< "1.继续查看"
<< "2.退出" << endl;
cin >> str;
if (str == "2") {
break;
}
if (str == "1"){
goto label2;
}
case 3:
label3:
deleteEmp(saveList);
cout << "是否继续删除:"
<< "1.继续删除"
<< "2.退出" << endl;
cin >> str;
if (str == "2") {
break;
}
if (str == "1"){
goto label3;
}
case 4:
label4:
modifyEmp(saveList);
cout << "是否继续修改:"
<< "1.继续修改"
<< "2.退出" << endl;
cin >> str;
if (str == "2") {
break;
}
if (str == "1"){
goto label4;
}
case 5:
label5:
findEmp(saveList);
cout << "是否继续查找:"
<< "1.继续查找"
<< "2.退出" << endl;
cin >> str;
if (str == "2") {
break;
}
if (str == "1"){
goto label5;
}
case 6:
sortEmp(saveList);
cout << "排序成功" << endl;
break;
case 7:
cout << "确定删除所有员工?:";
cin >> str;
if (str == "是"){
deleteAll(saveList);
cout << "删除成功" << endl;
break;
}
default:
cout << "欢迎使用" << endl;
}
}
return 0;
}
通过switch
实现不同功能函数的调用,同时引入goto
语句,实现对同一个功能函数的重复调用。
6.总结
通过这个简单的案例,可以加深对基础语法的理解,以及夯实基础知识,为学习更加复杂的知识打下基础。