鱼C论坛

 找回密码
 立即注册
查看: 1250|回复: 0

[技术交流] C++旅程第六站-----友元函数

[复制链接]
发表于 2020-5-12 21:38:15 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 这是她 于 2020-5-12 21:37 编辑

You have to have confidence in your ability and then be tough enough to follow through.           ----Rosalynn Carter

                                                                                                                               友元                 使用friend关键字
      友元的目的:让一个函数或者类访问另一个类中私有成员
     
   友元的三种实现形式:
     *全局函数做友元
     *类做友元
     *成员函数做友元


   全局函数做友元
  1. #include<iostream>
  2. #include<string>

  3. using namespace std;

  4. //全局函数做友元
  5. class Card
  6. {
  7.         //使用friend关键字----relatives全局函数是Card的友元,可以访问Card中的私有成员
  8.         friend void relatives(Card * card);
  9.        
  10.         private:
  11.                 int m_account;//类的私有变量
  12.                
  13.         public:
  14.                 string m_bank;//类的公有变量
  15.                
  16.             Card()
  17.             {
  18.                     this->m_bank = "Construction Bank";
  19.                     this->m_account = 6666;
  20.             }
  21. };

  22. //全局函数
  23. void  relatives(Card * card)
  24. {
  25.         cout << "relatives : bank -> " << card->m_bank << endl;//公有
  26.         cout << "relatives : account -> " << card->m_account << endl;//私有
  27. }

  28. int main()
  29. {
  30.         Card c1;
  31.         relatives(&c1);
  32.        
  33.         return 0;
  34. }
复制代码
         类做友元
  1. #include<iostream>
  2. #include<string>

  3. using namespace std;

  4. //类做友元
  5. class Card
  6. {
  7.         //使用friend关键字----GoodCard类是Card类的友元,可以访问Card类中的私有成员
  8.         friend class GoodCard;
  9.        
  10.         private:
  11.                 int m_account;//类的私有变量
  12.                
  13.         public:
  14.                 string m_bank;//类的公有变量
  15.                
  16.             Card()
  17.             {
  18.                     this->m_bank = "Construction Bank";
  19.                     this->m_account = 6666;
  20.             }
  21. };

  22. class GoodCard
  23. {
  24.         public:
  25.                 GoodCard();
  26.                 void visit();
  27.        
  28.         private:
  29.                 //创建一个指针
  30.                 Card * card;
  31. };

  32. GoodCard::GoodCard()
  33. {
  34.         //创建一个Card对象
  35.         card = new Card;
  36. }

  37. void GoodCard::visit()
  38. {
  39.         cout << "GoodCard : bank -> " << card->m_bank << endl;//公有
  40.         cout << "GoodCard : account -> " << card->m_account << endl;//私有
  41. }

  42. int main()
  43. {
  44.         GoodCard c1;
  45.         c1.visit();
  46.        
  47.         return 0;
  48. }
复制代码
        成员函数做友元
  1. #include<iostream>
  2. #include<string>

  3. using namespace std;

  4. //类做友元
  5. //必须将先定义的类的成员函数作为后定义类的友元函数,调换顺序会出现语法错误
  6. class Card;//类-----由于没有定义,需要先声明
  7. class GoodCard//定义的类的成员函数-------先于定义类定义
  8. {
  9.         public:
  10.                 GoodCard();
  11.                 void visit1();//让visit1函数可以访问Card中私有成员
  12.                 void visit2();//让visit2函数不可以访问Card中私有成员
  13.                
  14.         private:
  15.                 //创建一个指针
  16.                 Card * card;
  17. };


  18. class Card
  19. {
  20.         //使用friend关键字----GoodCard类中visit1函数是Card类的友元,可以访问Card类中的私有成员
  21.         friend void GoodCard::visit1();
  22.        
  23.         private:
  24.                 int m_account;//类的私有变量
  25.                
  26.         public:
  27.                 string m_bank;//类的公有变量
  28.                
  29.             Card()
  30.             {
  31.                     this->m_bank = "Construction Bank";
  32.                     this->m_account = 6666;
  33.             }
  34. };

  35. //这是定义类的成员函数的类,执行语句的时候会产生错误
  36. /*class GoodCard
  37. {
  38.         public:
  39.                 GoodCard();
  40.                 void visit1();//让visit1函数可以访问Card中私有成员
  41.                 void visit2();//让visit2函数不可以访问Card中私有成员
  42.                
  43.         private:
  44.                 //创建一个指针
  45.                 Card * card;
  46. };
  47. */

  48. GoodCard::GoodCard()
  49. {
  50.         //创建一个Card对象
  51.         card = new Card;
  52. }

  53. void GoodCard::visit1()
  54. {
  55.         cout << "-------------visit1函数---------------" << endl;
  56.         cout << "GoodCard : bank -> " << card->m_bank << endl;//公有---可以访问
  57.         cout << "GoodCard : account -> " << card->m_account << endl;//私有----可以访问
  58. }

  59. void GoodCard::visit2()
  60. {
  61.         cout << "------------visit2函数---------------" << endl;
  62.         cout << "GoodCard : bank -> " << card->m_bank << endl;//公有----可以访问
  63.         cout << "GoodCard : account -> " << card->m_account << endl;//私有----不可以访问
  64. }

  65. int main()
  66. {
  67.         GoodCard h1;
  68.         h1.visit1();
  69.         h1.visit2();
  70.        
  71.         return 0;
  72. }
复制代码

                      渣渣一个望大佬们指教







本帖被以下淘专辑推荐:

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-15 06:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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