以学校职工为基类,派生出教师类和管理人员类,又从教师类和管理人员类共同派生出教师管理人员类。C++代码
时间: 2024-05-22 07:11:58 浏览: 65
用c++编写的学生教师管理类
4星 · 用户满意度95%
#include <iostream>
using namespace std;
class SchoolStaff {
protected:
string name;
int age;
public:
SchoolStaff(string n, int a):name(n), age(a){}
virtual void printInfo() = 0; //纯虚函数
};
class Teacher : public SchoolStaff {
private:
string subject;
public:
Teacher(string n, int a, string s):SchoolStaff(n, a), subject(s){}
void printInfo() {
cout << "姓名:" << name << "\t年龄:" << age << "\t科目:" << subject << endl;
}
};
class Management : public SchoolStaff {
private:
string position;
public:
Management(string n, int a, string p):SchoolStaff(n, a), position(p){}
void printInfo() {
cout << "姓名:" << name << "\t年龄:" << age << "\t职位:" << position << endl;
}
};
class TeacherManagement : public Teacher, public Management {
public:
TeacherManagement(string n, int a, string s, string p):Teacher(n, a, s), Management(n, a, p){}
void printInfo() {
cout << "姓名:" << name << "\t年龄:" << age << "\t科目:" << subject << "\t职位:" << position << endl;
}
};
int main() {
SchoolStaff *p1 = new Teacher("Tom", 30, "数学");
SchoolStaff *p2 = new Management("Jerry", 40, "校长");
SchoolStaff *p3 = new TeacherManagement("Alice", 35, "英语", "教务主任");
p1->printInfo();
p2->printInfo();
p3->printInfo();
return 0;
}
阅读全文