C++面向对象编程:设计员工管理系统(含代码示例)
C++面向对象编程:设计员工管理系统
本篇文章将使用C++语言设计一个简单的员工管理系统,该系统包含四种类型的员工:总经理、技术人员、销售经理和推销员。我们将使用面向对象编程中的继承和多态来实现不同类型员工的特性,并展示如何录入和显示员工信息。
需求分析
某小型公司有四类人员:总经理、技术人员、销售经理、推销员。需要设计一个基类employee,派生出manager(总经理)、technician(技术人员)、salesmanager(销售经理)、saleman(推销员)。销售经理既是经理又是销售人员,兼具两类人员的特点,因此同时继承manager 和salesman 两个类。
设计思路
- 
创建基类
Employee- 包含员工的通用信息:编号、姓名、性别、出生日期、职位、薪水等。
 - 使用私有属性存储员工信息,使用公有函数提供访问和修改接口。
 - 提供多个构造函数,分别用于无参数、带参数和带默认参数的初始化。
 
 - 
创建日期类
Date- 包含年、月、日信息。
 - 提供设置和获取年、月、日的方法。
 
 - 
创建派生类
Technician:新增属性工作时间。Salesman:新增属性销售额和所属部门。
 - 
实现销售经理类
SalesManager- 同时继承 
Manager和Salesman类,拥有两者的属性和方法。 
 - 同时继承 
 - 
实现录入和显示员工信息
- 使用代码示例演示如何创建不同类型的员工对象,并录入和显示其信息。
 
 
代码实现
#include <iostream>
#include <string>
using namespace std;
class Date {
private:
    int year;
    int month;
    int day;
public:
    Date() {
        year = 0;
        month = 0;
        day = 0;
    }
    Date(int y, int m, int d) {
        year = y;
        month = m;
        day = d;
    }
    void SetYear(int y) {
        year = y;
    }
    void SetMonth(int m) {
        month = m;
    }
    void SetDay(int d) {
        day = d;
    }
    int GetYear() {
        return year;
    }
    int GetMonth() {
        return month;
    }
    int GetDay() {
        return day;
    }
};
class Employee {
private:
    string id;
    string name;
    string gender;
    Date birth;
    string position;
    double salary;
public:
    Employee() {
        id = "";
        name = "";
        gender = "";
        salary = 0.0;
    }
    Employee(string i, string n, string g, Date b, string p, double s) {
        id = i;
        name = n;
        gender = g;
        birth = b;
        position = p;
        salary = s;
    }
    Employee(string i, string n, string g, string p, double s) {
        id = i;
        name = n;
        gender = g;
        position = p;
        salary = s;
    }
    void SetId(string i) {
        id = i;
    }
    void SetName(string n) {
        name = n;
    }
    void SetGender(string g) {
        gender = g;
    }
    void SetBirth(Date b) {
        birth = b;
    }
    void SetPosition(string p) {
        position = p;
    }
    void SetSalary(double s) {
        salary = s;
    }
    string GetId() {
        return id;
    }
    string GetName() {
        return name;
    }
    string GetGender() {
        return gender;
    }
    Date GetBirth() {
        return birth;
    }
    string GetPosition() {
        return position;
    }
    double GetSalary() {
        return salary;
    }
    void ShowInfo() {
        cout << "编号:" << id << endl;
        cout << "姓名:" << name << endl;
        cout << "性别:" << gender << endl;
        cout << "出生日期:" << birth.GetYear() << "年" << birth.GetMonth() << "月" << birth.GetDay() << "日" << endl;
        cout << "职位:" << position << endl;
        cout << "薪水:" << salary << endl;
    }
};
class Manager : public Employee {
public:
    Manager() {
        SetPosition("总经理");
    }
    Manager(string i, string n, string g, Date b, double s) {
        SetId(i);
        SetName(n);
        SetGender(g);
        SetBirth(b);
        SetPosition("总经理");
        SetSalary(s);
    }
    Manager(string i, string n, string g, double s) {
        SetId(i);
        SetName(n);
        SetGender(g);
        SetPosition("总经理");
        SetSalary(s);
    }
};
class Technician : public Employee {
private:
    int workTime;
public:
    Technician() {
        SetPosition("技术人员");
        workTime = 0;
    }
    Technician(string i, string n, string g, Date b, double s, int w) {
        SetId(i);
        SetName(n);
        SetGender(g);
        SetBirth(b);
        SetPosition("技术人员");
        SetSalary(s);
        workTime = w;
    }
    Technician(string i, string n, string g, double s, int w) {
        SetId(i);
        SetName(n);
        SetGender(g);
        SetPosition("技术人员");
        SetSalary(s);
        workTime = w;
    }
    void SetWorkTime(int w) {
        workTime = w;
    }
    int GetWorkTime() {
        return workTime;
    }
};
class Salesman : public Employee {
private:
    double sales;
    string department;
public:
    Salesman() {
        SetPosition("推销员");
        sales = 0.0;
        department = "";
    }
    Salesman(string i, string n, string g, Date b, double s, double se, string d) {
        SetId(i);
        SetName(n);
        SetGender(g);
        SetBirth(b);
        SetPosition("推销员");
        SetSalary(s);
        sales = se;
        department = d;
    }
    Salesman(string i, string n, string g, double s, double se, string d) {
        SetId(i);
        SetName(n);
        SetGender(g);
        SetPosition("推销员");
        SetSalary(s);
        sales = se;
        department = d;
    }
    void SetSales(double se) {
        sales = se;
    }
    double GetSales() {
        return sales;
    }
    void SetDepartment(string d) {
        department = d;
    }
    string GetDepartment() {
        return department;
    }
};
class SalesManager : public Manager, public Salesman {
public:
    SalesManager() {
        SetPosition("销售经理");
    }
    SalesManager(string i, string n, string g, Date b, double s, double se, string d) {
        SetId(i);
        SetName(n);
        SetGender(g);
        SetBirth(b);
        SetPosition("销售经理");
        SetSalary(s);
        SetSales(se);
        SetDepartment(d);
    }
    SalesManager(string i, string n, string g, double s, double se, string d) {
        SetId(i);
        SetName(n);
        SetGender(g);
        SetPosition("销售经理");
        SetSalary(s);
        SetSales(se);
        SetDepartment(d);
    }
};
int main() {
    // 录入人员信息
    Manager m("001", "张三", "男", Date(1980, 1, 1), 10000);
    Technician t("002", "李四", "男", Date(1985, 2, 2), 8000, 8);
    Salesman s("003", "王五", "女", Date(1990, 3, 3), 5000, 20000, "销售部");
    SalesManager sm("004", "赵六", "男", Date(1975, 4, 4), 12000, 30000, "销售部");
    // 显示人员信息
    m.ShowInfo();
    cout << endl;
    t.ShowInfo();
    cout << endl;
    s.ShowInfo();
    cout << endl;
    sm.ShowInfo();
    return 0;
}
代码说明
- Date 类:用于存储日期信息,提供设置和获取年、月、日的方法。
 - Employee 类:基类,包含员工的通用信息。
 - Manager 类:总经理类,继承自 
Employee类,设置职位为“总经理”。 - Technician 类:技术人员类,继承自 
Employee类,新增属性workTime(工作时间)。 - Salesman 类:推销员类,继承自 
Employee类,新增属性sales(销售额)和department(所属部门)。 - SalesManager 类:销售经理类,同时继承 
Manager和Salesman类,兼具两者的属性和方法,设置职位为“销售经理”。 - main 函数:演示如何创建不同类型的员工对象,并录入和显示其信息。
 
总结
本篇文章演示了如何使用 C++ 面向对象编程来设计一个简单的员工管理系统,并介绍了继承和多态的概念。通过代码示例,读者可以学习如何创建类、继承类、实现多态以及如何使用对象来存储和管理数据。
扩展
- 可以添加更多员工类型,例如财务人员、人事专员等。
 - 可以实现更多功能,例如添加员工、删除员工、修改员工信息等。
 - 可以使用数据库来存储员工信息,提高系统效率和数据安全性。
 
原文地址: https://www.cveoy.top/t/topic/oajh 著作权归作者所有。请勿转载和采集!