#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) { in >> p.name >> 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; } virtual void ShowMe() { Person::ShowMe(); cout << "Work ID: " << wID << endl; } friend istream& operator>>(istream &in, Staff &p) { in >> p.wID >> p.name >> p.sex; return in; } };

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

class Staff_Student: public Staff, public Student { 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) { }; virtual void ShowMe() { Person::ShowMe(); cout << "Work ID: " << wID << endl; cout << "Student ID: " << sID << endl; } friend istream& operator>>(istream &in, Staff_Student &p) { in >> p.wID >> p.sID >> p.name >> p.sex; 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(); cout << endl; } } };

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/mM18 著作权归作者所有。请勿转载和采集!

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