fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. // 教师类(基类1)
  6. class Teacher {
  7. protected:
  8. // 公共数据成员:姓名、年龄、性别、地址、电话
  9. string name;
  10. int age;
  11. string sex;
  12. string addr;
  13. string phone;
  14. string title; // 职称(教师独有)
  15. public:
  16. // 构造函数声明
  17. Teacher(string n, int a, string s, string ad, string p, string t);
  18. // 显示信息函数声明
  19. void display();
  20. };
  21.  
  22. // 干部类(基类2)
  23. class Cadre {
  24. protected:
  25. // 公共数据成员(与Teacher类同名)
  26. string name;
  27. int age;
  28. string sex;
  29. string addr;
  30. string phone;
  31. string post; // 职务(干部独有)
  32. public:
  33. // 构造函数声明
  34. Cadre(string n, int a, string s, string ad, string p, string po);
  35. };
  36.  
  37. // 教师干部类(多重继承派生类)
  38. class Teacher_Cadre : public Teacher, public Cadre {
  39. private:
  40. float wages; // 工资(派生类独有)
  41. public:
  42. // 构造函数声明
  43. Teacher_Cadre(string n, int a, string s, string ad, string p, string t, string po, float w);
  44. // 显示信息函数声明
  45. void show();
  46. };
  47.  
  48. // ==================== 类外实现成员函数 ====================
  49. // Teacher类构造函数
  50. Teacher::Teacher(string n, int a, string s, string ad, string p, string t) {
  51. name = n;
  52. age = a;
  53. sex = s;
  54. addr = ad;
  55. phone = p;
  56. title = t;
  57. }
  58.  
  59. // Teacher类显示函数
  60. void Teacher::display() {
  61. cout << "姓名:" << name << endl;
  62. cout << "年龄:" << age << endl;
  63. cout << "性别:" << sex << endl;
  64. cout << "职称:" << title << endl;
  65. cout << "地址:" << addr << endl;
  66. cout << "电话:" << phone << endl;
  67. }
  68.  
  69. // Cadre类构造函数
  70. Cadre::Cadre(string n, int a, string s, string ad, string p, string po) {
  71. name = n;
  72. age = a;
  73. sex = s;
  74. addr = ad;
  75. phone = p;
  76. post = po;
  77. }
  78.  
  79. // Teacher_Cadre类构造函数(解决同名成员,指定作用域初始化)
  80. Teacher_Cadre::Teacher_Cadre(string n, int a, string s, string ad, string p, string t, string po, float w)
  81. : Teacher(n, a, s, ad, p, t), Cadre(n, a, s, ad, p, po) {
  82. wages = w; // 初始化工资
  83. }
  84.  
  85. // Teacher_Cadre类显示函数(调用基类display,指定作用域访问同名成员)
  86. void Teacher_Cadre::show() {
  87. // 调用教师类的display函数,输出基础信息+职称
  88. Teacher::display();
  89. // 直接输出职务(来自Cadre类)和工资
  90. cout << "职务:" << Cadre::post << endl;
  91. cout << "工资:" << wages << "元" << endl;
  92. }
  93.  
  94. // ==================== 主函数测试 ====================
  95. int main() {
  96. // 创建教师干部对象
  97. Teacher_Cadre tc("张三", 40, "男", "北京市海淀区", "13800138000", "副教授", "系主任", 8500.50);
  98.  
  99. // 调用显示函数
  100. tc.show();
  101. return 0;
  102. }
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
姓名:张三
年龄:40
性别:男
职称:副教授
地址:北京市海淀区
电话:13800138000
职务:系主任
工资:8500.5元