// 补充必要的头文件原代码缺失 #include iostream #include string using namespace std; // 基类人员基本信息 class Person // 类名首字母大写符合C命名规范 { private: int age; // 年纪 string name; // 姓名 string identity; // 身份原shenfen英文更易读 public: // 优化点1string类型默认值改为空字符串而非NULL Person(int m_age 0, string m_name , string m_identity ) : age(m_age), name(m_name), identity(m_identity) { // 可选增加参数合法性检查增强健壮性 if (age 0) age 0; // 年龄不能为负数 } // 虚函数为多态提供基础 virtual void show() const { cout 信息如下: endl; cout 年龄: age endl; cout 姓名: name endl; cout 身份: identity endl; } }; // 派生类学生 class Student : public Person { private: string education; // 学历原xueli英文更易读 public: // 优化点1string默认值改为 Student(int age 0, string name , string identity , string m_education ) : Person(age, name, identity), education(m_education) {} void show() const override // 补充override关键字明确重写基类虚函数 { Person::show(); cout 学历: education endl; } }; // 派生类老师 class Teacher : public Person { private: string teachingSubject; // 教学科目原Subject命名更清晰 public: // 优化点1string默认值改为修正参数名拼写错误原m_subjcet Teacher(int age 0, string name , string identity , string m_subject ) : Person(age, name, identity), teachingSubject(m_subject) {} void show() const override { Person::show(); cout 教学科目: teachingSubject endl; } }; // 派生类职工 class Employee : public Person { private: string workUnit; // 工作单位原unit命名更清晰 public: // 优化点1string默认值改为 Employee(int age 0, string name , string identity , string m_unit ) : Person(age, name, identity), workUnit(m_unit) {} void show() const override { Person::show(); cout 工作单位: workUnit endl; } }; // 派生类在职读书的老师原Study_Work类名更直观 class TeacherWithStudy : public Person { private: string teachingSubject; // 教学科目命名和Teacher类保持一致 string education; // 学历命名和Student类保持一致 public: // 优化点1string默认值改为 TeacherWithStudy(int age 0, string name , string identity , string m_subject , string m_education ) : Person(age, name, identity), teachingSubject(m_subject), education(m_education) {} void show() const override { Person::show(); cout 教学科目: teachingSubject endl; cout 学历: education endl; } }; int main() { Person* p1 nullptr, * p2 nullptr, * p3 nullptr, * p4 nullptr; // 创建各派生类对象 Student st(22, 王五, 学生, 高中生); p1 st; p1-show(); cout endl; Teacher te(30, 张三, 老师, 数学); p2 te; p2-show(); cout endl; Employee em(45, 老王, 职工, 保安); p3 em; p3-show(); cout endl; TeacherWithStudy sw(26, 李四, 在职读书老师, 大学英语, 研究生); p4 sw; p4-show(); return 0; }