某公司有两类职员Employee和Manager,Manager是一种特殊的Employee。每个Employee对象所具有的基本信息为:姓名、年龄、工作年限、部门号;Manager对象除具有上述基本信息外,还具有级别(level)信息。公司中的Employee和Manager都具有以下两种基本操作,请根据上述描述,用C++语言定义并实现类Employee和Manager。 insertInfo()//实现对象信息的输入 printOn() // 输出对象的基本信息(姓名、年龄、工作年限、部门号、在职情况) retire() // 判断是否到了退休年龄,是,表明在职情况为退休。公司规定:Employee类对象的退休年龄为55岁,Manager类对象的退休年龄为60岁。 编程要求: (1) 定义并实现类Employee和Manager (2) 按照年龄降序模式分别输出公司中两类人员的人员情况(注意:Manager对象也是Employee对象)。
时间: 2024-03-25 15:39:38 浏览: 188
以下是C++代码实现:
```cpp
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Employee {
protected:
string name;
int age;
int yearsOfWork;
int departmentNum;
bool isRetired;
public:
Employee(string n = "", int a = 0, int y = 0, int d = 0) : name(n), age(a), yearsOfWork(y), departmentNum(d), isRetired(false) {}
virtual void insertInfo() {
cout << "请输入姓名:";
getline(cin, name);
cout << "请输入年龄:";
cin >> age;
cout << "请输入工作年限:";
cin >> yearsOfWork;
cout << "请输入部门号:";
cin >> departmentNum;
cin.ignore();
}
virtual void printOn() const {
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "工作年限:" << yearsOfWork << endl;
cout << "部门号:" << departmentNum << endl;
cout << "在职情况:" << (isRetired ? "退休" : "在职") << endl;
}
virtual void retire() {
if (age >= 55) {
isRetired = true;
}
}
bool operator<(const Employee& other) const {
return age > other.age;
}
};
class Manager : public Employee {
private:
int level;
public:
Manager(string n = "", int a = 0, int y = 0, int d = 0, int l = 0) : Employee(n, a, y, d), level(l) {}
void insertInfo() override {
Employee::insertInfo();
cout << "请输入级别:";
cin >> level;
cin.ignore();
}
void printOn() const override {
Employee::printOn();
cout << "级别:" << level << endl;
}
void retire() override {
if (age >= 60) {
isRetired = true;
}
}
};
int main() {
vector<Employee*> employees;
int n;
cout << "请输入公司中员工人数:";
cin >> n;
cin.ignore();
for (int i = 0; i < n; i++) {
char type;
cout << "请输入第" << i + 1 << "个员工的类型(E为普通员工,M为经理):";
cin >> type;
cin.ignore();
Employee* emp;
if (type == 'E') {
emp = new Employee;
} else if (type == 'M') {
emp = new Manager;
} else {
cout << "输入有误!" << endl;
i--;
continue;
}
emp->insertInfo();
emp->retire();
employees.push_back(emp);
}
sort(employees.begin(), employees.end(), [](const Employee* e1, const Employee* e2) {
return *e1 < *e2;
});
cout << "公司中普通员工情况:" << endl;
for (Employee* emp : employees) {
if (dynamic_cast<Manager*>(emp) == nullptr) {
emp->printOn();
cout << endl;
}
}
cout << "公司中经理情况:" << endl;
for (Employee* emp : employees) {
if (dynamic_cast<Manager*>(emp) != nullptr) {
emp->printOn();
cout << endl;
}
}
for (Employee* emp : employees) {
delete emp;
}
return 0;
}
```
在这个程序中,我们定义了一个抽象的Employee类,和一个继承自Employee类的Manager类,用于表示公司中的两类职员。
Employee类中包含了姓名、年龄、工作年限、部门号、在职情况等基本信息以及insertInfo、printOn和retire等基本操作。
Manager类继承了Employee类,并添加了级别信息。同时,Manager类也重写了retire操作,以便符合规定的退休年龄。
在主函数中,我们首先通过输入员工人数,动态创建Employee或Manager对象,并将其添加到一个vector中。然后,我们对vector中的Employee指针进行排序,按照年龄降序模式输出公司中的普通员工和经理的情况。
最后,我们需要记得释放动态分配的内存。
阅读全文