当前位置:网站首页>Source code and some understanding of employee management system based on polymorphism
Source code and some understanding of employee management system based on polymorphism
2022-04-23 02:44:00 【Wuhao9_】
Employee management system can be used to manage the information of all employees in the company


To write a management system, you should first create a management class :
Management responsible content :
- Interface of communication menu with users
- Add, delete, modify and query employees
- Read, write and interact with files
That is, all operations on the system are implemented in the management class !
Create separate WorkerManger.h File with the WorkerManger.cpp file .
The realization of menu function :
Realize the exchange interface :
WorkerManger.cpp in :
void WorkerManger::Show_Menu()
{
std::cout << "------------------------------" << std::endl;
std::cout << "|*** Welcome to the employee management system !***|" << std::endl;
std::cout << "|*******0. Exit the management system *******|" << std::endl;
std::cout << "|*******1. Add employee information *******|" << std::endl;
std::cout << "|*******2. Display employee information *******|" << std::endl;
std::cout << "|*******3. Delete the resigned employee *******|" << std::endl;
std::cout << "|*******4. Modify employee information *******|" << std::endl;
std::cout << "|*******5. Find employee information *******|" << std::endl;
std::cout << "|*******6. Sort by number *******|" << std::endl;
std::cout << "|*******7. Empty all documents *******|" << std::endl;
std::cout << "------------------------------" << std::endl;
std::cout << std::endl;
}
Staff management system .cpp( Project documents ) in :
#include<iostream> // Add input / output stream header file
#include"workerManger.h" // Add management class header file
#include"worker.h"
#include"employee.h"
#include"Manager.h"
#include"Boss.h"
int main()
{
WorkerManger wm; // Create management class objects
int choice;
while (true) // Use circulation and switch Statement control interactive interface
{
wm.Show_Menu();
std::cin >> choice;
switch (choice)
{
case 0:
wm.exit_System();
break;
case 1:
wm.add_EMp();
break;
case 2:
wm.show_Info();
break;
case 3:
wm.Delete_Emp();
break;
case 4:
wm.Mod_Emp();
break;
case 5:
wm.Find_Emp();
break;
case 6:
wm.Sort_List();
break;
case 7:
wm.Clear_File();
break;
default:
system("cls");
break;
}
}
system("pause");
return 0;
}
Realize the function of exiting the system :
void WorkerManger::exit_System()
{
std::cout << " Welcome to use next time " << std::endl;
system("pause");
exit(0);
}
Implementation of employee abstract class and derived class :
It is known that there are three types of employees in the company , They all have a common feature that employees .
We can encapsulate an abstract base class Worker To show employees , And employees should have attributes related to employees , Such as ID Number , full name , jobs ……
Create the base class , Then create a derived class Employee class 、Manager class 、Boss Class to represent ordinary employees 、 The manager 、 Boss , And inherit the properties of the base class .
So we can pass the pointer ( Or quote ) Create the corresponding object .
Create abstract classes :
Here's the thing to watch out for , If you declare a pure virtual function in an abstract class , Be sure to override pure virtual functions in derived classes !
#pragma once // Make sure that the header file is compiled only once
#include<iostream>
class Worker
{
public:
virtual void showInfo() = 0; // Display personal information
virtual std::string getDepName() = 0; // Get job information
virtual std::string getDuty() = 0; // Get job responsibilities
int m_Id;
std::string m_Name;
int m_DeptId;
};
Create a derived class :
.h file :
// Ordinary employees
#pragma once
#include"worker.h"
#include<iostream>
class Employee : public Worker
{
public:
Employee(int id, std::string name, int deptid);
virtual void Worker::showInfo();
virtual std::string Worker::getDepName();
virtual std::string Worker::getDuty();
};
.cpp file :
#include"employee.h"
Employee:: Employee(int id, std::string name, int deptid) // Derived class initialization
{
this->m_Id = id;
this->m_Name = name;
this->m_DeptId = deptid;
}
void Employee::showInfo() // Rewrite the pure virtual function
{
std::cout << " Employee number :" << this->m_Id;
std::cout << "\t Employee's name :" << this->m_Name;
std::cout << "\t Staff positions :" << this->getDepName();
std::cout << "\t Responsibilities :" << this->getDuty();
std::cout << std::endl;
}
std::string Employee ::getDepName() // Rewrite the pure virtual function
{
return std::string(" staff ");
}
std::string Employee::getDuty() // Rewrite the pure virtual function
{
return std::string(" Complete the tasks assigned by the manager !");
}
Omit here Manager Classes and Boss class ...
After encapsulating the class , We should manage ( Additions and deletions ) These workers ?
It can be realized by two-dimensional array pointer !
This system can realize the function of adding employees in batch , When users create in batch , Employees in different positions may be created
How to put all employees in different positions ( That is, different classes ) Put them in an array ?
Because you want to put different classes into the same array , And the array can only store the same data type
We can bring all employees ( That is, the created object ) The pointers of are stored in the same array !
meanwhile , Because you can add , Delete employee , Therefore, the array should be a dynamic array with indefinite length
To maintain this indefinite length array , We're going to open up this array to the heap , Otherwise, as the function ends , The destructor automatically frees the memory in the stack area , The array is then released ! In other words, the array can no longer be used to realize the corresponding functions .
Icon :

Worker* * m_Emparray = new Worker[x];// Open up an array of worker pointers in the heap and pass it to m_Emparray
// Because in the pile area new What comes out is a pointer , Therefore, use the pointer to receive
// m_Emparray The data type of is Worker Pointer to class
Worker* * m_Emparray ; // Pointer to employee pointer array
Determine the general idea , We can write the project !
The source code of the main functions of the system :
#include"workerManger.h"
#include"employee.h"
#include"Manager.h"
#include"Boss.h"
#include"worker.h"
#include<fstream>
#define FILENAME "empFile.txt"
// By constructor , Every time the system starts, it will track the file status and related contents in time
WorkerManger::WorkerManger()
{
std::ifstream ifs;
ifs.open(FILENAME, std::ios::in);
// situation 1: The file does not exist
if (!ifs.is_open())
{
//std::cout << " This file does not exist !" << std::endl;
this->m_Emparray = NULL;
this->m_EmpNum = 0;
this->m_FileIsEmpty = true;
ifs.close();
return;
}
// situation 2: The file exists but there is no record
char test;
ifs >> test;
if (ifs.eof())
{
//std::cout << " File exists , But no content " << std::endl;
this->m_Emparray = NULL;
this->m_EmpNum = 0;
this->m_FileIsEmpty = true;
ifs.close();
return;
}
// situation 3: The file exists and has data
//std::cout << " The number of people currently recorded is : " << this->get_EmpNum() << std::endl;
this->m_EmpNum = get_EmpNum();
this->m_Emparray = new Worker * [get_EmpNum()]; // Open up space to store the records before the file into the array
this->init_Emp();
/* for (int i = 0; i < get_EmpNum(); i++)
{
std::cout << "ID" << this->m_Emparray[i]->m_Id << std::endl;
std::cout << " full name " << this->m_Emparray[i]->m_Name << std::endl;
std::cout << " Position " << this->m_Emparray[i]->m_DeptId << std::endl;
} */
}
WorkerManger::~WorkerManger() // The system is about to run before the end of , Release heap data
{
if (m_Emparray != NULL)
{
delete[] m_Emparray;
m_Emparray = NULL;
}
}
void WorkerManger::Show_Menu()
{
std::cout << "------------------------------" << std::endl;
std::cout << "|*** Welcome to the employee management system !***|" << std::endl;
std::cout << "|*******0. Exit the management system *******|" << std::endl;
std::cout << "|*******1. Add employee information *******|" << std::endl;
std::cout << "|*******2. Display employee information *******|" << std::endl;
std::cout << "|*******3. Delete the resigned employee *******|" << std::endl;
std::cout << "|*******4. Modify employee information *******|" << std::endl;
std::cout << "|*******5. Find employee information *******|" << std::endl;
std::cout << "|*******6. Sort by number *******|" << std::endl;
std::cout << "|*******7. Empty all documents *******|" << std::endl;
std::cout << "------------------------------" << std::endl;
std::cout << std::endl;
}
void WorkerManger::exit_System()
{
std::cout << " Welcome to use next time " << std::endl;
system("pause");
exit(0);
}
void WorkerManger::add_EMp()
{
system("cls");
std::cout << " Please enter the number of employees you want to add " << std::endl;
int add_Num = 0;
std::cin >> add_Num;
if (add_Num > 0)
{
int newSize = this->m_EmpNum + add_Num;
Worker* * newArray = new Worker * [newSize];
if (m_EmpNum>0)
{
for (int i = 0; i < newSize; i++)
{
newArray[i] = this->m_Emparray[i];
}
}
for (int i = 0; i < add_Num; i++)
{
int ID;
std::string name;
int DepName;
std::cout << " Please enter the number... To add " << i + 1 << " It's an employee's ID" << std::endl;
std::cin >> ID;
std::cout << " Please enter the number... To add " << i + 1 << " The name of the employee " << std::endl;
std::cin >> name;
std::cout << " Please enter the number... To add " << i + 1 << " The position of an employee " << std::endl;
std::cout << "1. staff " << std::endl;
std::cout << "2. The manager " << std::endl;
std::cout << "3. Boss " << std::endl;
std::cin >> DepName;
Worker * worker = NULL;
switch (DepName)
{
case 1:
worker = new Employee(ID, name, DepName);
break;
case 2:
worker = new Manager(ID, name, DepName);
break;
case 3:
worker = new Boss(ID, name, DepName);
break;
default:
break;
}
system("cls");
newArray[m_EmpNum + i] = worker;
}
delete[] m_Emparray;
this->m_Emparray = newArray;
this->m_EmpNum = newSize;
this->m_FileIsEmpty = false;
std::cout << " Add success !" << std::endl;
this->save();
system("pause");
system("cls");
}
else
{
std::cout << " Incorrect input !" << std::endl;
}
}
void WorkerManger::save()
{
std::ofstream ofs;
ofs.open(FILENAME, std::ios::out);
for (int i = 0; i < m_EmpNum; i++)
{
ofs << m_Emparray[i]->m_Id << " "
<< m_Emparray[i]->m_Name << " ";
switch (m_Emparray[i]->m_DeptId)
{
case 1:
ofs << std::string(" staff ") << std::endl;
break;
case 2:
ofs << std::string(" The manager ") << std::endl;
break;
case 3:
ofs << std::string(" Boss ") << std::endl;
break;
default:
std::cout << " Input error !" << std::endl;
}
}
ofs.close();
}
int WorkerManger::get_EmpNum()
{
std::ifstream ifs;
ifs.open(FILENAME, std::ios::in);
int id;
std::string EDepName;
std::string name;
int num = 0;
while (ifs >> id && ifs >> name && ifs >> EDepName)
{
num++;
}
return num;
}
void WorkerManger::init_Emp()
{
int num = get_EmpNum();
std::ifstream ifs;
ifs.open(FILENAME, std::ios::in);
int id;
std::string name;
std::string EDepName;
for (int i = 0; i < num; i++)
{
Worker* worker = NULL;
ifs >> id && ifs >> name && ifs >> EDepName;
if (EDepName == " staff ")
{
worker = new Employee(id,name,1);
}
else if (EDepName == " The manager ")
{
worker = new Employee(id, name, 2);
}
else if (EDepName == " Boss ")
{
worker = new Employee(id, name, 3);
}
this->m_Emparray[i] = worker;
}
ifs.close();
return;
}
void WorkerManger::show_Info()
{
if (m_FileIsEmpty)
{
std::cout << " The file is empty or does not exist !" << std::endl;
}
else
{
for (int i = 0; i < this->m_EmpNum; i++)
{
this->m_Emparray[i]->showInfo();
}
}
system("pause");
system("cls");
}
int WorkerManger::EmpIsExist(int id)
{
int index = -1;
for (int i = 0; i < this->m_EmpNum; i++)
{
if (id == this->m_Emparray[i]->m_Id)
{
index = i;
return index;
break;
}
}
return index;
}
void WorkerManger::Delete_Emp()
{
if (this->m_FileIsEmpty)
{
std::cout << " File does not exist or record is empty !" << std::endl;
}
std::cout << " Please enter the employee to delete ID" << std::endl;
int EmpId,index;
std::cin >> EmpId;
index = this->EmpIsExist(EmpId);
if (index == -1)
{
std::cout << " Delete failed , Check no one !" << std::endl;
system("pause");
system("cls");
return;
}
else
{
for (int i = index; i < this->m_EmpNum-1; i++)
{
this->m_Emparray[i] = this->m_Emparray[i + 1];
}
this->m_EmpNum--;
this->save();
std::cout << " Delete successful !" << std::endl;
system("pause");
system("cls");
}
}
void WorkerManger::Mod_Emp()
{
if (!this->m_FileIsEmpty)
{
std::cout << " Please enter the employee number to be modified " << std::endl;
int Id;
std::cin >> Id;
int ret = this->EmpIsExist(Id);
if (ret != -1)
{
delete this->m_Emparray[ret];
int newid = 0;
std::string name = "";
int DId = 0;
std::cout << " find out " << Id << " Employee No , Please enter the new employee number :" << std::endl;
std::cin >> newid;
std::cout << " Please enter a new name :" << std::endl;
std::cin >> name;
std::cout << " Please enter a new position :" << std::endl;
std::cout << "1. staff " << std::endl;
std::cout << "2. The manager " << std::endl;
std::cout << "3. Boss " << std::endl;
std::cin >> DId;
Worker* worker = NULL;
switch (DId)
{
case 1:
worker = new Employee(newid,name,1);
break;
case 2:
worker = new Manager(newid, name, 2);
break;
case 3:
worker = new Boss(newid, name, 3);
break;
default:
break;
}
this->m_Emparray[ret] = worker;
std::cout << " Modification successful !" << std::endl;
this->save();
}
else
{
std::cout << " The number does not exist !" << std::endl;
}
}
else
{
std::cout << " The file is empty or does not exist !" << std::endl;
}
system("pause");
system("cls");
}
void WorkerManger::Find_Emp()
{
if (this->m_FileIsEmpty)
{
std::cout << " This file does not exist or the content is empty !" << std::endl;
}
else
{
std::cout << " Please select the search method :" << std::endl;
std::cout << "1. By employee ID lookup " << std::endl;
std::cout << "2. Search by employee name " << std::endl;
int choice;
std::cin >> choice;
if (choice == 1)
{
std::cout << " Please enter the employee you want to find ID:" << std::endl;
int id;
std::cin >> id;
int index = this->EmpIsExist(id);
if (index != -1)
{
this->m_Emparray[index]->showInfo();
}
else
{
std::cout << " Check no one !" << std::endl;
}
}
else if (choice == 2)
{
std::string name;
int index = -1;
std::cout << " Please enter the name of the employee you want to find :" << std::endl;
std::cin >> name;
for (int i = 0; i < this->m_EmpNum; i++)
{
if (name == this->m_Emparray[i]->m_Name)
{
index = i;
this->m_Emparray[index]->showInfo();
}
else
{
std::cout << " Check no one !" << std::endl;
}
}
}
else
{
std::cout << " Input error !" << std::endl;
}
}
system("pause");
system("cls");
}
void WorkerManger::Sort_List()
{
if (this->m_FileIsEmpty)
{
std::cout << " The file is empty or does not exist !" << std::endl;
}
else
{
std::cout << " Please select sort by :" << std::endl;
std::cout << "1. Ascending " << std::endl;
std::cout << "2. Descending " << std::endl;
int choices;
std::cin >> choices;
for (int i = 0; i < this->m_EmpNum; i++)
{
int Max_or_Min = i;
for (int j = i+1; j < m_EmpNum; j++)
{
if (choices == 1)
{
if (this->m_Emparray[Max_or_Min]->m_Id > this->m_Emparray[j]->m_Id)
{
Max_or_Min = j;
}
}
else if (choices == 2)
{
if (this->m_Emparray[Max_or_Min]->m_Id < this->m_Emparray[j]->m_Id)
{
Max_or_Min = j;
}
}
else
{
std::cout << " Input error !" << std::endl;
}
}
if (i != Max_or_Min)
{
Worker* temp = m_Emparray[i];
m_Emparray[i] = m_Emparray[Max_or_Min];
m_Emparray[Max_or_Min] = temp;
}
}
std::cout << " Sort success !" << std::endl;
this->save();
this->show_Info();
}
}
void WorkerManger::Clear_File()
{
std::cout << " Make sure to clear ?" << std::endl;
std::cout << "1. confirm " << std::endl;
std::cout << "2. return " << std::endl;
int select;
std::cin >> select;
if (select == 1)
{
std::ofstream ofs;
ofs.open(FILENAME, std::ios::trunc);
ofs.close();
if (this->m_Emparray != NULL)
{
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->m_Emparray[i] != NULL)
{
delete m_Emparray[i];
m_Emparray[i] = NULL;
}
}
delete[] this->m_Emparray;
this->m_Emparray = NULL;
this->m_EmpNum = 0;
this->m_FileIsEmpty = true;
}
std::cout << " Emptying complete !" << std::endl;
}
system("pause");
system("cls");
}
版权声明
本文为[Wuhao9_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230242135363.html
边栏推荐
- 基于Torchserve部署SBERT模型<语义相似度任务>
- IAR embedded development stm32f103c8t6 Lighting LED
- Rich intelligent auxiliary functions and exposure of Sihao X6 security configuration: it will be pre sold on April 23
- 想用Mac学习sql,主要给自己个充足理由买Mac听听意见
- Learn regular expression options, assertions
- JDBC JDBC
- The problem of removing spaces from strings
- If MySQL / SQL server judges that the table or temporary table exists, it will be deleted
- So library dependency
- 认识进程(多线程_初阶)
猜你喜欢

Jupyter for local and remote access to ECS

Day18 -- stack queue
![[xjtu Computer Network Security and Management] session 2 Cryptographic Technology](/img/b0/263e8dcbfeb2ce9f504a9c8eb76b07.png)
[xjtu Computer Network Security and Management] session 2 Cryptographic Technology

Global, exclusive, local Routing Guard

Halo open source project learning (I): project launch

ROP Emporium x86_64 7~8题

The usage and difference of * and & in C language and the meaning of keywords static and volatile

php+mysql對下拉框搜索的內容修改
![[wechat applet] set the bottom menu (tabbar) for the applet](/img/e2/98711dfb1350599cbdbdf13508b84f.png)
[wechat applet] set the bottom menu (tabbar) for the applet

LeetCode 1450 - 1453
随机推荐
Essential qualities of advanced programmers
使用Go语言构建Web服务器
【Hcip】OSPF常用的6种LSA详解
After idea is successfully connected to H2 database, there are no sub files
Handwritten memory pool and principle code analysis [C language]
Interpretation of the future development of smart agriculture
十六、异常检测
字符串去掉空格问题
Machine learning (Zhou Zhihua) Chapter 14 probability graph model
First day of rhcsa
Preliminary understanding of stack and queue
Suggestion: block reference sorting is in the order of keywords
Log4j知识点记录
Web learning record (medium)
Step principle of logical regression in machine learning
Push data from onenet cloud platform to database
JVM类加载器
[XJTU计算机网络安全与管理]第二讲 密码技术
The interface request takes too long. Jstack observes the lock holding
OCR识别PDF文件