公司雇员管理 运用C++语言描述公司类、雇员类、职务类,每一个类应包含数据成员和成员函数。设计基类和派生类
时间: 2024-02-07 14:02:32 浏览: 103
以下是一个可能的实现示例:
```c++
// 职务类
class Job {
public:
Job(const std::string& title, double salary) : title_(title), salary_(salary) {}
const std::string& GetTitle() const { return title_; }
double GetSalary() const { return salary_; }
protected:
std::string title_;
double salary_;
};
// 基类:员工类
class Employee {
public:
Employee(const std::string& name, int age, const std::string& id) : name_(name), age_(age), id_(id) {}
const std::string& GetName() const { return name_; }
int GetAge() const { return age_; }
const std::string& GetId() const { return id_; }
virtual const Job& GetJob() const = 0; // 纯虚函数,获取职务信息
protected:
std::string name_;
int age_;
std::string id_;
};
// 派生类:技术员类
class Technician : public Employee {
public:
Technician(const std::string& name, int age, const std::string& id, const Job& job)
: Employee(name, age, id), job_(job) {}
const Job& GetJob() const override { return job_; }
protected:
Job job_;
};
// 派生类:经理类
class Manager : public Employee {
public:
Manager(const std::string& name, int age, const std::string& id, const Job& job, const std::string& department)
: Employee(name, age, id), job_(job), department_(department) {}
const Job& GetJob() const override { return job_; }
const std::string& GetDepartment() const { return department_; }
protected:
Job job_;
std::string department_;
};
// 公司类
class Company {
public:
Company(const std::string& name, const std::string& address) : name_(name), address_(address) {}
const std::string& GetName() const { return name_; }
const std::string& GetAddress() const { return address_; }
// 添加员工
void AddEmployee(Employee* employee) { employees_.push_back(employee); }
// 获取员工数
int GetEmployeeCount() const { return employees_.size(); }
// 获取员工信息
const Employee* GetEmployee(int index) const { return employees_[index]; }
protected:
std::string name_;
std::string address_;
std::vector<Employee*> employees_;
};
```
在此实现中,职务类包含职位标题和薪资,员工类是一个抽象基类,包含姓名、年龄和ID等基本信息,以及一个纯虚函数获取职务信息,经理类和技术员类是员工类的派生类,分别添加了部门和职务信息。公司类包含名称、地址和员工信息等基本信息,以及添加员工、获取员工数和获取员工信息的函数。这个设计可能还有改进的空间,具体取决于应用场景和需求。
阅读全文
相关推荐
![doc](https://img-home.csdnimg.cn/images/20241231044833.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)