基于c++语言设计设计职工信息表,并由此产生一个信息简表,并使用虚函数实现多态性,完成显示不同简表信息的任务。使用了继承的方法,编写最多能输入10个职工的信息表,再根据这个表产生一个职工信息简表,并利用多
时间: 2024-05-04 07:18:18 浏览: 16
态性实现不同类型的职工信息简表。
下面是一个简单的示例代码:
```c++
#include <iostream>
#include <string>
using namespace std;
class Employee {
protected:
string name;
int age;
string gender;
double salary;
public:
Employee(string n, int a, string g, double s) {
name = n;
age = a;
gender = g;
salary = s;
}
virtual void display() {}
};
class Manager : public Employee {
private:
string department;
public:
Manager(string n, int a, string g, double s, string d) : Employee(n, a, g, s) {
department = d;
}
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Gender: " << gender << endl;
cout << "Salary: " << salary << endl;
cout << "Department: " << department << endl;
}
};
class Engineer : public Employee {
private:
string project;
public:
Engineer(string n, int a, string g, double s, string p) : Employee(n, a, g, s) {
project = p;
}
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Gender: " << gender << endl;
cout << "Salary: " << salary << endl;
cout << "Project: " << project << endl;
}
};
int main() {
Employee *employees[10];
// 加入几个经理和工程师
employees[0] = new Manager("John", 45, "Male", 80000, "Sales");
employees[1] = new Engineer("Mary", 30, "Female", 60000, "Software");
employees[2] = new Manager("Tom", 50, "Male", 90000, "Marketing");
employees[3] = new Engineer("Susan", 35, "Female", 65000, "Hardware");
// 显示职工信息
for (int i = 0; i < 4; i++) {
employees[i]->display();
cout << endl;
}
return 0;
}
```
在这个例子中,我们定义了一个 `Employee` 类作为基类,然后派生出了 `Manager` 和 `Engineer` 两个子类。每个子类都重写了 `display()` 函数,以显示不同的信息。在主函数中,我们创建了一个由 `Employee` 的指针组成的数组,并加入了几个经理和工程师的信息。然后,我们循环遍历这个数组,并调用每个指针指向的对象的 `display()` 函数,以显示不同类型的职工信息简表。