友元函数

 1 /* 友元函数 */
 2 
 3 #include<iostream>
 4 
 5 using namespace std;
 6 
 7 class myclass
 8 {
 9 public:
10     myclass(int a,int b):x(a),y(b)
11     {
12 
13     }
14     
15     friend void show(const myclass & my1);// 友元函数
16 
17 private:
18     int x;
19     int y;
20 
21 };
22 
23 void show(const myclass & my1)
24 {
25     cout << my1.x << my1.y << endl;
26 }
27 
28 int main()
29 {
30     myclass my1(10,19);
31 
32     cout << my1.x;// 不可以 私有变量不可再外部访问
33     
34     show(my1);
35 
36     cin.get();
37     return 0;
38 }

 

posted on 2015-06-06 16:10  Dragon-wuxl  阅读(96)  评论(0)    收藏  举报

导航