1
00:00:01,670 --> 00:00:02,240
Hello everyone.

2
00:00:02,270 --> 00:00:05,660
So in this we do I want to discuss about friend functions.

3
00:00:05,960 --> 00:00:11,080
OK so let us take an example suppose I have a class drug now inside the class drug.

4
00:00:11,090 --> 00:00:15,610
I have some private public and protected properties so private.

5
00:00:15,610 --> 00:00:17,730
Protected and public.

6
00:00:17,740 --> 00:00:18,020
OK.

7
00:00:18,050 --> 00:00:20,360
So let's say x is a private property.

8
00:00:20,360 --> 00:00:23,930
Why is a predicted property and Z is a public property.

9
00:00:23,930 --> 00:00:24,170
OK.

10
00:00:24,200 --> 00:00:28,260
So the private property X it is not accessible outside the truck class.

11
00:00:28,280 --> 00:00:28,840
Okay.

12
00:00:28,850 --> 00:00:31,010
Similarly the protected property right.

13
00:00:31,040 --> 00:00:31,960
It is accessible.

14
00:00:31,960 --> 00:00:36,150
Our tender truck last but only with the help of derived classes and Z.

15
00:00:36,200 --> 00:00:37,420
This property is open to all.

16
00:00:37,430 --> 00:00:39,520
We can access it anywhere.

17
00:00:39,540 --> 00:00:40,100
Okay.

18
00:00:40,170 --> 00:00:43,380
Now suppose I have a class bus.

19
00:00:43,480 --> 00:00:48,230
Now the bus class has a print function okay.

20
00:00:48,320 --> 00:00:55,220
And this print function it wants to access the private property of the class.

21
00:00:55,290 --> 00:00:58,640
Okay you want to access the X..

22
00:00:58,640 --> 00:00:59,060
Okay.

23
00:00:59,090 --> 00:01:04,270
So how can this print function can access the private property of an entire class.

24
00:01:04,280 --> 00:01:04,760
Okay.

25
00:01:04,820 --> 00:01:07,730
So the answer is with the help of friend functions.

26
00:01:07,730 --> 00:01:08,240
Okay.

27
00:01:08,360 --> 00:01:15,350
So with the help of a friend functions a class a class can access the private and the protected properties

28
00:01:15,470 --> 00:01:17,220
of the order class.

29
00:01:17,430 --> 00:01:19,570
Okay so with the help of friend functions.

30
00:01:19,590 --> 00:01:24,200
A class can access the private in the protected property of the class because the public properties

31
00:01:24,200 --> 00:01:26,610
are already accessible to everyone.

32
00:01:26,640 --> 00:01:27,160
Okay.

33
00:01:27,270 --> 00:01:29,010
So what we have to do.

34
00:01:29,430 --> 00:01:34,980
So what we have to do we have to make this print function a friend of this class.

35
00:01:34,980 --> 00:01:35,520
Okay.

36
00:01:35,580 --> 00:01:38,560
Now we all know friends knows it don't.

37
00:01:38,590 --> 00:01:42,160
Okay I want to access the secret off track.

38
00:01:42,180 --> 00:01:44,580
So the print has to become the friend.

39
00:01:45,030 --> 00:01:45,630
Okay.

40
00:01:45,720 --> 00:01:47,400
Different knows the secret.

41
00:01:47,430 --> 00:01:51,630
So when the brain function will become the friend of the class.

42
00:01:51,780 --> 00:01:57,300
It can access the secret property it can access all the secrets of the track class.

43
00:01:57,300 --> 00:01:57,880
Okay.

44
00:01:57,930 --> 00:01:58,920
In order to see in gold

45
00:02:03,200 --> 00:02:03,430
okay.

46
00:02:03,440 --> 00:02:09,130
So I have a class drug now inside the gastric.

47
00:02:09,150 --> 00:02:19,790
We have decided that I have some I have some private property I have some predicted property and I will

48
00:02:19,790 --> 00:02:20,870
have some public property

49
00:02:24,800 --> 00:02:25,470
okay.

50
00:02:25,560 --> 00:02:31,290
Now I have a class bus class bus has a function

51
00:02:34,030 --> 00:02:46,090
so class bus has a function now what this function wants this function wants to excess excess the X

52
00:02:46,090 --> 00:02:47,560
of drug.

53
00:02:47,560 --> 00:02:52,180
This function was to exist the private and the public properties private and the protected properties

54
00:02:52,420 --> 00:02:53,770
of the track class.

55
00:02:53,830 --> 00:02:55,870
Okay so how can we do this.

56
00:02:55,900 --> 00:03:01,930
So the only ways what we will do we will declare this print function as a friend of the class.

57
00:03:02,860 --> 00:03:07,420
Okay so what is the syntax syntax is very simple.

58
00:03:07,480 --> 00:03:09,700
First of all we have to write friend

59
00:03:12,520 --> 00:03:21,080
then we have to write void then bus scope resolution operator the name of the function print.

60
00:03:21,850 --> 00:03:23,590
Okay so what will happen now.

61
00:03:26,190 --> 00:03:32,440
Okay so now the print function of the bus class is now a friend of the class.

62
00:03:32,520 --> 00:03:38,850
Okay the print function of the bus class is now a friend of the class.

63
00:03:38,880 --> 00:03:44,380
So this print function now can access all the secrets of the class okay.

64
00:03:44,660 --> 00:03:50,660
But if it will then this file will get an added okay that it will be at this lane.

65
00:03:50,690 --> 00:03:55,330
Let's end if I look okay so what is the added

66
00:04:00,880 --> 00:04:01,120
okay.

67
00:04:01,130 --> 00:04:04,930
So this will be Z okay.

68
00:04:04,940 --> 00:04:12,130
So it will then this file we will get an error at this line so what is their ETA is at this line.

69
00:04:12,130 --> 00:04:13,950
Compiler doesn't know what this bus.

70
00:04:13,970 --> 00:04:14,510
Okay.

71
00:04:14,680 --> 00:04:17,980
If you will see that it so bus has not been declared.

72
00:04:18,010 --> 00:04:19,600
So what happened in C++

73
00:04:22,480 --> 00:04:25,420
Okay so we are getting a compilation error at this length.

74
00:04:25,450 --> 00:04:26,980
So what happens in C++.

75
00:04:26,980 --> 00:04:32,410
So in C++ the execution starts from the mean but the compiler should start from the very first line.

76
00:04:32,440 --> 00:04:32,680
Okay.

77
00:04:32,690 --> 00:04:35,190
So the completion will be from top to bottom.

78
00:04:35,230 --> 00:04:38,950
So at this line line number well we do not have bus.

79
00:04:38,950 --> 00:04:40,570
Okay buses not declared.

80
00:04:40,570 --> 00:04:45,910
So compiler will not go at line number 15 it will not search at this land the compiler will stop and

81
00:04:45,910 --> 00:04:48,790
it will tell us that it doesn't know what is bus.

82
00:04:48,910 --> 00:04:49,140
Okay.

83
00:04:49,150 --> 00:04:55,570
So what we have to do we have to declare we have to write this code in front of before the class okay

84
00:04:55,930 --> 00:04:59,990
above the track class so let's move the whole code

85
00:05:04,640 --> 00:05:04,890
okay.

86
00:05:04,920 --> 00:05:05,910
So listen this file

87
00:05:08,870 --> 00:05:11,650
so at this time we are not getting any added.

88
00:05:11,720 --> 00:05:15,350
Okay now one more thing that we have to we have to be careful.

89
00:05:15,380 --> 00:05:19,060
So now this function is a friend of the track class.

90
00:05:19,160 --> 00:05:21,230
So we cannot give the definition here.

91
00:05:21,730 --> 00:05:22,260
Okay.

92
00:05:22,400 --> 00:05:27,850
We cannot give the definition we have to give the definition outside the class.

93
00:05:27,890 --> 00:05:28,190
Okay.

94
00:05:28,220 --> 00:05:30,950
So I am given the definition here.

95
00:05:30,950 --> 00:05:38,600
So for giving the definition the structure will be void bus scope resolution operator print okay.

96
00:05:38,720 --> 00:05:41,780
So now in this function.

97
00:05:41,840 --> 00:05:48,350
So now here within this function I can access all the secrets of the class private and protected properties.

98
00:05:48,350 --> 00:05:48,640
Okay.

99
00:05:48,650 --> 00:05:50,440
X Y and Z all are accessible.

100
00:05:51,200 --> 00:05:52,160
Okay.

101
00:05:52,370 --> 00:05:58,160
So for accessing the private properties and the protected properties first of all we will create an

102
00:05:58,160 --> 00:05:58,640
object.

103
00:05:58,700 --> 00:06:02,320
So correctly so I can access X..

104
00:06:02,380 --> 00:06:05,250
Let's say existing I can access y.

105
00:06:05,320 --> 00:06:16,530
So do not y as let's say 20 and let us also print the value of x and y so sealed do not X and do not

106
00:06:16,530 --> 00:06:16,800
v

107
00:06:19,940 --> 00:06:20,780
okay.

108
00:06:20,900 --> 00:06:24,370
Now let us call this function brain function.

109
00:06:24,380 --> 00:06:28,730
So I am creating an object must be and will call the function b dot print

110
00:06:31,650 --> 00:06:32,160
okay.

111
00:06:32,160 --> 00:06:33,090
Now listen this file

112
00:06:36,790 --> 00:06:39,090
so I would produce coming out with 10 and 20.

113
00:06:39,130 --> 00:06:41,190
Okay so I will code is working fine.

114
00:06:41,410 --> 00:06:46,770
So our code is working fine okay so let's do one more thing.

115
00:06:46,890 --> 00:06:49,920
Suppose I have a global function okay.

116
00:06:49,940 --> 00:06:51,760
So I have a normal global function.

117
00:06:51,800 --> 00:06:55,590
Let's say the name of the function is best okay.

118
00:06:55,600 --> 00:06:57,580
Now this test function

119
00:07:00,370 --> 00:07:07,350
this test function also wants to access the private and the predicted this track.

120
00:07:07,540 --> 00:07:10,930
It also wants to access the private property of the class.

121
00:07:11,500 --> 00:07:11,960
Okay.

122
00:07:12,070 --> 00:07:14,680
So what we have to do we will do the same thing.

123
00:07:14,680 --> 00:07:18,910
We will make this test function as the friend of the track class.

124
00:07:18,910 --> 00:07:19,380
Okay.

125
00:07:19,450 --> 00:07:21,010
So this index will be quite simple.

126
00:07:21,970 --> 00:07:26,410
We will write friend then void and then test.

127
00:07:26,450 --> 00:07:27,290
Okay.

128
00:07:27,500 --> 00:07:32,480
So again if you will then this file we will get the compilation error at this line because this test

129
00:07:32,480 --> 00:07:38,060
function does not belong to a class because we have written friend in before the test function.

130
00:07:38,060 --> 00:07:42,110
So this test function doesn't belong to drug class and when we will reach line number it Dean we will

131
00:07:42,110 --> 00:07:48,520
get compilation error saying that best is not defined okay because test is written below the track class.

132
00:07:48,540 --> 00:07:51,640
Okay so what we have to do we have to move this code above.

133
00:07:51,650 --> 00:07:56,720
Okay so what we can do we will write the declaration.

134
00:07:56,720 --> 00:07:58,680
So this is the declaration okay.

135
00:07:58,820 --> 00:08:01,490
And here we can write the definition.

136
00:08:01,490 --> 00:08:04,090
So now we will not get completion at it.

137
00:08:04,280 --> 00:08:05,200
Okay so.

138
00:08:05,240 --> 00:08:05,500
Okay.

139
00:08:05,510 --> 00:08:07,160
So let's copy this code only

140
00:08:12,930 --> 00:08:13,370
okay.

141
00:08:13,450 --> 00:08:16,750
So now let us call this function test from the mean.

142
00:08:16,750 --> 00:08:20,600
Okay so test now let's define

143
00:08:23,300 --> 00:08:25,520
Okay so 10 20 and again 10 20.

144
00:08:25,540 --> 00:08:27,980
So the first one is due to by Sprint function.

145
00:08:27,980 --> 00:08:32,720
The second one is due to the global function which is the friend of the class.

146
00:08:32,750 --> 00:08:33,170
Okay.

147
00:08:36,220 --> 00:08:36,490
Okay.

148
00:08:36,500 --> 00:08:40,860
So one thing to note here is this test and this print function.

149
00:08:40,860 --> 00:08:42,980
They are not the part of the class.

150
00:08:42,980 --> 00:08:43,280
Okay.

151
00:08:43,280 --> 00:08:45,500
They are not the part of the class.

152
00:08:45,500 --> 00:08:45,890
So.

153
00:08:47,570 --> 00:08:50,350
So these two functions are not part of class.

154
00:08:50,360 --> 00:08:57,860
So if I declare another function if I defined another function here let's have white display and inside

155
00:08:57,860 --> 00:08:59,390
the display function.

156
00:08:59,390 --> 00:09:07,980
So can I write C out this a can I write this.

157
00:09:08,010 --> 00:09:09,300
So is this line correct.

158
00:09:09,420 --> 00:09:10,520
Obviously this is correct.

159
00:09:10,530 --> 00:09:11,810
So what is the meaning of this.

160
00:09:11,850 --> 00:09:16,740
This may contain the address of the current object so I can access the private property inside the class.

161
00:09:16,770 --> 00:09:17,500
So this is correct.

162
00:09:17,520 --> 00:09:22,220
This implicitly means C out arrow x c out x.

163
00:09:22,260 --> 00:09:24,570
Okay so this implicitly means this only.

164
00:09:24,690 --> 00:09:25,170
Okay.

165
00:09:25,290 --> 00:09:27,990
So what is the point I want to make here is.

166
00:09:28,020 --> 00:09:31,040
Friend function do not have access to this point.

167
00:09:31,620 --> 00:09:35,650
Okay so I am writing some points here.

168
00:09:36,990 --> 00:09:37,580
Okay.

169
00:09:37,800 --> 00:09:47,500
So first of all friend function do not have access to this point that way because friend functions are

170
00:09:47,500 --> 00:09:48,760
not the part of the class.

171
00:09:49,480 --> 00:09:49,830
Okay.

172
00:09:49,840 --> 00:09:51,630
Friend function are not the part of the class.

173
00:09:51,640 --> 00:09:54,990
So you do not have access to this pointer okay.

174
00:09:55,240 --> 00:10:00,440
Now the second point is we can put the friend function under any excess modifier.

175
00:10:01,000 --> 00:10:08,530
Okay so we can put these friend functions under any excess modifier.

176
00:10:08,780 --> 00:10:10,800
There will be no change okay.

177
00:10:11,060 --> 00:10:11,660
No effect.

178
00:10:12,140 --> 00:10:16,620
So the point is the excess modifiers do not work for friend function.

179
00:10:16,670 --> 00:10:18,690
Okay so I am writing here.

180
00:10:18,900 --> 00:10:19,960
Access modify it

181
00:10:23,800 --> 00:10:31,640
does not look for a friend function Y because the excess modified defined the accessibility of the members

182
00:10:31,640 --> 00:10:36,980
of the class and friend functions are not part of the class since friend function not the part of the

183
00:10:36,980 --> 00:10:37,300
class.

184
00:10:37,310 --> 00:10:40,830
Then access modify it cannot be used for friend function.

185
00:10:40,850 --> 00:10:47,510
So what I want to say here is so here the friend functions are in that the public we can move it to

186
00:10:47,510 --> 00:10:50,080
protected and we can also move it to private okay.

187
00:10:50,240 --> 00:10:53,320
There will be no changes okay.

188
00:10:53,390 --> 00:10:58,340
Now what I want is so we have a bus class inside the bus class.

189
00:10:58,400 --> 00:11:06,170
Let's say we have many functions so print one function similarly we have print to function print three

190
00:11:06,170 --> 00:11:09,830
functions print for function and so on.

191
00:11:09,860 --> 00:11:12,160
Okay so inside the bus class.

192
00:11:12,170 --> 00:11:14,850
Suppose we have many functions okay.

193
00:11:14,930 --> 00:11:19,130
And all these functions want to access the secrets of the class.

194
00:11:19,130 --> 00:11:19,660
Okay.

195
00:11:19,730 --> 00:11:24,600
All these functions wants to access the private and the protected properties of the upper class.

196
00:11:24,770 --> 00:11:26,830
So there are two ways to do this.

197
00:11:26,870 --> 00:11:32,630
So the first raise what we will do we will make all these functions as friend a function of their track

198
00:11:32,630 --> 00:11:33,750
class.

199
00:11:33,950 --> 00:11:35,430
Okay so this is the first Ruby.

200
00:11:35,660 --> 00:11:39,480
The second way the shortcut is what we will do.

201
00:11:39,650 --> 00:11:43,070
We will make the bus class as a friend of class.

202
00:11:43,190 --> 00:11:45,120
Okay we will make bus class.

203
00:11:45,110 --> 00:11:47,900
We will declare bus class as a friend of class.

204
00:11:48,020 --> 00:11:49,370
So the syntax is very simple

205
00:11:52,130 --> 00:11:52,350
okay.

206
00:11:52,370 --> 00:11:55,750
So let's combine these two lines.

207
00:11:55,920 --> 00:12:05,360
So what I want to do I want to make I want to make I want to declare I want to declare bus as a friend

208
00:12:05,360 --> 00:12:06,150
after class.

209
00:12:06,170 --> 00:12:10,770
So the syntax is very simple friend class bus okay.

210
00:12:10,940 --> 00:12:12,900
So what will happen with the help of this line.

211
00:12:12,980 --> 00:12:18,920
So with the help of this line what I want to say here is now the class bus is a friend of class.

212
00:12:19,280 --> 00:12:25,700
Okay so all the functions all the all the member function of the bus class can access all the properties

213
00:12:25,700 --> 00:12:28,160
of the truck class okay.

214
00:12:28,220 --> 00:12:32,530
Every member function of the bus class is now a friend of the truck class.

215
00:12:32,600 --> 00:12:38,180
Okay so these type of classes these type of classes are called Friend classes.

216
00:12:38,330 --> 00:12:47,250
Okay so these type of classes are called Friend classes so we know what our friend classes.

217
00:12:47,250 --> 00:12:47,880
Okay.

218
00:12:48,060 --> 00:12:56,250
Now the fourth point is the friendship is one way so we have one with friendship.

219
00:12:56,250 --> 00:12:57,620
So what is the meaning of when we.

220
00:12:57,630 --> 00:12:59,680
Friendship.

221
00:12:59,700 --> 00:13:02,250
So what is the meaning of a new friendship.

222
00:13:02,370 --> 00:13:07,730
So class drug has declared bus as his friend.

223
00:13:08,280 --> 00:13:12,140
So bus can access all the properties of the track class.

224
00:13:12,230 --> 00:13:15,300
Okay but divisive outside is not true.

225
00:13:15,480 --> 00:13:22,080
Okay bus can access all the properties of the truck class because truck has declared bus as its friend

226
00:13:22,440 --> 00:13:24,450
but the vice MSA is not true.

227
00:13:24,450 --> 00:13:28,970
Truck cannot access the private and the predicted properties of the bus class.

228
00:13:29,100 --> 00:13:32,140
Okay because bus has not declared Brooke as its friend.

229
00:13:32,220 --> 00:13:36,480
Now okay because bus has not declared drug as a stranger yet.

230
00:13:37,530 --> 00:13:41,640
Okay so the friendship is one where friendship okay.

231
00:13:41,800 --> 00:13:48,660
So when we friendship and we know what is a friend plus excess money if I will not vote for a friend

232
00:13:48,670 --> 00:13:52,850
glasses and Frank glasses do not have access to this point.

233
00:13:53,020 --> 00:13:53,260
Okay.

234
00:13:53,260 --> 00:13:56,110
Friend functions do not have access to this point.

235
00:13:57,210 --> 00:14:00,180
Okay so I hope everything is cleared.

236
00:14:00,180 --> 00:14:00,600
Thank you.
