用c++代码写以学校职工为基类,派生出教师类和管理人员类,又从教师类和管理人员类共同派生出教师管理人员类。
时间: 2024-05-24 22:01:06 浏览: 11
#include <iostream>
using namespace std;
class SchoolStaff {
protected:
string name;
int age;
string sex;
string id;
public:
SchoolStaff(string n, int a, string s, string i) {
name = n;
age = a;
sex = s;
id = i;
}
virtual void printInfo() {
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "性别:" << sex << endl;
cout << "工号:" << id << endl;
}
};
class Teacher : public SchoolStaff {
private:
string subject;
public:
Teacher(string n, int a, string s, string i, string sub) : SchoolStaff(n, a, s, i) {
subject = sub;
}
void printInfo() {
SchoolStaff::printInfo();
cout << "科目:" << subject << endl;
}
};
class Manager : public SchoolStaff {
private:
string department;
public:
Manager(string n, int a, string s, string i, string dep) : SchoolStaff(n, a, s, i) {
department = dep;
}
void printInfo() {
SchoolStaff::printInfo();
cout << "部门:" << department << endl;
}
};
class TeacherManager : public Teacher, public Manager {
public:
TeacherManager(string n, int a, string s, string i, string sub, string dep) : Teacher(n, a, s, i, sub), Manager(n, a, s, i, dep) {}
void printInfo() {
SchoolStaff::printInfo();
cout << "科目:" << subject << endl;
cout << "部门:" << department << endl;
}
};
int main() {
SchoolStaff s("张三", 30, "男", "10001");
s.printInfo();
cout << endl;
Teacher t("李四", 35, "女", "10002", "数学");
t.printInfo();
cout << endl;
Manager m("王五", 40, "男", "10003", "后勤");
m.printInfo();
cout << endl;
TeacherManager tm("赵六", 45, "女", "10004", "化学", "行政");
tm.printInfo();
cout << endl;
return 0;
}