友元类

 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 class newclass;// 友元类  friend可以放在任意位置
16 
17     ~myclass()
18     {
19     
20     }
21 
22 private:
23     int x;
24     int y;
25 
26 }
27 
28 class newclass
29 {
30 public:
31     myclass *p;
32     newclass()
33     {
34         p = new myclass(10,9);
35         cout << p->x << "  " << p->y << endl;// x,y私有不可以访问 声明friend class newclass;即可
36     }
37 };
38 
39 int main()
40 {    
41     
42     newclass new1;
43     
44     int(5);// 用构造函数构造临时对象
45 
46     int a1(int(5));// 创建一个变量 接受构造函数构造的临时变量
47 
48     int a2(5);// 创建一个变量 接受构造函数构造的临时变量
49 
50     
51     cin.get();
52     return 0;
53 }
54 
55 //----------------------------------------------------
56 
57 class A
58 {
59 public:
60     A()
61     {
62         cout << "A" << endl;
63     }
64 
65     ~A()
66     {
67         cout << "~A" << endl;
68     }
69 
70     A(const A & a)
71     {
72         cout << "copy a" << endl;
73     }
74 };
75 
76 void main()
77 {
78     // A();
79     
80     // A a;
81     
82     // A a1;
83     // A a(a1);
84 
85      A b = A(); // 调用构造
86      A b(A());// 不能这样写
87 
88      A *p = new A();// 指针的方法
89      A *p1(new A());
90     
91     cin.get();
92 }

 

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

导航