#include #include using namespace std;

class Person { public: Person(string name1 = "", char s = 'M') { name = name1; sex = s; } virtual void ShowMe() { cout << "Name: " << name << endl; cout << "Sex: " << sex << endl; } friend istream& operator>>(istream &in, Person &p) { cout << "Enter name: "; in >> p.name; cout << "Enter sex (M/F): "; in >> p.sex; return in; } protected: char sex; string name; };

class Staff: public Person { protected: int wID; //工作号 public: Staff(int id = 0, string name1 = "", char s = 'M'):Person(name1,s) { wID = id; } void ShowMe() { Person::ShowMe(); cout << "Work ID: " << wID << endl; } friend istream& operator>>(istream &in, Staff &p) { Person &p1 = p; in >> p1; cout << "Enter work ID: "; in >> p.wID; return in; } };

class Student: public Person { protected: int sID; //学号 public: Student(int id = 0, string name1 = "", char s = 'M'):Person(name1,s) { sID = id; } void ShowMe() { Person::ShowMe(); cout << "Student ID: " << sID << endl; } friend istream& operator>>(istream &in, Student &p) { Person &p1 = p; in >> p1; cout << "Enter student ID: "; in >> p.sID; return in; } };

class Staff_Student: public Student, public Staff { public: Staff_Student(string name1 = "", char s = 'M', int id1 = 0, int id2 = 0):Person(name1,s),Student(id1,name1,s),Staff(id2,name1,s) { }; void ShowMe() { Person::ShowMe(); cout << "Student ID: " << sID << endl; cout << "Work ID: " << wID << endl; } friend istream& operator>>(istream &in, Staff_Student &p) { Person &p1 = p; in >> p1; cout << "Enter student ID: "; in >> p.sID; cout << "Enter work ID: "; in >> p.wID; return in; } };

class School { private: Person *p[100]; int size; public: School() { size = 0; } ~School() { for (int i = 0; i < size; i++) { delete p[i]; } } void append(Person &p1) { p[size++] = &p1; } void display() { for (int i = 0; i < size; i++) { p[i]->ShowMe(); } } };

int main() { School sch; Staff s1; cin >> s1; sch.append(s1); Student st1; cin >> st1; sch.append(st1); Staff_Student ss1("SS1",'F',1001,1003); cin >> ss1; sch.append(ss1); sch.display(); return 0; }

C++ 多态性与继承:学校人员管理系统

原文地址: https://www.cveoy.top/t/topic/mNA8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录