90.友元函数的作用

 1 #include <iostream>
 2 #include <cstdlib>
 3 using namespace std;
 4 
 5 //友元函数的主要作用就是访问私有变量
 6 class myclass
 7 {
 8 private:
 9     int x;
10     int y;
11 
12 public:
13     myclass(int a,int b)
14     {
15         x = a;
16         y = b;
17     }
18     //声明是朋友可以访问私有变量
19     friend void show(const myclass &my);
20 };
21 
22 void show(const myclass &my)
23 {
24     cout << my.x << my.y << endl; 
25 }
26 
27 void main()
28 {
29     myclass my1(3,4);
30 
31     show(my1);
32 
33     cin.get();
34 }

 

posted @ 2018-03-18 11:56  喵小喵~  阅读(464)  评论(0编辑  收藏  举报