写一个教职工管理系统的c++程序
时间: 2023-11-11 20:07:45 浏览: 81
C++职工管理系统:本教程主要利用C++来实现一个基于多态的职工管理系统
好的,以下是一个简单的教职工管理系统的C++程序,仅供参考:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Person {
public:
Person(string name, int age) : name_(name), age_(age) {}
virtual void Display() const {
cout << "Name: " << name_ << ", Age: " << age_ << endl;
}
virtual string GetType() const {
return "Person";
}
protected:
string name_;
int age_;
};
class Teacher : public Person {
public:
Teacher(string name, int age, string title) : Person(name, age), title_(title) {}
virtual void Display() const {
cout << "Name: " << name_ << ", Age: " << age_ << ", Title: " << title_ << endl;
}
virtual string GetType() const {
return "Teacher";
}
private:
string title_;
};
class Staff : public Person {
public:
Staff(string name, int age, string department) : Person(name, age), department_(department) {}
virtual void Display() const {
cout << "Name: " << name_ << ", Age: " << age_ << ", Department: " << department_ << endl;
}
virtual string GetType() const {
return "Staff";
}
private:
string department_;
};
class TeacherManager {
public:
void AddTeacher(Teacher teacher) {
teachers_.push_back(teacher);
}
void DisplayAllTeachers() {
cout << "All Teachers:" << endl;
for (int i = 0; i < teachers_.size(); i++) {
teachers_[i].Display();
}
}
private:
vector<Teacher> teachers_;
};
class StaffManager {
public:
void AddStaff(Staff staff) {
staffs_.push_back(staff);
}
void DisplayAllStaffs() {
cout << "All Staffs:" << endl;
for (int i = 0; i < staffs_.size(); i++) {
staffs_[i].Display();
}
}
private:
vector<Staff> staffs_;
};
int main() {
Teacher t1("Tom", 30, "Professor");
Teacher t2("Jerry", 35, "Associate Professor");
Staff s1("John", 25, "Administration");
Staff s2("Mary", 28, "Finance");
TeacherManager teacher_manager;
teacher_manager.AddTeacher(t1);
teacher_manager.AddTeacher(t2);
teacher_manager.DisplayAllTeachers();
StaffManager staff_manager;
staff_manager.AddStaff(s1);
staff_manager.AddStaff(s2);
staff_manager.DisplayAllStaffs();
return 0;
}
```
该程序定义了三个类:`Person`, `Teacher`, `Staff`,分别表示人、教师和职工。其中,`Teacher`和`Staff`都是`Person`的派生类,拥有更多的属性和方法。程序还定义了两个管理器类:`TeacherManager`和`StaffManager`,用于管理教师和职工。
在`main`函数中,先创建了几个教师和职工的实例,然后将它们添加到对应的管理器中,并输出所有教师和职工的信息。
阅读全文