当前位置: 首页 > 网络知识

C++快速入门 第二十八讲:多继承(multiple inheritance)

时间:2026-01-29 09:25:39
1 多继承语法: 2 3 //助教类既继承于老师类,又继承于学生类 4 class Assistant:public Teacher,public Student 5 ; 7 8 当遇到的问题无法只用一个“是一个”关系来描述的时候,就是多继承出场的时候。例即是学生,又是人,还是助教。 9 10 #include <iostream> 11 #include <string> 12 13 using namespace std; 14 15 #include <string> 16 //人类 17 class Person 18 ; 25 Person::Person(string theName)//构造函数实现 26 29 void Person::introduce()//introduce()函数实现 30 33 34 //老师类继承于人类 35 class Teacher:public Person 36 ; 45 Teacher::Teacher(string theName,string theClass):Person(theName)//老师的名字继承于人类中的名字 46 49 void Teacher::teach() 50 53 void Teacher::introduce() 54 57 58 //学生类继承于人类 59 class Student:public Person 60 ; 69 Student::Student(string theName,string theClass):Person(theName)//学生名字继承于人类中的名字 70 73 void Student::attendClass() 74 77 void Student::introduce() 78 81 82 //助教类既继承于老师类,又继承于学生类 83 class Assistant:public Teacher,public Student 84 ; 90 Assistant::Assistant(string theName,string classTeaching,string classAttending):Teacher(theName, classTeaching),Student(theName,classAttending) 91 96 void Assistant::introduce() 97 101 102 int main() 103


上一篇:C++快速入门 第三十六讲:副本构造器
下一篇:C++快速入门 第四十六讲:内联模板
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器
  • 英特尔第五代 Xeon CPU 来了:详细信息和行业反应
  • 由于云计算放缓引发扩张担忧,甲骨文股价暴跌
  • Web开发状况报告详细介绍可组合架构的优点
  • 如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳
  • 美光在数据中心需求增长后给出了强有力的预测
  • 2027服务器市场价值将接近1960亿美元
  • 生成式人工智能的下一步是什么?
  • 分享在外部存储上安装Ubuntu的5种方法技巧
  • 全球数据中心发展的关键考虑因素
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器

    英特尔第五代 Xeon CPU 来了:详细信息和行业反应

    由于云计算放缓引发扩张担忧,甲骨文股价暴跌

    Web开发状况报告详细介绍可组合架构的优点

    如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳

    美光在数据中心需求增长后给出了强有力的预测

    2027服务器市场价值将接近1960亿美元

    生成式人工智能的下一步是什么?

    分享在外部存储上安装Ubuntu的5种方法技巧

    全球数据中心发展的关键考虑因素