c++使用多态实现职工管理系统源码

1.职工管理系统.cpp

#include<iostream>
using namespace std;
#include "workManger.h"
#include "worker.h"
#include"employ.h"
#include"manger.h"
#include"boss.h"
int main()
{
	WorkManger wm;
	int choice = 0;
		while (true)
		{
			//调用菜单函数
			wm.ShowMenu();
			cout << "请输入您的选择: " << endl;
			cin >> choice;
			switch (choice)
			{
			case 0:
				//退出系统
				wm.ExitSystem();
				break;
			case 1:
				//添加职工信息
				wm.AddSystem();
				break;
			case 2:
				//显示职工信息
				wm.ShowSystem();
				break;
			case 3:
				//删除职工信息
				wm.DeleteSystem();
				break;
			case 4:
				//修改职工信息
				wm.ModifySystem();
				break;
			case 5:
				//查找职工信息
				wm.FindSystem();
				break;
			case 6:
				//按照职工编号升序或降序
				wm.SortSystem();
				break;
			case 7:
				//清空职工信息
				wm.CleanSystem();
				break;
			default:
				system("cls");//cls清屏操作
				break;
			}
	
		}
	
	system("pause");
	return 0;
}

2.workManger.h

#pragma once
#include<iostream>
#include"worker.h"
#include<fstream>
#define FILENAME "emFile.txt"
using namespace std;
class WorkManger
{
public:
	//构造函数,用于成员变量的赋初值操作
	WorkManger();
	//展示菜单,用于打印菜单
	void  ShowMenu();
	//退出系统
	void ExitSystem();
	//记录员工个数
	int empNum;
	//员工数组的指针
	worker** empArry;
	//添加员工函数声明
	void AddSystem();
	//保存文件
	void save();
	//判断文件是否为空
	bool m_FileIsEmpty;
	//用于记录文本数据员工个数
	int get_empNum();
	//初始化职工
	void init_emp();
	//显示职工声明
	void ShowSystem();
	//删除职工函数声明
	void DeleteSystem();
	//判断职工是否存在,存在返回该职工在数组中的地址,不存在返回-1;
	int IsExist(int id);
	//修改职工函数声明
	void ModifySystem();
	//查找职工函数声明
	void FindSystem();
	//排序函数实现
	void SortSystem();
	//清空函数实现
	void CleanSystem();
	//析构函数用于堆区释放
	~WorkManger();
	
};

3.workManger.cpp


#include"WorkManger.h"
#include"worker.h"
#include"employ.h"
#include"manger.h"
#include"boss.h"
WorkManger::WorkManger()
{
    //文件不存在
    ifstream ifs;
    ifs.open(FILENAME, ios::in);
    if (!ifs.is_open() )
    {
        //cout << "文件不存在" << endl;
        //初始化记录人数
        this->empNum = 0;
        //初始化数组指针
        this->empArry = NULL;
        //判断文件是否为空
        this->m_FileIsEmpty = true;
        //关闭文件
        ifs.close();
        return;
    }
    //文件存在但数据为空时
    char ch;
    ifs >> ch;
    if (ifs.eof())
    {
        //cout << "文件存在但数据为空" << endl;
        this->empNum = 0;
        this->empArry = NULL;
        this->m_FileIsEmpty = true;
        ifs.close();
        return;
    }
    //文件存在且数据存在
    int num = this->get_empNum();
    //cout << "职工人数为: " << num << endl;
    this->empNum = num;
    this->empArry = new worker * [this->empNum];
    this->init_emp();
    /*for (int i = 0; i < this->empNum;i++)
    {
        cout << "职工编号" << this->empArry[i]->m_id;
        cout << "职工姓名" << this->empArry[i]->m_name;
        cout << "部门编号" << this->empArry[i]->m_departid;
    }*/
}
void WorkManger::ShowMenu()
{
    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 WorkManger::ExitSystem()
{
    cout << "欢迎下次使用" << endl;
    system("pause");
    exit(0);
}
void WorkManger::AddSystem()
{
    cout << "请输入需要添加职工的数量: " << endl;
    int addNum = 0;
    cin >>addNum;
    if (addNum > 0)
    {
        //添加
        //添加后的员工个数=之前员工个数+添加的员工个数
        int newSize = empNum + addNum;
        worker** newSpace = new worker * [newSize];
        if(this->empArry != NULL)
        {
            for ( int i = 0; i < this->empNum; i++)
            {
                newSpace[i] = this->empArry[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* pworker = NULL;
            switch (dselect)
            {
            case 1:
                pworker = new employee(id,name,1);
                break;
            case 2:
                pworker = new manger(id, name, 2);
                break;
            case 3:
                pworker = new boss(id, name, 3);
                break;
            default:
                break;
            }
            newSpace[this->empNum + i] = pworker;

        }
        //将新的数组指针指向的地址赋给empArry[]数组,将新的员工数量付给emNum
        delete [] this->empArry;
        this->empArry = newSpace;
        this->empNum = newSize;
        cout<<"成功添加了"<<addNum<<"名新职工" << endl;
        //添加成功后保存数据
        this->save();

    }
    else
    {
        cout << "输入有误,请重新输入" << endl;
    }
    //按任意键继续
    system("pause");
    //清屏重新选择
    system("cls");
}
//写文件操作
void WorkManger::save()
{
    ofstream ofs;
    ofs.open(FILENAME, ios::out);
    for (int i = 0; i < this->empNum; i++)
    {
        ofs << this->empArry[i]->m_id << " "
            << this->empArry[i]->m_name << " "
            << this->empArry[i]->m_departid << endl;
    }
    ofs.close();
}
int WorkManger::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 WorkManger::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* pworker = NULL;
        if (did == 1)
        {
            pworker = new employee(id, name, did);
        }
        else if (did == 2)
        {
            pworker = new manger(id, name, did);
        }
        else
        {
            pworker = new boss(id, name, did);
        }
        this->empArry[index] = pworker;
        index++;
    }
    ifs.close();

}
void WorkManger::ShowSystem()
{
    if (this->m_FileIsEmpty)
    {
        cout << "文件不存在或记录为空!" << endl;
    }
    else
    {
        for (int i = 0; i < empNum; i++)
        {
            this->empArry[i]->showInfo();
        }
    }
    system("pause");
    system("cls");
}
int WorkManger::IsExist(int id)
{
    int index = -1;
    for (int i = 0; i < this->empNum; i++)
    {
        if (this->empArry[i]->m_id == id)
        {
            index = i;
            break;
        }
    }
    return index;
}
void WorkManger::DeleteSystem()
{
         if (this->m_FileIsEmpty)
        {
               cout << "文件不存在或者记录为空" << endl;
        }
        else
        {
            cout << "请输入想要删除的职工编号: " << endl;
            int id = 0;
            cin >> id;
            //将要删除位置地址给index
            int index = this->IsExist(id);
            if (index != -1)
            {
                //从要删除位置下标开始循环到结尾,由于下标从0开始,所以是empNum-1
                for (int i = index; i < this->empNum - 1; i++)
                {
                    //将要删除数据后的数据全部前移一位
                    this->empArry[i] = this->empArry[i + 1];
                }
                //统计职工个数减1
                this->empNum--;
                //重新保存数据
                this->save();
                cout << "删除成功" << endl;
            }
            else
            {
                cout << "删除失败,未找到该员工" << endl;
            }
        }
    system("pause");
    system("cls");
}
void WorkManger::ModifySystem()
{
    if (this->m_FileIsEmpty)
    {
        cout << "文件不存在或数据为空" << endl;

    }
    else
    {
        cout << "请输入修改职工编号: " << endl;
        int id = 0;
        cin>> id;
        int ret = this->IsExist(id);
        if (ret != -1)
        {
            delete this->empArry[ret];
            int newid = 0;
            string newname=" ";
            int deselect=0;
            cout << "查到: " << id << "号职工,请输入新职工号:" << endl;
            cin >> newid;
            cout << "请输入职工姓名: " << endl;
            cin >> newname;
            cout << "请输入职工所在岗位" << endl;
            cout << "1.普通职工" << endl;
            cout << "2.经理" << endl;
            cout << "3.老板" << endl;
            cin >> deselect;
            worker* pworker = NULL;
            switch (deselect)
            {
            case 1:
                pworker = new employee(newid, newname, deselect);
                break;
            case 2:
                pworker = new manger(newid, newname, deselect);
                break;
            case 3:
                pworker = new boss(newid, newname, deselect);
                break;               
            default:
                break;
            }
            this->empArry[ret] = pworker;
            cout << "修改成功" << endl;
            this->save();

        }
        else
        {
            cout << "修改失败" << endl;
        }
    }
    system("pause");
    system("cls");
}
void WorkManger::FindSystem()
{
    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->empArry[ret]->showInfo();
            }
            else
            {
                cout << "查找失败,查无此人" << endl;
            }
        }
        else if (select == 2)
        {
            string name;
            cout << "请输入查找职工姓名: " << endl;
            cin >> name;
            bool flag = false;
            for (int i = 0; i < empNum; i++)
            {
                if (this->empArry[i]->m_name == name)
                {
                    cout << "查找成功,职工编号为:"
                        << this->empArry[i]->m_id
                        << "号职工信息如下: " << endl;
                    flag = true;
                    this->empArry[i]->showInfo();
                }
            }
            if (flag == false)
            {
                cout << "查找失败,查无此人" << endl;
            }
        }
        else
        {
            cout << "输入选项有误" << endl;
        }
    }
    system("pause");
    system("cls");
}
void WorkManger::SortSystem()
{
    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 < empNum; i++)
        {
            int minormax =i;
            for (int j = i + 1; j < this->empNum; j++)
            {
                if (select == 1)
                {
                    if (this->empArry[minormax]->m_id > this->empArry[j]->m_id)
                    {
                        minormax = j;
                    }
                }
                else 
                {
                    if (this->empArry[minormax]->m_id < this->empArry[j]->m_id)
                    {
                        minormax = j;
                    }
                }
            }
            if (i != minormax)
            {
                worker* temp = this->empArry[i];
                this->empArry[i] = this->empArry[minormax];
                this->empArry[minormax] = temp;
            }
        }
        cout << "排序成功!排序后的结果为:" << endl;
        this->save();
        this->ShowSystem();
    }
}
void WorkManger::CleanSystem()
{
    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->empArry != NULL)
        {
            for (int i = 0; i < this->empNum; i++)
            {
                delete this->empArry[i];
                this->empArry[i] = NULL;
            }
            delete[] this->empArry;
            this->empArry = NULL;
            this->empNum = 0;
            this->m_FileIsEmpty = true;
        }
        cout << "清空成功!" << endl;
    }
    system("pause");
    system("cls");
}
WorkManger::~WorkManger()
{
    if (this->empArry != NULL)
    {
        //如果empArry数组指针指向地址不为空,则delete堆区释放该处地址,
        //并手动置空,防止内存泄漏
        delete[] this->empArry;
        this->empArry = NULL;
    }
}

4.worker.h

#pragma once
#include<iostream>
#include<string>
using namespace std;
class worker
{
public:
	//员工个人信息
	virtual void showInfo() = 0;
	// 员工所在岗位信息
	virtual string getDep() = 0;
	//员工编号
	int m_id;
	//员工姓名
	string m_name;
	//员工所在岗位
	int m_departid;
};

5.manger.h

#pragma once
#include<iostream>
#include"worker.h"
using namespace std;
class manger :public worker
{
public:
	//员工构造函数声明
	manger(int id, string name, int did);
	//员工个人信息函数声明
	virtual void showInfo();
	// 员工所在岗位函数声明
	virtual string getDep();
};

6.manger.cpp 

#include"manger.h"
//经理构造函数实现
manger::manger(int id, string name, int did)
{
	//通过有参构造函数实现 经理编号,姓名,所在岗位初始化
	this->m_id = id;
	this->m_name = name;
	this->m_departid = did;
}
//经理个人信息函数实现
 void manger::showInfo()
{
	 cout << "经理的编号:" << this->m_id;
	 cout << "\t经理的姓名:" << this->m_name;
	 cout << "\t经理的岗位:" << this->getDep();
	 cout << "\t所在岗位职责:完成老板交给的任务,并且分配任务给员工" << endl;

}
// 经理所在岗位函数实现
 string manger::getDep()
{
	 return string("经理");
}

7.employ.h

#pragma once
#include<iostream>
#include"worker.h"
using namespace std;
class employee:public worker
{
public:
	//员工构造函数
	employee(int id,string name,int did);
	//员工个人信息
	virtual void showInfo();
	// 员工所在岗位
	virtual string getDep() ;
};

8.employee.cpp

#include"employ.h"
//员工构造函数
employee::employee(int id, string name, int did)
{
	//通过有参构造函数实现 员工编号,姓名,所在岗位初始化
	this->m_id = id;
	this->m_name = name;
	this->m_departid = did;

}
//员工个人信息
 void employee::showInfo()
{
	 cout << "职工的编号:" << this->m_id;
	 cout << "\t职工的姓名:" << this->m_name;
	 cout << "\t职工的岗位:" << this->getDep();
	 cout << "\t所在岗位职责:完成经理交给的任务" << endl;
}
// 员工所在岗位信息
string employee::getDep()
{
	 return string("员工");
}

9.boss.h

#pragma once
#include<iostream>
#include"worker.h"
using namespace std;
class boss :public worker
{
public:
	//员工构造函数声明
	boss(int id, string name, int did);
	//员工个人信息函数声明
	virtual void showInfo();
	// 员工所在岗位函数声明
	virtual string getDep();
};

10.boss.cpp

#include"boss.h"
//经理构造函数实现
boss::boss(int id, string name, int did)
{
	//通过有参构造函数实现 经理编号,姓名,所在岗位初始化
	this->m_id = id;
	this->m_name = name;
	this->m_departid = did;
}
//经理个人信息函数实现
void boss::showInfo()
{
	cout << "老板的编号:" << this->m_id;
	cout << "\t老板的姓名:" << this->m_name;
	cout << "\t老板的岗位:" << this->getDep();
	cout << "\t所在岗位职责:完成公司所有事物" << endl;

}
// 经理所在岗位函数实现
string boss::getDep()
{
	return string("老板");
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值