C + + employee management system based on polymorphism

Time:2021-9-27

Main module (employee management system. CPP)

include

using namespace std;

include “workerManger.h”

include “worker.h”

include “employee.h”

include “Boss.h”

include “Manager.h”

int main()

{

//Instantiate a manager object, foreign exchange supervision http://www.fx61.com/supervision

WorkerManger wm;

int choice = 0;

while (true)

{

//Call member function to display menu

wm.Show_Menu();

Cout < < please enter your choice: < < endl;

cin >> choice;

switch (choice)

{

Case 0: / / exit

wm.ExitSystem();

break;

Case 1: / / add

wm.Add_Emp();

break;

Case 2: / / display

wm.Show_Emp();

break;

Case 3: / / delete

wm.Del_Emp();

break;

Case 4: / / modify

wm.Mod_Emp();

break;

Case 5: / / find

wm.Find_Emp();

break;

Case 6: / / sort

wm.Sort_Emp();

break;

Case 7: / / empty

wm.Clean_File();

break;

default:

system(“cls”);// Clear screen

break;

}

}

system(“pause”);

return 0;

}

Workermanger. H file:

Pragma once / / prevent header files from being included repeatedly

include

include

include “worker.h”

include “employee.h”

include “Boss.h”

include “Manager.h”

define FILENAME “empFile.txt”

using namespace std;

class WorkerManger

{

public:

//Constructor

WorkerManger();

//Display menu

void Show_Menu();

//Exit the system

void ExitSystem();

//Record the number of employees

int m_EmpNum;

//Employee array pointer

Worker ** m_EmpArray;

//Add employee

void Add_Emp();

//Save file

void save();

//Flag to judge whether the file is empty

bool m_FileIsEmpty;

//Number of people in statistics file

int get_EmpNum();

//Initialize employee

void init_Emp();

//Show employees

void Show_Emp();

//Delete employee

void Del_Emp();

//Judge whether the employee exists

int IsExist(int id);

//Modify employee

void Mod_Emp();

//Find employees

void Find_Emp();

//Sort by number

void Sort_Emp();

//Empty file

void Clean_File();

//Destructor

~WorkerManger();

};

Workermanger.cpp file:

include “workerManger.h”

WorkerManger::WorkerManger()

{

//1. File does not exist

ifstream ifs;

ifs.open(FILENAME, ios::in);

if (!ifs.is_open())

{

this->m_ EmpNum = 0; // Number of initialization records

this->m_ EmpArray = NULL; // Initialize array pointer

this->m_ FileIsEmpty = true; // Is the initialization file empty

ifs.close();

return;

}

//File exists, data is empty

char ch;

ifs >> ch;

if (ifs.eof())

{

this->m_ EmpNum = 0; // Number of initialization records

this->m_ EmpArray = NULL; // Initialize array pointer

this->m_ FileIsEmpty = true; // Is the initialization file empty

ifs.close();

return;

}

//When the file exists and data is recorded

int num = this->get_EmpNum();

this->m_EmpNum = num;

this->m_EmpArray = new Worker*[this->m_EmpNum];

this->init_Emp();

}

//Display menu

void WorkerManger::Show_Menu()

{

cout << “*********************************************” << endl;

Cout < “******************** welcome to the employee management system! ***********” < < endl;

Cout < “*****************************************************************************;

Cout < “*****************************************************************************;

Cout < “*****************************************************************************;

Cout < “*****************************************************************************;

Cout < “*****************************************************************************;

Cout < “*****************************************************************************;

Cout < “*****************************************************************************;

Cout < “***************************************************************************************************;

cout << “*********************************************” << endl;

cout << endl;

}

//Add employee

void WorkerManger::Add_Emp()

{

Cout < < “please enter the number of employees added:” < endl;

int addNum = 0; // Save user input quantity

cin >> addNum;

if(addNum > 0)

{

//Calculates the size of the added new space

int newSize = this->m_ EmpNum + addNum; // Number of people in new space = number of people originally recorded + number of people newly added

//Open up new space

Worker ** newSpace = new Worker*[newSize];

//Copy the data in the original space to the new space

if (this->m_EmpArray != NULL)

{

for (int i = 0; i < this->m_EmpNum; i++)

{

newSpace[i] = this->m_EmpArray[i];

}

}

//Batch add new data

for (int i = 0; i < addNum; i++)

{

int id; // Employee number

string name; // Employee name

int dSelect; // Department selection

Cout < < “please enter the number” < < I + 1 < < “new employee number:” < endl;

cin >> id;

Cout < < “please enter the first” < < I + 1 < < “new employee name:” < endl;

cin >> name;

Cout < < please select this employee position < < endl;

Cout < < 1. Ordinary employees < < endl;

Cout < < 2. Manager < < endl;

Cout < < 3. Boss < < 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;

}

//Create employee responsibilities and save them to the array

newSpace[this->m_EmpNum + i] = worker;

}

//Release the original space

delete[] this->m_EmpArray;

//Change the orientation of the new space

this->m_EmpArray = newSpace;

//Update the number of new employees

this->m_EmpNum = newSize;

this->m_ FileIsEmpty = false; // Employee cannot be empty

//Prompt for successful addition

Cout < < successfully added “< < addnum < < new employees!” < < endl;

//Save data to file

this->save();

}

else

{

Cout < < wrong input! “< < endl;

}

//Press any key to clear the screen and return to the superior directory

system(“pause”);

system(“cls”);

}

//Number of people in statistics file

int WorkerManger::get_EmpNum()

{

ifstream ifs;

ifs.open(FILENAME, ios::in); // Open file read file

int id;

string name;

int dId;

int num = 0;

while (ifs >> id && ifs >> name && ifs >> dId)

{

//Number of Statistics

num++;

}

return num;

}

//Exit the system

void WorkerManger::ExitSystem()

{

Cout < < welcome to use next time < < endl;

system(“pause”);

exit(0);// Exit program

}

//Save file

void WorkerManger::save()

{

ofstream ofs;

ofs.open(FILENAME, ios::out); // Open file as output — write file

//Write everyone’s data to a file

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;

}

//Close file

ofs.close();

}

//Initialize employee

void WorkerManger::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) / / ordinary employee

{

worker = new Employee(id, name, dId);

}

Else if (did = = 2) / / Manager

{

worker = new Manager(id, name, dId);

}

else

{

worker = new Boss(id, name, dId);

}

this->m_EmpArray[index] = worker;

index++;

}

ifs.close();

}

//Show employees

void WorkerManger::Show_Emp()

{

//Determine whether the file is empty

if (this->m_FileIsEmpty)

{

Cout < < file does not exist or record is empty! < < endl;

}

else

{

for (int i = 0; i < m_EmpNum; i++)

{

//Using polymorphic calling program interface

this->m_EmpArray[i]->showInfo();

}

}

system(“pause”);

system(“cls”);

}

//Delete employee

void WorkerManger::Del_Emp()

{

if (this->m_FileIsEmpty)

{

Cout < < file does not exist or record is empty! < < endl;

}

else

{

//Delete employee by number

Cout < < “please enter the employee number you want to delete:” < endl;

int id = 0;

cin >> id;

int index = this->IsExist(id);

If (index! = – 1) / / the employee exists and you want to delete the employee at the index position

{

//Data forward

for (int i = index; i < this->m_EmpNum – 1; i++)

{

this->m_EmpArray[i] = this->m_EmpArray[i + 1];

}

this->m_ EmpNum–;// Update the number of people recorded in the array

//Synchronize updates to files

this->save();

Cout < < delete succeeded! “< < endl;

}

else

{

Cout < < delete failed! The employee was not found! < < endl;

}

}

system(“pause”);

system(“cls”);

}

//Judge whether the employee exists

int WorkerManger::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;

}

//Modify employee

void WorkerManger::Mod_Emp()

{

if (this->m_FileIsEmpty)

{

Cout < < file does not exist or record is empty! < < endl;

}

else

{

Cout < < “please enter the employee number to modify:” < endl;

int id;

cin >> id;

int ret = this->IsExist(id);

If (RET! = – 1) / / find the employee

{

delete this->m_EmpArray[ret];

int newId = 0;

string newName = “”;

int dSelect = 0;

Cout < < found: < < ID < < employee No., please enter the new employee No.: < < endl;

cin >> newId;

Cout < < please enter a new name: “< endl;

cin >> newName;

Cout < < please enter position: < < endl;

Cout < < 1. Ordinary employees < < endl;

Cout < < 2. Manager < < endl;

Cout < < 3. Boss < < 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;

}

//Update data

this->m_EmpArray[ret] = worker;

Cout < < modification succeeded! “< < endl;

this->save(); // Save to file

}

else

{

Cout < < “modification failed! No one found!” < < endl;

}

}

system(“pause”);

system(“cls”);

}

//Find employees

void WorkerManger::Find_Emp()

{

if (this->m_FileIsEmpty)

{

Cout < < file does not exist or record is empty! < < endl;

}

else

{

Cout < < please enter the search method: < < endl;

Cout < < 1. Search by employee number < < endl;

Cout < < 2. Search by employee name < < endl;

int select;

cin >> select;

if (select == 1)

{

//By number

int id;

Cout < < “please enter the employee number you are looking for:” < endl;

cin >> id;

int ret = this->IsExist(id);

if (ret != -1)

{

//Find employees

Cout < < search succeeded! The employee’s information is as follows: < < endl;

this->m_EmpArray[ret]->showInfo();

}

else

{

Cout < < search failed! There is no such person found! < < endl;

}

}

else if(select == 2)

{

//By name

string name;

Cout < < “please enter the name you want to find:”;

cin >> name;

bool flag = false; // Judge whether it is found. It is not found by default

for (int i = 0; i < m_EmpNum; i++)

{

if (this->m_EmpArray[i]->m_Name == name)

{

Cout < < “search succeeded, employee number is”

<< this->m_EmpArray[i]->m_ID

< < the employee information is as follows: < < endl;

this->m_EmpArray[i]->showInfo();

flag = true;

}

}

if (flag == false)

{

Cout < < search failed! There is no such person found! < < endl;

}

}

else

{

Cout < < wrong input options! “< < endl;

}

}

system(“pause”);

system(“cls”);

}

//Sort

void WorkerManger::Sort_Emp()

{

if (this->m_FileIsEmpty)

{

Cout < < file does not exist or record is empty! < < endl;

system(“pause”);

system(“cls”);

}

else

{

Cout < < please select sorting method: < < endl;

Cout < < 1. Arrange in ascending order according to employee number < < endl;

Cout < < 2. Arrange in descending order according to employee number < < endl;

int select;

cin >> select;

for (int i = 0; i < this->m_EmpNum; i++)

{

int MinOrMax = i;

for (int j = i + 1; j < this->m_EmpNum; j++)

{

If (select = = 1) / / ascending

{

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;

}

}

}

//Judge whether the minimum value or maximum value recognized at the beginning is the calculated minimum value or maximum value. If not, exchange

if (i != MinOrMax)

{

Worker * temp = this->m_EmpArray[i];

this->m_EmpArray[i] = this->m_EmpArray[MinOrMax];

this->m_EmpArray[MinOrMax] = temp;

}

}

Cout < < sorting succeeded! The sorted result is: < < endl;

this->save();// Save sorted results to file

this->Show_ Emp();// Show all employees

}

}

//Empty file

void WorkerManger::Clean_File()

{

Cout < < are you sure to clear? < < endl;

Cout < < 1. Confirm < < endl;

Cout < < 2. Return < < endl;

int select = 0;

cin >> select;

if (select == 1)

{

ofstream ofs(FILENAME, ios::trunc);// Delete the file and recreate it

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 heap array pointer

delete[] this->m_EmpArray;

this->m_EmpArray = NULL;

this->m_EmpNum = 0;

this->m_FileIsEmpty = true;

}

Cout < < empty successfully! “< < endl;

}

system(“pause”);

system(“cls”);

}

WorkerManger::~WorkerManger()

{

if (this->m_EmpArray != NULL)

{

delete[] this->m_EmpArray;

this->m_EmpArray = NULL;

}

}

Worker. H file:

pragma once

include

using namespace std;

include

//Employee abstract class

class Worker

{

public:

//Display personal information

virtual void showInfo() = 0;

//Get position name

virtual string getDeptName() = 0;

int m_ ID; // Employee number

string m_ Name; // Employee name

int m_ DeptID; // Department number

};

Employee. H file:

//General staff documents

pragma once

include

include

using namespace std;

include “worker.h”

class Employee : public Worker

{

public:

//Constructor

Employee(int id, string name, int dID);

//Display personal information

void showInfo();

//Get position name

string getDeptName();

};

Employee.cpp file:

include “employee.h”

//Constructor

Employee::Employee(int id, string name, int dID)

{

this->m_ID = id;

this->m_Name = name;

this->m_DeptID = dID;

}

//Display personal information

void Employee::showInfo()

{

Cout < < employee No.: < < this – > m_ ID

< < “\ t employee name:” < this – > m_ Name

< < “\ tposition:” < this – > getdeptname()

< < “\ t job responsibilities: complete the tasks assigned by the manager” < < endl;

}

//Get position name

string Employee::getDeptName()

{

Return string (“employee”);

}

Manager. H file:

pragma once

include

include “worker.h”

using namespace std;

//Manager class

class Manager : public Worker

{

public:

//Constructor

Manager(int id, string name, int dId);

//Display personal information

virtual void showInfo();

//Get position name

virtual string getDeptName();

};

Manager.cpp file:

include “Manager.h”

//Constructor

Manager::Manager(int id, string name, int dId)

{

this->m_ID = id;

this->m_Name = name;

this->m_DeptID = dId;

}

//Display personal information

void Manager::showInfo()

{

Cout < < employee No.: < < this – > m_ ID

< < “\ t employee name:” < this – > m_ Name

< < “\ tposition:” < this – > getdeptname()

< < “\ t job responsibilities: complete the tasks assigned by the boss and distribute the tasks to ordinary employees” < < endl;

}

//Get position name

string Manager::getDeptName()

{

Return string (“manager”);

}

Boss. H file:

pragma once

include

include “worker.h”

using namespace std;

//Manager class

class Boss : public Worker

{

public:

//Constructor

Boss(int id, string name, int dId);

//Display personal information

virtual void showInfo();

//Get position name

virtual string getDeptName();

};

Boss.cpp file:

include “Boss.h”

//Constructor

Boss::Boss(int id, string name, int dId)

{

this->m_ID = id;

this->m_Name = name;

this->m_DeptID = dId;

}

//Display personal information

void Boss::showInfo()

{

Cout < < employee No.: < < this – > m_ ID

< < “\ t employee name:” < this – > m_ Name

< < “\ tposition:” < this – > getdeptname()

< < “\ t job responsibilities: manage everything in the company” < < endl;

}

//Get position name

string Boss::getDeptName()

{

Return string (“boss”);

}

Recommended Today

Monkey patch monkey patch programming method and its application in Ruby

What ismonkey patch (Monkey Patch)? In a dynamic language, functions are added and changed without modifying the source code. Purpose of using monkey patch:1. Additional function2. Function change3. Fix program errors4. Add a hook to execute some other processing while executing a method, such as printing logs, realizing AOP, etc,5. Cache, when the amount of […]