-
Notifications
You must be signed in to change notification settings - Fork 7
/
friendClassAndFunction.cpp
61 lines (48 loc) · 1.16 KB
/
friendClassAndFunction.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
#include<bits/stdc++.h>
using namespace std;
// Predecleration of class B is necessary becuase it is being passed as an argument in function1
class B;
class A {
private:
int x = 2;
public:
/*
If it is to be used as a friend to some other class, it should be defined after that class's definition
and not int class A.
*/
void function1(B);
/*
It means that C (to be specific all member functions of C)can access all the private members of A
*/
friend class C;
};
class B {
private:
int x = 0;
/*
This statement implies that function1 from class A is a friend of class B
and can access private members of class B
*/
friend void A::function1(B);
};
/*
Definition of the member function from class A which is friend to class B
*/
void A::function1(B b) {
cout << "In Function 1 of class A\n";
cout << "Accessing value of x(Private member of B) outside class B: b.x = " << b.x << endl;
}
class C {
public:
void showA(A a) {
cout << "In showA function of class C\n";
cout << "Accessing value of x(Private member of A) outside class A: a.x = " << a.x << endl;
}
};
int main() {
A a;
B b;
C c;
a.function1(b);
c.showA(a);
}