鱼C论坛

 找回密码
 立即注册
查看: 2448|回复: 2

友元关系

[复制链接]
发表于 2016-7-14 22:56:02 | 显示全部楼层 |阅读模式

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

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

x
代码编译不过!
  1. #include <iostream>
  2. #include <string>

  3. class Lovers
  4. {
  5. public:
  6.     Lovers(std::string theName);
  7.     void kiss(Lovers *lover);
  8.     void ask(Lovers *lover, std::string something);

  9. protected:
  10.     std::string name;

  11.     friend class Others;
  12. };

  13. class Boyfriend : public Lovers
  14. {
  15. public:
  16.     Boyfriend(std::string theName);
  17. };

  18. class Girlfriend : public Lovers
  19. {
  20. public:
  21.     Girlfriend(std::string theName);
  22. };

  23. class Others
  24. {
  25. public:
  26.     Others(std::string theName);
  27.     void kiss(Lovers *lover);

  28. protected:
  29.     std::string name;
  30. };

  31. Lovers::Lovers(std::string theName)
  32. {
  33.     name = theName;
  34. }

  35. void Lovers::kiss(Lovers *lover)
  36. {
  37.     std::cout << name << "亲亲我们家的" << lover->name << std::endl;
  38. }

  39. void Lovers::ask(Lovers *lover, std::string something)
  40. {
  41.     std::cout << "宝贝儿" << lover->name << "帮我" << something << std::endl;
  42. }

  43. Boyfriend::Boyfriend(std::string theName)
  44. {
  45. }

  46. Girlfriend::Girlfriend(std::string theName)
  47. {
  48. }

  49. Others::Others(std::string theName)
  50. {
  51.     name = theName;
  52. }

  53. void Others::kiss(Lovers *lover)
  54. {
  55.     std::cout << name << "亲一下" << lover->name << std::endl;
  56. }

  57. int main()
  58. {
  59.     Boyfriend boyfriend("A君");
  60.     Girlfriend girlfriend("B妞");

  61.     Others others("路人甲");

  62.     girlfriend.kiss(&boyfriend);
  63.     girlfriend.ask(&boyfriend, "洗衣服啦~");

  64.     std::cout << "\n当当当当! 传说中的路人甲登场......\n";
  65.     others.kiss(&girlfriend);

  66.     return 0;
  67. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-7-14 23:37:14 | 显示全部楼层
本帖最后由 Krant5 于 2016-7-14 23:38 编辑

你的两个构造器有问题,boyfriend 类构造器后面应该跟 :Lovers(theName),同理Grilfriend类构造器后面也一样,子类构造器也要初始化父类
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-7-15 20:58:32 | 显示全部楼层
Krant5 发表于 2016-7-14 23:37
你的两个构造器有问题,boyfriend 类构造器后面应该跟 :Lovers(theName),同理Grilfriend类构造器后面也一 ...

soo~~ ga~~万分感谢!
的确Boyfriend和Girlfriend的构造器都是继承自Lovers,忘了这个语法,哈哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-6 09:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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