You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++实现员工与部门多对多关联类及部门员工查询方法

员工与部门M:N关联系统的实现方案

需求说明

现有一套员工与部门信息管理系统,核心需求如下:

  • 员工具备姓名属性,部门具备名称属性
  • 薪资是员工与部门关联关系的属性(同一部门内不同员工薪资可不同,同一员工在不同部门薪资也可不同)
  • 需创建Linking类存储薪资并关联员工与部门,支持独立创建员工和部门,员工入职部门时录入薪资
  • 员工可隶属于多个部门(M:N关系)
  • 实现根据部门名称输出所有所属员工的查询方法

现有代码

Department.h

class Department{
private:
    std::string m_dname;

public:
    Department(std::string dname);

    std::string getName();
};

Department.cpp

#include "Department.h"

Department::Department(std::string dname) {
    m_dname=dname;
}

std::string Department::getName(){
    return m_dname;
}

Employee.h

class Employee{
private:
    std::string m_name;
    int m_salary;

public:
    Employee(std::string name);

    std::string getName();
    void addSalary();

    void addSalary(int salary);
};

Employee.cpp

#include "Employee.h"

Employee::Employee(std::string name) {
    m_name=name;
}

std::string Employee::getName() {
    return m_name;
}

void Employee::addSalary(int salary){
    m_salary=salary;
}

Linking.h

#include <vector>
#include "Department.h"
#include "Employee.h"

class Linking{
private:
    std::vector<Department*> m_department;
    std::vector<Employee*> m_employee;

public:
    Linking(Department* department, Employee* employee, int salary);

    Employee* getemployee(Department* department, std::string name);
};

Linking.cpp

#include "Linking.h"
#include "Separation.h"
#include "Employee.h"


Linking::Linking(Department* department, Employee* employee, int salary) {
    m_department.push_back(department);
    m_employee.push_back(employee);
    employee->addPay(salary);
}

Employee* Linking::getemployee(Department* department, std::string name){
    for(int i=0; i >=m_department.size(); i++){
          //next code
    }
}

问题分析与修正方案

现有代码存在3个核心问题:

  1. Employee类存储m_salary不合理——薪资是员工与部门的关联属性,同一员工在不同部门薪资可能不同,不应放在员工类中
  2. Linking类设计错误——两个独立的vector无法对应部门、员工、薪资的关联关系
  3. 查询方法不符合需求——需要返回指定部门的所有员工列表,而非单个员工

1. 修改Employee类

移除薪资相关成员,仅保留姓名属性:

Employee.h

class Employee{
private:
    std::string m_name;

public:
    Employee(std::string name);
    std::string getName();
};

Employee.cpp

#include "Employee.h"

Employee::Employee(std::string name) {
    m_name = name;
}

std::string Employee::getName() {
    return m_name;
}

2. 重构Linking类

定义内部结构体封装员工-部门-薪资的关联关系,实现关联添加和查询功能:

Linking.h

#include <vector>
#include <string>
#include "Department.h"
#include "Employee.h"

class Linking{
private:
    // 封装单组员工-部门-薪资关联
    struct EmpDeptLink {
        Department* dept;
        Employee* emp;
        int salary;
    };
    std::vector<EmpDeptLink> m_links;

public:
    // 添加员工与部门的关联并设置薪资
    void addLink(Department* department, Employee* employee, int salary);

    // 根据部门名称获取所有所属员工列表
    std::vector<Employee*> getEmployeesByDeptName(const std::string& deptName);
};

Linking.cpp

#include "Linking.h"

void Linking::addLink(Department* department, Employee* employee, int salary) {
    // 可选:避免重复添加同一员工与同一部门的关联
    for (const auto& link : m_links) {
        if (link.dept == department && link.emp == employee) {
            return;
        }
    }
    m_links.push_back({department, employee, salary});
}

std::vector<Employee*> Linking::getEmployeesByDeptName(const std::string& deptName) {
    std::vector<Employee*> result;
    for (const auto& link : m_links) {
        if (link.dept->getName() == deptName) {
            // 可选:避免同一员工重复出现在结果中
            bool exists = false;
            for (const auto& emp : result) {
                if (emp == link.emp) {
                    exists = true;
                    break;
                }
            }
            if (!exists) {
                result.push_back(link.emp);
            }
        }
    }
    return result;
}

3. 主函数测试示例

#include <iostream>
#include "Department.h"
#include "Employee.h"
#include "Linking.h"

int main() {
    // 创建部门
    Department* techDept = new Department("技术部");
    Department* marketDept = new Department("市场部");

    // 创建员工
    Employee* zhangSan = new Employee("张三");
    Employee* liSi = new Employee("李四");

    // 关联管理器
    Linking linkManager;

    // 建立关联并设置薪资
    linkManager.addLink(techDept, zhangSan, 15000);
    linkManager.addLink(techDept, liSi, 12000);
    linkManager.addLink(marketDept, zhangSan, 13000); // 张三同时属于两个部门

    // 查询技术部员工
    std::cout << "技术部员工列表:" << std::endl;
    std::vector<Employee*> techEmps = linkManager.getEmployeesByDeptName("技术部");
    for (const auto& emp : techEmps) {
        std::cout << "- " << emp->getName() << std::endl;
    }

    // 查询市场部员工
    std::cout << "\n市场部员工列表:" << std::endl;
    std::vector<Employee*> marketEmps = linkManager.getEmployeesByDeptName("市场部");
    for (const auto& emp : marketEmps) {
        std::cout << "- " << emp->getName() << std::endl;
    }

    // 释放资源(实际项目建议使用智能指针)
    delete techDept;
    delete marketDept;
    delete zhangSan;
    delete liSi;

    return 0;
}

内容的提问来源于stack exchange,提问作者Davidcz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 11:27:40