模拟某单位的人事管理。人事信息主要包括:员工编号、所在部门、职务、薪资等级、薪资、联系方式等内容;部门信息主要包括:部门编号、部门名称等内容。完成以下操作:实现员工信息、部门信息的添加、修改、删除和查询。使用C++程序代码完成。
时间: 2024-06-21 09:02:40 浏览: 14
在C++中,实现一个简单的人事管理系统,我们可以使用类和数据结构来表示员工和部门信息。以下是一个基本的示例,展示了如何使用类来存储这些信息,并提供添加、修改、删除和查询功能:
```cpp
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
// 部门类
class Department {
public:
int id;
std::string name;
// 构造器和getter/setter
Department(int id, const std::string& name) : id(id), name(name) {}
int getId() { return id; }
std::string getName() { return name; }
};
// 员工类,继承自Department
class Employee : public Department {
public:
int empId;
std::string position;
int salaryGrade;
double salary;
std::string contact;
// 构造器和getter/setter
Employee(int id, const std::string& name, int empId,
const std::string& position, int grade, double salary,
const std::string& contact)
: Department(id, name), empId(empId), position(position),
salaryGrade(grade), salary(salary), contact(contact) {}
int getEmpId() { return empId; }
std::string getPosition() { return position; }
// ... 其他getter方法同上
};
// 人事管理类,包含数据容器和操作方法
class HRManager {
private:
std::vector<Employee> employees;
std::unordered_map<int, Department> departments;
public:
void addEmployee(Employee emp) {
employees.push_back(emp);
departments[emp.getId()] = emp.getDepartment(); // 假设每个员工都有部门ID关联
}
void updateEmployee(int empId, std::string newPosition, double newSalary) {
for (auto& e : employees) {
if (e.getEmpId() == empId) {
e.setPosition(newPosition);
e.salary = newSalary;
break;
}
}
}
void deleteEmployee(int empId) {
for (auto it = employees.begin(); it != employees.end(); ++it) {
if (it->getEmpId() == empId) {
employees.erase(it);
departments.erase(it->getId());
break;
}
}
}
Employee* findEmployee(int empId) {
auto it = employees.find(empId);
if (it != employees.end()) {
return &(*it);
}
return nullptr;
}
// ... 其他类似的方法,如添加部门、更新部门等
};
int main() {
HRManager manager;
// ... 使用manager对象执行相应的操作
return 0;
}
```
在这个例子中,我们使用`std::vector`来存储员工列表,`std::unordered_map`用于存储部门信息,以便快速查找。注意,这只是一个简化的示例,实际应用可能需要更复杂的数据结构(如数据库)来持久化数据,并且可能涉及到异常处理和更细致的错误检查。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)