某公司有两类职员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 11:43:04 浏览: 63
一个用C++编写的雇员类
4星 · 用户满意度95%
以下是C++语言的实现:
```
#include <iostream>
#include <string>
using namespace std;
class Employee {
protected:
string name; // 姓名
int age; // 年龄
int years; // 工作年限
int department; // 部门号
bool isRetired; // 是否退休
public:
Employee(string name, int age, int years, int department) {
this->name = name;
this->age = age;
this->years = years;
this->department = department;
this->isRetired = false;
}
virtual void insertInfo() {
cout << "请输入姓名:";
cin >> name;
cout << "请输入年龄:";
cin >> age;
cout << "请输入工作年限:";
cin >> years;
cout << "请输入部门号:";
cin >> department;
}
virtual void printOn() {
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "工作年限:" << years << endl;
cout << "部门号:" << department << endl;
cout << "在职情况:" << (isRetired ? "退休" : "在职") << endl;
}
virtual void retire() {
if (age >= 55) {
isRetired = true;
}
}
};
class Manager : public Employee {
private:
int level; // 级别
public:
Manager(string name, int age, int years, int department, int level) : Employee(name, age, years, department) {
this->level = level;
}
void insertInfo() {
Employee::insertInfo();
cout << "请输入级别:";
cin >> level;
}
void printOn() {
Employee::printOn();
cout << "级别:" << level << endl;
}
void retire() {
if (age >= 60) {
isRetired = true;
}
}
};
int main() {
const int MAX_EMPLOYEES = 100; // 最大员工数
Employee* employees[MAX_EMPLOYEES]; // 员工数组
int numEmployees = 0; // 员工数
// 输入Employee和Manager对象信息
cout << "请输入员工数:";
cin >> numEmployees;
for (int i = 0; i < numEmployees; i++) {
int type;
cout << "请输入员工类型(1:普通员工;2:经理):";
cin >> type;
string name;
int age, years, department, level;
cout << "请输入姓名:";
cin >> name;
cout << "请输入年龄:";
cin >> age;
cout << "请输入工作年限:";
cin >> years;
cout << "请输入部门号:";
cin >> department;
if (type == 2) {
cout << "请输入级别:";
cin >> level;
employees[i] = new Manager(name, age, years, department, level);
} else {
employees[i] = new Employee(name, age, years, department);
}
}
// 判断是否退休并输出信息
for (int i = 0; i < numEmployees; i++) {
employees[i]->retire();
}
// 按照年龄降序输出信息
for (int i = 0; i < numEmployees - 1; i++) {
for (int j = i + 1; j < numEmployees; j++) {
if (employees[i]->age < employees[j]->age) {
Employee* temp = employees[i];
employees[i] = employees[j];
employees[j] = temp;
}
}
}
cout << "按照年龄降序输出员工信息:" << endl;
for (int i = 0; i < numEmployees; i++) {
employees[i]->printOn();
cout << endl;
}
// 释放内存
for (int i = 0; i < numEmployees; i++) {
delete employees[i];
}
return 0;
}
```
实现了Employee类和Manager类,其中Manager类继承自Employee类,重写了retire()方法。在main函数中,先输入员工信息,然后判断是否退休并输出信息,最后按照年龄降序输出员工信息。
阅读全文