1 #include <iostream> // 标准的输入输出
2 using namespace std;
3
4 class Building {
5 friend class goodFriend;
6
7 public:
8 Building(); // 类内申明,类外实现
9 public:
10 string m_sittingrookm;
11
12 private:
13 string m_bedroom;
14 };
15
16 // 类外实现
17 Building::Building()
18 {
19 this->m_sittingrookm = "卧室";
20 this->m_bedroom = "客厅";
21 }
22
23 class goodFriend {
24 public:
25 goodFriend();
26 void visit();
27 Building* b;
28 };
29
30 // 类外实现
31 goodFriend::goodFriend()
32 {
33 b = new Building;
34 }
35
36 void goodFriend::visit()
37 {
38 cout << "好基友正在参观" << this->b->m_sittingrookm << endl;
39 cout << "好基友正在参观" << this->b->m_bedroom << endl; // 将goodFriend申明为Building的友元类;
40 }
41
42 void test01()
43 {
44
45 goodFriend g;
46 g.visit();
47
48 }
49
50 int main()
51 {
52 test01();
53 return 0;
54 }
c++ 类做友元类
最新推荐文章于 2023-10-02 11:42:06 发布