-
Notifications
You must be signed in to change notification settings - Fork 28
/
Why_Friend_Function_II.cpp
70 lines (56 loc) · 1.73 KB
/
Why_Friend_Function_II.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
#include <bits/stdc++.h>
using namespace std;
/*
Friend function Example-II to show why it's required
-When a function needs to access private data members of two or more different/independent classes and do some operation,
and at the same time the function can't be a member function of any one class because it won't be able to access private
members of other class, then friend function comes to rescue in this situation.
So, using friend function, a member function of one class can access private members of another class.
*/
class Node;
class List {
Node* head; //Head of the list
Node* tail; //Tail of the list
public:
List(Node* h = NULL) : head(h), tail(h) {}
void display();
void append(Node* p);
};
class Node {
int val;
Node* next;
public:
Node(int i) : val(i), next(NULL) {}
friend void List::display();
friend void List::append(Node* p);
};
//Member function
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;
}
}
//Member function
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;
}