鱼C论坛

 找回密码
 立即注册
查看: 911|回复: 4

[已解决]小白求助 关于抽象类的一点疑惑 大佬请过目

[复制链接]
发表于 2020-4-11 19:18:40 | 显示全部楼层 |阅读模式

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

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

x
  1. #include "iostream"
  2. using std::endl;
  3. using std::cout;
  4. using std::cin;

  5. class Interface1
  6. {
  7. public:
  8.         virtual int add(int a, int b) = 0;
  9.         virtual void print() = 0;
  10. };

  11. class Interface2
  12. {
  13. public:
  14.         virtual int mult(int a, int b) = 0;
  15.         virtual void print() = 0;
  16. };

  17. class Parent
  18. {
  19. private:
  20.         int a;
  21. public:
  22.         int getA()
  23.         {
  24.                 a = 0;
  25.                 return a;
  26.         }
  27. };

  28. class Child :public Parent, public Interface1, public Interface2
  29. {
  30. public:
  31.         virtual int add(int a, int b)
  32.         {
  33.                 cout << "Child:add()\n";
  34.                 return a + b;
  35.         }
  36. public:                                   
  37.         virtual void print()  //问题:前面类 Interface1 和 Interface2中有两个print函数,可是虚函数继承后  Child只写了 一个 print函数 请问这是怎么回事?
  38.         {                          //问题:这个print函数是类Interface1的 还是Interface2的呀?
  39.                 cout << "Child:print()\n";
  40.         }
  41. public:
  42.         virtual int mult(int a, int b)
  43.         {
  44.                 cout << "Child:mult()\n";
  45.                 return a * b;
  46.         }
  47. };
  48. void main()
  49. {
  50.         Child c1;

  51.         Interface1* if1 = new Child;
  52.         if1->add(11,22);
  53.         //if1->print();
  54.        
  55.         Interface2* if2 = new Child;
  56.         if2->mult(11, 22);
  57.         //if2->print();
  58.         system("pause");
  59. }
复制代码
最佳答案
2020-4-11 19:24:38
是它自身的,子类只要有实现就都是自身的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-11 19:24:38 | 显示全部楼层    本楼为最佳答案   
是它自身的,子类只要有实现就都是自身的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-11 19:31:33 | 显示全部楼层
BngThea 发表于 2020-4-11 19:24
是它自身的,子类只要有实现就都是自身的

哦哦 知道了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-11 19:32:14 | 显示全部楼层
BngThea 发表于 2020-4-11 19:24
是它自身的,子类只要有实现就都是自身的

那为啥两个类中都有print函数 而子类中只有一个呢?因为virtual吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-11 19:57:32 | 显示全部楼层
只需要一个啊,难道写两个?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-2 04:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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