鱼C论坛

 找回密码
 立即注册
查看: 1713|回复: 3

[技术交流] 关于C++多继承 正在学习C++的同学来分享一下自己的建议、疑惑

[复制链接]
发表于 2015-1-21 16:20:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <iostream>
  2. #include <string>
  3. class Person
  4. {
  5. public:
  6.         Person(std::string theName);
  7.         void introduce();
  8. protected:
  9.         std::string name;
  10. };
  11. class Teacher :public Person
  12. {
  13. public:
  14.         Teacher(std::string theName, std::string theClass);
  15.         void teach();
  16.         void introduce();
  17. protected:
  18.         std::string classes;
  19. };
  20. class Student :public Person
  21. {
  22. public:
  23.         Student(std::string theName, std::string theClass);
  24.         void attendClass();
  25.         void introduce();
  26. protected:
  27.         std::string classes;
  28. };
  29. class TeachingStudent :public Student, public Teacher
  30. {
  31. public:
  32.         TeachingStudent(std::string theName, std::string classTeaching, std::string classAttending);
  33.         void introduce();
  34. };
  35. Person::Person(std::string theName)
  36. {
  37.         name = theName;
  38. }
  39. void Person::introduce()
  40. {
  41.         std::cout << "大家好,我是" << name << "。\n";
  42. }
  43. Teacher::Teacher(std::string theName, std::string theClass) :Person(theName)
  44. {
  45.         classes = theClass;
  46. }
  47. void Teacher::teach()
  48. {
  49.         std::cout << name << "教" << classes << "。\n";
  50. }
  51. void Teacher::introduce()
  52. {
  53.         std::cout << "大家好,我是" << name << ",我教" << classes << "。\n";
  54. }
  55. Student::Student(std::string theName, std::string theClass) :Person(theName)
  56. {
  57.         classes = theClass;
  58. }
  59. void Student::attendClass()
  60. {
  61.         std::cout << name << "加入" << classes << "学习。\n";
  62. }
  63. void Student::introduce()
  64. {
  65.         std::cout << "大家好,我是" << name << ",我在" << classes << "学习。\n";
  66. }
  67. TeachingStudent::TeachingStudent(std::string theName,
  68.         std::string classTeaching, std::string classAttending) :Teacher(theName, classTeaching), Student(theName, classAttending)
  69. {

  70. }
  71. void TeachingStudent::introduce()
  72. {
  73.         std::cout << "大家好,我是" << Student::name << "。我教" << Teacher::classes << ","<<"同时,我在"<<Student::classes<<"学习。\n";
  74. }
  75. int main()
  76. {
  77.         Teacher teacher("天天", "初级语言");
  78.         Student student("小白", "初级语言");
  79.         TeachingStudent teachingstudent("飞鱼", "初级语言", "高级语言");
  80.         teacher.introduce();
  81.         teacher.teach();
  82.         student.introduce();
  83.         student.attendClass();
  84.         teachingstudent.introduce();
  85.         teachingstudent.teach();
  86.         teachingstudent.attendClass();
  87.         return 0;
  88. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-1-21 17:04:25 | 显示全部楼层
MD,整得这么复杂,看了半天才有一点点理解了   蛋疼啊。。。。。。。。。。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-1-21 17:07:35 | 显示全部楼层
introduce是覆盖操作吗?子类覆盖基类?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-1-21 17:30:40 | 显示全部楼层
自己动手丰衣足食  
明白了  
明白的差不多了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-18 22:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表