-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path30_类与对象_友元_03.cpp
67 lines (53 loc) · 1.15 KB
/
30_类与对象_友元_03.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
using namespace std;
// 成员函数做友元
class Building;
class GoodFriend
{
public:
GoodFriend();
void visit(); // 让visit函数可以访问Building中私有成员
void visit2(); // 让visit2函数不可以访问Building中私有成员
Building *building;
};
class Building
{
// GoodFriend类下的visit成员函数为本类的friend,可以访问私有成员
friend void GoodFriend::visit();
public:
Building();
string m_SittingRoom;
private:
string m_BedRoom;
};
// 类外实现成员函数
Building::Building()
{
m_SittingRoom = "客厅";
m_BedRoom = "卧室";
}
GoodFriend::GoodFriend()
{
building = new Building;
}
void GoodFriend::visit()
{
cout << "visit函数正在访问" << building->m_SittingRoom << endl;
cout << "visit函数正在访问" << building->m_BedRoom << endl;
}
void GoodFriend::visit2()
{
cout << "visit2函数正在访问" << building->m_SittingRoom << endl;
// cout << "visit2函数正在访问" << building->m_BedRoom << endl;
}
void test01()
{
GoodFriend gf;
gf.visit();
gf.visit2();
}
int main()
{
test01();
return 0;
}