-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathFriend_class.cpp
95 lines (73 loc) · 2.76 KB
/
Friend_class.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <bits/stdc++.h>
using namespace std;
/*
Friend class of a class
-(all members) has access to the private and protected members of the class(breaks encapsulation)
-does not have its name qualified with the class scope (not a nested class)
-can be declared friend in more than one class
A friend class can be
-class
-class template
In the Linked list example we saw before, if we had to create another function say delete() in List class.
Then also, I need to make this displa() a friend of Node class as well. It's cumbersome.
We can achieve this by using friend class easily.
So, if a class has too many member functions to be made friend of, its better to go for friend class instead of friend
function
IMPORTANT POINTS:
- If class A is a friend of class B, it doesn't mean class B is a friend of class A
- If class A is a friend of class B and class B is a friend of class C, it doesn't mean class A is a friend of class C
Now, we are aware of three kinds of visibilities:
-public
-private
-friend : It breaks visibility structure and hence should be used judiciously in cases like :
- A function needs to access the internals of two(or more) independent classes. Ex: Matrix-vector multiplication
- A class is built on top of another class. Ex: List-Node access
- Certain situations of operator overloading. Ex: streaming operators (Later we will study this too)
*/
class Node;
class List {
Node* head; //Head of the list
Node* tail; //Tail of the list
public:
explicit List(Node* h = NULL) : head(h), tail(h) {}
void display();
void append(Node* p);
};
class Node {
int val;
Node* next;
public:
explicit Node(int i) : val(i), next(NULL) {}
//No need to add these anymore
//friend void List::display();
//friend void List::append(Node* p);
friend class List; //List is a friend of Node. So, List can access Node's private members
};
void List::display() {
Node* temp = head;
while(temp) {
cout << temp->val << " "; //Node val is private within class Node. To access it inside a member function of class List, we
//need to make display() a friend function of class Node
temp = temp->next;
}
}
void List::append(Node* p) {
if(!head)
head = tail = p;
else {
tail->next = p; //Node next is private within class Node. To access it inside a member function of class List, we
//need to make append() a friend function of class Node
tail = tail->next;
}
}
int main() {
List ll;
Node n1(1);
Node n2(2);
Node n3(3);
ll.append(&n1);
ll.append(&n2);
ll.append(&n3);
ll.display();
return 0;
}