某公司有两类职员Employee和Manager,Manager是一种特殊的Employee。每个Employee对象所具有的基本信息为:姓名、年龄、工作年限、部门号;Manager对象除具有上述基本信息外,还具有级别(level)信息。公司中的Employee和Manager都具有以下两种基本操作,请根据上述描述,用C++语言定义并实现类Employee和Manager。 a.insert Info()//实现对象信息的输入 b.printOn() // 输出对象的基本信息(姓名、年龄、工作年限、部门号、在职情况) c. retire() // 判断是否到了退休年龄,是,表明在职情况为退休。公司规定: // Employee类对象的退休年龄为55岁,Manager类对象的退休年龄为60岁 编程要求: 1、定义并实现类Employee和Manager 2、按照年龄降序模式分别输出公司中两类人员的人员情况(注意:Manager对象也是Employee对象)。
时间: 2024-03-12 13:45:34 浏览: 76
以下是一个可能的解决方案:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Employee {
protected:
string name;
int age;
int workYears;
int deptNo;
bool retired;
public:
Employee() {
name = "";
age = 0;
workYears = 0;
deptNo = 0;
retired = false;
}
virtual void insertInfo() {
cout << "请输入姓名:";
cin >> name;
cout << "请输入年龄:";
cin >> age;
cout << "请输入工作年限:";
cin >> workYears;
cout << "请输入部门号:";
cin >> deptNo;
}
virtual void printOn() {
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "工作年限:" << workYears << endl;
cout << "部门号:" << deptNo << endl;
cout << "在职情况:" << (retired ? "退休" : "在职") << endl;
}
virtual void retire() {
if (age >= 55) {
retired = true;
}
}
};
class Manager : public Employee {
private:
int level;
public:
Manager() : Employee() {
level = 0;
}
void insertInfo() override {
Employee::insertInfo();
cout << "请输入级别:";
cin >> level;
}
void printOn() override {
Employee::printOn();
cout << "级别:" << level << endl;
}
void retire() override {
if (age >= 60) {
retired = true;
}
}
};
int main() {
const int MAX_EMPLOYEES = 100;
Employee* employees[MAX_EMPLOYEES];
int numEmployees = 0;
// 输入Employee和Manager对象信息
cout << "请输入Employee和Manager对象信息:" << endl;
char ch;
do {
cout << "请输入员工类型(E代表Employee,M代表Manager):";
cin >> ch;
if (ch == 'E' || ch == 'e') {
employees[numEmployees] = new Employee();
} else if (ch == 'M' || ch == 'm') {
employees[numEmployees] = new Manager();
} else {
cout << "无效输入,请重新输入!" << endl;
continue;
}
employees[numEmployees]->insertInfo();
numEmployees++;
cout << "是否继续输入?(Y代表是,N代表否):";
cin >> ch;
} while (ch == 'Y' || ch == 'y');
// 输出Employee对象信息
cout << "按照年龄降序模式输出Employee对象信息:" << endl;
for (int i = 0; i < numEmployees; i++) {
if (dynamic_cast<Employee*>(employees[i]) != nullptr) {
employees[i]->retire();
}
}
for (int i = 0; i < numEmployees; i++) {
for (int j = i + 1; j < numEmployees; j++) {
if (dynamic_cast<Employee*>(employees[i]) != nullptr &&
dynamic_cast<Employee*>(employees[j]) != nullptr &&
employees[i]->age < employees[j]->age) {
Employee* temp = employees[i];
employees[i] = employees[j];
employees[j] = temp;
}
}
}
for (int i = 0; i < numEmployees; i++) {
if (dynamic_cast<Employee*>(employees[i]) != nullptr) {
employees[i]->printOn();
cout << endl;
}
}
// 输出Manager对象信息
cout << "按照年龄降序模式输出Manager对象信息:" << endl;
for (int i = 0; i < numEmployees; i++) {
if (dynamic_cast<Manager*>(employees[i]) != nullptr) {
employees[i]->retire();
}
}
for (int i = 0; i < numEmployees; i++) {
for (int j = i + 1; j < numEmployees; j++) {
if (dynamic_cast<Manager*>(employees[i]) != nullptr &&
dynamic_cast<Manager*>(employees[j]) != nullptr &&
employees[i]->age < employees[j]->age) {
Employee* temp = employees[i];
employees[i] = employees[j];
employees[j] = temp;
}
}
}
for (int i = 0; i < numEmployees; i++) {
if (dynamic_cast<Manager*>(employees[i]) != nullptr) {
employees[i]->printOn();
cout << endl;
}
}
// 释放内存
for (int i = 0; i < numEmployees; i++) {
delete employees[i];
}
return 0;
}
```
在这个例子中,我们定义了两个类:Employee和Manager,其中Manager是Employee的子类。Employee类和Manager类都实现了insertInfo()、printOn()和retire()三个方法。其中,insertInfo()用于输入对象信息,printOn()用于输出对象信息,retire()用于判断对象是否到了退休年龄。需要注意的是,由于Manager类是Employee类的子类,因此Manager类中可以直接调用Employee类中的方法,不需要重新实现。
在主函数中,我们首先输入Employee和Manager对象信息,并按照年龄降序模式分别输出公司中两类人员的人员情况。在输出之前,我们需要先调用retire()方法判断对象是否到了退休年龄,然后按照年龄降序对对象进行排序。在排序时,我们使用了dynamic_cast运算符来区分Employee对象和Manager对象。
最后,我们需要记得释放动态分配的内存。
阅读全文