1
00:00:01,580 --> 00:00:02,210
Hello everyone.

2
00:00:02,210 --> 00:00:05,750
So in the last video we started discussing about the volume of freedom.

3
00:00:05,760 --> 00:00:08,560
So we have discussed about compiled them polymorphism.

4
00:00:08,570 --> 00:00:11,270
Now let's discuss about the runtime polymorphism.

5
00:00:11,270 --> 00:00:13,250
So what is the meaning offering them polymorphism.

6
00:00:13,250 --> 00:00:16,470
So it means that I will take the decision at runtime.

7
00:00:16,490 --> 00:00:16,930
Okay.

8
00:00:17,090 --> 00:00:20,120
What operations you want to perform will be decided at different time.

9
00:00:20,480 --> 00:00:22,730
So in the last two year deal we did something like this

10
00:00:26,700 --> 00:00:29,860
so in the last review we did something like this.

11
00:00:29,870 --> 00:00:35,900
We created a vehicle pointer and then the vehicle pointer was pointing towards the derived class object

12
00:00:36,200 --> 00:00:39,090
so we can write like this also.

13
00:00:39,650 --> 00:00:46,840
So we can start we do this is new called okay.

14
00:00:46,870 --> 00:00:50,440
So base class pointer is pointing towards a god class object.

15
00:00:50,650 --> 00:00:51,240
Okay.

16
00:00:51,250 --> 00:00:56,750
And when we did something like this when we are calling the print function then what will happen.

17
00:00:57,570 --> 00:01:00,690
So the car and vehicle both have brain functions.

18
00:01:00,690 --> 00:01:03,330
So when I am writing with two arrow print.

19
00:01:03,330 --> 00:01:07,110
So the vehicle print function will be called Why Do We Call brain function.

20
00:01:07,110 --> 00:01:12,870
Because through we do we can only call the properties through we do we can only access those properties

21
00:01:12,930 --> 00:01:14,830
that are present in vehicle class.

22
00:01:14,850 --> 00:01:15,410
Okay.

23
00:01:15,540 --> 00:01:22,700
So why do we call print function will be called because we are deciding at compile time only that this

24
00:01:22,740 --> 00:01:27,540
we do is an object of type vehicle essentially does it off type vehicle.

25
00:01:27,790 --> 00:01:30,470
That's why we will call the vehicle brain function.

26
00:01:30,490 --> 00:01:30,940
Okay.

27
00:01:31,030 --> 00:01:34,420
Now what I want is I want to take decision at runtime.

28
00:01:34,720 --> 00:01:35,340
Okay.

29
00:01:35,350 --> 00:01:44,680
So I want when I write to Arrow print then instead of vehicle print function car class print function

30
00:01:44,680 --> 00:01:48,060
should we call Okay white car class.

31
00:01:48,370 --> 00:01:52,510
Because we do this pointing towards a car class object.

32
00:01:52,660 --> 00:01:52,950
Okay.

33
00:01:52,960 --> 00:01:55,990
So we do is pointing towards a car class object.

34
00:01:56,020 --> 00:01:57,720
That's why I want that.

35
00:01:57,760 --> 00:02:01,030
When I write we do it or print guard last spring function should be called.

36
00:02:01,790 --> 00:02:02,430
Okay.

37
00:02:02,470 --> 00:02:06,660
Now this is runtime polymorphism because I am one too big decision at one time.

38
00:02:06,820 --> 00:02:11,920
Okay now to achieve that in polymorphism we need to take help of virtual functions.

39
00:02:11,920 --> 00:02:12,410
Okay.

40
00:02:12,520 --> 00:02:17,710
So first of all let us try to understand what our virtual functions okay.

41
00:02:17,840 --> 00:02:19,590
So our goal is very simple.

42
00:02:19,700 --> 00:02:26,990
What is our goal I want when I write we do arrow print then instead of the kill brain function I want

43
00:02:26,990 --> 00:02:29,690
to call the print function of the card class.

44
00:02:29,780 --> 00:02:30,360
Okay.

45
00:02:30,380 --> 00:02:32,170
And via the print function of calculus.

46
00:02:32,180 --> 00:02:35,750
Because we do is pointing towards a kind of class object.

47
00:02:36,610 --> 00:02:37,370
Okay.

48
00:02:37,520 --> 00:02:43,570
Now to achieve this we need runtime polymorphism and to achieve that in time polymorphism.

49
00:02:43,580 --> 00:02:46,010
We need two big help of virtual functions.

50
00:02:46,520 --> 00:02:49,180
So now let us discuss about.

51
00:02:49,370 --> 00:02:52,410
Now let us discuss what that virtual functions.

52
00:02:52,760 --> 00:02:58,070
So virtual functions are those functions that are present in the base class.

53
00:02:58,430 --> 00:03:02,530
And that has been overridden in the derived class.

54
00:03:02,540 --> 00:03:02,980
Okay.

55
00:03:03,110 --> 00:03:08,560
So these function will be present in the base class and then we will override laws function indeed derived

56
00:03:08,570 --> 00:03:09,710
class.

57
00:03:09,710 --> 00:03:10,180
Okay.

58
00:03:10,250 --> 00:03:13,370
So how to make a function of virtual function.

59
00:03:14,120 --> 00:03:19,640
So in order to make a function of virtual function we need to write the keyword virtual.

60
00:03:19,640 --> 00:03:20,270
Okay.

61
00:03:20,390 --> 00:03:22,130
So where do we do.

62
00:03:22,310 --> 00:03:25,450
I want to make this print function as a virtual function.

63
00:03:25,460 --> 00:03:29,520
So I have to write the keyword virtual okay.

64
00:03:29,530 --> 00:03:35,530
So now this function is a virtual function why because I have written the keyword virtual and also this

65
00:03:35,530 --> 00:03:38,140
function has been overwritten in the derived class.

66
00:03:38,850 --> 00:03:39,320
Okay.

67
00:03:39,430 --> 00:03:43,450
So what is the meaning what we will achieve by writing the keyword virtual.

68
00:03:43,960 --> 00:03:44,160
Okay.

69
00:03:44,170 --> 00:03:46,920
So the virtual keyword with the help of virtual keyword.

70
00:03:47,050 --> 00:03:50,080
I am telling the compiler to take the decision at runtime.

71
00:03:50,590 --> 00:03:55,030
Okay I will tell the compiler to dig with the editor in time at which print function to be called.

72
00:03:55,420 --> 00:03:55,840
Okay.

73
00:03:55,930 --> 00:04:01,510
So what will happen that in time I will go and check which type of object view two points to.

74
00:04:01,510 --> 00:04:05,140
Okay so at the time when I when I am writing the statement.

75
00:04:05,140 --> 00:04:09,340
So when the compiler will reach here what will happen at the right time.

76
00:04:09,350 --> 00:04:14,140
Compiler will go and check what type of what type of object view points to.

77
00:04:14,140 --> 00:04:20,230
So we two points to discard class object since we are two points to car class object then called Class

78
00:04:20,230 --> 00:04:21,500
print function will be called.

79
00:04:22,560 --> 00:04:23,090
Okay.

80
00:04:23,200 --> 00:04:28,780
Now why I am taking this with at one time because I have written the keyword module and also this function

81
00:04:28,780 --> 00:04:30,970
has been overwritten in the car class.

82
00:04:31,110 --> 00:04:31,420
Okay.

83
00:04:31,420 --> 00:04:32,910
And they're derived class.

84
00:04:33,220 --> 00:04:40,730
Now when I will run this file what will happen this car class brain function will be called so let's

85
00:04:40,730 --> 00:04:46,920
send the file so we can see here the output is God.

86
00:04:47,550 --> 00:04:48,130
Okay.

87
00:04:48,150 --> 00:04:49,940
So I am repeating myself again.

88
00:04:49,950 --> 00:04:51,640
So what will happen at that time.

89
00:04:51,690 --> 00:04:55,800
I will go and check what this voodoo object points to.

90
00:04:55,800 --> 00:05:00,040
If it is pointing to a car class then countless print function will be called.

91
00:05:00,150 --> 00:05:04,540
If it is pointing to a vehicle class then we call class brain function will be called.

92
00:05:04,600 --> 00:05:07,540
Okay now let's see one more example.

93
00:05:07,770 --> 00:05:13,590
Now what will happen if I if I will remove the print function of the car class.

94
00:05:13,880 --> 00:05:14,350
Okay.

95
00:05:14,460 --> 00:05:17,530
So what will happen what will the output of this line we do it opened.

96
00:05:17,760 --> 00:05:23,790
Okay so let's say in our file so the output is coming out to be we call.

97
00:05:23,920 --> 00:05:24,600
Okay.

98
00:05:24,770 --> 00:05:27,440
Okay so the output is coming out to be we call.

99
00:05:27,580 --> 00:05:29,980
Now let us try to understand.

100
00:05:29,980 --> 00:05:35,940
So when I write we do error print now which print function will be called.

101
00:05:36,010 --> 00:05:38,490
So we do is of type we call.

102
00:05:38,620 --> 00:05:43,400
But since I have written virtually in the base class so I have to take the decision at runtime.

103
00:05:43,540 --> 00:05:45,880
So we will check at the wrong time.

104
00:05:46,780 --> 00:05:47,650
So this is God.

105
00:05:47,680 --> 00:05:51,090
This is we call this is print function and this is the print function of the card.

106
00:05:51,280 --> 00:05:54,880
So we do is pointing towards a card class object.

107
00:05:54,880 --> 00:05:57,420
So this time this print function will be called.

108
00:05:57,640 --> 00:05:58,290
Okay.

109
00:05:58,300 --> 00:06:01,400
Now then I will remove the print function from the Card class.

110
00:06:01,450 --> 00:06:02,860
Then what will happen again.

111
00:06:02,950 --> 00:06:05,710
I will check whether the Card class has a print function.

112
00:06:05,710 --> 00:06:06,550
No it doesn't have.

113
00:06:06,580 --> 00:06:11,110
So I will go to its parent class its parent class has print function.

114
00:06:11,110 --> 00:06:14,290
So this time this print function of the weekly class will be called.

115
00:06:14,380 --> 00:06:14,610
Okay.

116
00:06:14,620 --> 00:06:19,360
And output will be weakened if the vehicle last doesn't have brain function then this line will throw

117
00:06:19,360 --> 00:06:20,580
us at it.

118
00:06:20,630 --> 00:06:22,420
Okay.

119
00:06:22,570 --> 00:06:26,350
Now what will happen if I comment the print function of the vehicle.

120
00:06:26,410 --> 00:06:27,680
Okay.

121
00:06:28,420 --> 00:06:29,740
Now let's.

122
00:06:29,740 --> 00:06:37,830
So we have print function inside the car class but we do not have print function for the vehicle class.

123
00:06:38,000 --> 00:06:38,660
Okay.

124
00:06:38,780 --> 00:06:41,750
So if I will run this file what will be our output.

125
00:06:41,750 --> 00:06:43,100
I think we will go down at it.

126
00:06:43,310 --> 00:06:43,730
Okay.

127
00:06:43,730 --> 00:06:47,480
Now let us try to understand why so we are getting an edit.

128
00:06:47,540 --> 00:06:53,700
And if you will see that it so class recall no name to print.

129
00:06:54,030 --> 00:06:58,970
Okay so what it is saying here is we do not have print function inside that we call class.

130
00:06:59,120 --> 00:06:59,610
OK.

131
00:06:59,640 --> 00:07:04,770
We do not have print function inside that we call it lies and we all know we can only access those properties

132
00:07:04,800 --> 00:07:07,660
that are present in difficult class through V2.

133
00:07:07,920 --> 00:07:08,590
Okay.

134
00:07:08,640 --> 00:07:09,700
I am repeating myself.

135
00:07:09,720 --> 00:07:15,390
We can only access those properties through we do that are present in difficult class and vacant class

136
00:07:15,390 --> 00:07:17,250
doesn't have any print function.

137
00:07:17,250 --> 00:07:17,840
Okay.

138
00:07:18,060 --> 00:07:22,920
So in this way with the help of virtual function we can achieve runtime polymorphism.

139
00:07:23,700 --> 00:07:26,000
Okay now let us try to.

140
00:07:26,010 --> 00:07:32,370
Now let's discuss about the use case of virtual function why we will need virtual function and why we

141
00:07:32,370 --> 00:07:33,780
need runtime polymorphism.

142
00:07:33,900 --> 00:07:34,170
Okay

143
00:07:38,920 --> 00:07:45,190
so suppose we have all organization in organization we have different type of employees and what we

144
00:07:45,190 --> 00:07:50,950
want is we want to convert the salaries of employees at the end of the month and the formula for calculating

145
00:07:50,950 --> 00:07:53,800
the salaries for different employee is different.

146
00:07:53,800 --> 00:08:00,310
Okay so suppose we have four type of employees we have a chart we have manager

147
00:08:03,110 --> 00:08:04,450
we have engineers

148
00:08:07,300 --> 00:08:08,600
and others.

149
00:08:08,710 --> 00:08:09,210
Okay.

150
00:08:09,370 --> 00:08:15,580
So we have four types of employees and we want to calculate the salary for all the four employees and

151
00:08:15,580 --> 00:08:18,130
the salary formula is different for different employee.

152
00:08:18,460 --> 00:08:18,910
Okay.

153
00:08:19,060 --> 00:08:21,460
So how we can solve this problem.

154
00:08:21,490 --> 00:08:28,870
So what we will do let's say I will make a class EMPLOYEE Okay.

155
00:08:28,900 --> 00:08:32,730
All the same features all the common features of the four employees.

156
00:08:32,730 --> 00:08:34,720
It will be present in the Employee class.

157
00:08:34,780 --> 00:08:44,770
Then we will create four classes H.R. manager engineer and that's okay.

158
00:08:44,780 --> 00:08:46,790
So we will use inheritance here.

159
00:08:46,820 --> 00:08:47,320
Okay.

160
00:08:47,450 --> 00:08:54,040
So H.R. manager engineers have said that they will inherit from the Employee class.

161
00:08:54,230 --> 00:08:54,700
Okay.

162
00:08:54,770 --> 00:08:59,540
So we are using inheritance here and all the common features of the employees are present in the Employee

163
00:08:59,540 --> 00:08:59,960
class.

164
00:09:00,830 --> 00:09:01,460
Okay.

165
00:09:01,550 --> 00:09:04,070
Now I have a calculate salary function.

166
00:09:04,070 --> 00:09:04,360
Okay.

167
00:09:04,370 --> 00:09:10,210
So I have a function calculate salary.

168
00:09:10,360 --> 00:09:10,590
Okay.

169
00:09:10,600 --> 00:09:14,570
So each class will have this function each other will have conferred salary function.

170
00:09:14,580 --> 00:09:18,410
Manager will also have converged salary function ingenious and others.

171
00:09:18,430 --> 00:09:21,970
Okay so all the four classes will have this function.

172
00:09:22,000 --> 00:09:26,560
Okay so called wrote Sally function is a convertible function is a common function.

173
00:09:26,560 --> 00:09:31,260
Okay now let's say I have one manager in the company in the organization.

174
00:09:31,510 --> 00:09:39,480
Let's say we have we have one nature and we have three managers let's say we have 20 engineers and that

175
00:09:39,490 --> 00:09:40,230
staffers.

176
00:09:40,260 --> 00:09:41,420
Let's say 4.

177
00:09:41,460 --> 00:09:45,680
So in total we have condemned employees okay.

178
00:09:45,770 --> 00:09:49,160
Now what we will do we will create an array of pointers.

179
00:09:49,190 --> 00:09:49,470
Okay.

180
00:09:49,490 --> 00:10:01,830
So what I will write so we will write I want to create array of pointers so employ that was start let's

181
00:10:01,870 --> 00:10:02,240
say.

182
00:10:02,270 --> 00:10:03,850
Name is E..

183
00:10:03,980 --> 00:10:05,210
This is new employee

184
00:10:08,920 --> 00:10:13,170
start and guaranteed I'll give I don't date because we have to get employees.

185
00:10:13,260 --> 00:10:15,480
Okay so this is area of point those.

186
00:10:15,490 --> 00:10:16,040
Okay.

187
00:10:16,300 --> 00:10:18,590
So we have Eddie.

188
00:10:18,610 --> 00:10:25,290
I'll say he's 28 so this is 0 1 2 3 and so on

189
00:10:31,840 --> 00:10:36,220
so we know that the base class pointer can point to a child class object.

190
00:10:36,220 --> 00:10:36,710
Okay.

191
00:10:36,790 --> 00:10:41,980
So this is an area of pointer and a base class pointer can point to a child class object.

192
00:10:42,100 --> 00:10:43,860
So the index is 0.

193
00:10:44,020 --> 00:10:46,200
It will point to a charter class object.

194
00:10:46,360 --> 00:10:52,810
Okay index 1 it will point to a manager class object simply manager and manager.

195
00:10:52,960 --> 00:10:57,230
And then we will have in juniors and finally at those.

196
00:10:57,320 --> 00:11:05,220
Okay so why it is so because because we know that the base class point that can point towards a child

197
00:11:05,220 --> 00:11:05,900
class object.

198
00:11:05,940 --> 00:11:06,250
Okay.

199
00:11:06,270 --> 00:11:12,060
So the index 0 is pointing towards a tag class object and next one is pointing towards manager class

200
00:11:12,060 --> 00:11:13,000
object and so on.

201
00:11:13,410 --> 00:11:14,040
Okay.

202
00:11:14,040 --> 00:11:18,720
Now I want to calculate this entity so vote calculating this I leave what do we do we will run a loop

203
00:11:18,780 --> 00:11:25,680
or this area okay we will run a loop or disarray and inside the loop what I will write I will write

204
00:11:25,680 --> 00:11:37,890
something like this E I l calculate salary I will call this function yay annual calculate salary.

205
00:11:37,930 --> 00:11:41,870
So we will write inside the loop e and can get salary.

206
00:11:42,010 --> 00:11:47,830
Okay now suppose this employee class it also has a good salary function.

207
00:11:47,830 --> 00:11:55,930
So this employee class also has a calculate salary function and that function is a virtual function.

208
00:11:56,060 --> 00:12:01,540
Okay that converts a little function inside the Employee class is a virtual function.

209
00:12:01,570 --> 00:12:04,350
So when I will write the statement the I had a good salary.

210
00:12:04,360 --> 00:12:05,190
What will happen.

211
00:12:05,260 --> 00:12:08,820
We will take the decision at runtime that which function will be called.

212
00:12:08,950 --> 00:12:09,560
Okay.

213
00:12:09,610 --> 00:12:18,290
Since we have written what Juliette and all the four classes and all the four classes have called cell

214
00:12:18,290 --> 00:12:19,410
will function.

215
00:12:19,450 --> 00:12:21,130
So at this line what will happen.

216
00:12:21,130 --> 00:12:26,700
We will take the decision at runtime that which calculate salary function we have to call.

217
00:12:26,920 --> 00:12:29,910
Okay so at that time what will happen.

218
00:12:30,160 --> 00:12:31,380
Which function to be called.

219
00:12:31,390 --> 00:12:33,430
It will depend upon the type of the object.

220
00:12:34,030 --> 00:12:39,330
Okay so for index 0 for index 0.

221
00:12:39,330 --> 00:12:40,220
What will happen.

222
00:12:40,230 --> 00:12:44,570
So index 0 is pointing towards a chart class object.

223
00:12:44,610 --> 00:12:52,120
Okay so for index 0 it Jared calculates every function will be called for index 1 manager calculate

224
00:12:52,120 --> 00:12:54,280
salary function will be called and so on.

225
00:12:55,290 --> 00:12:57,220
Okay so which function will be called.

226
00:12:57,240 --> 00:12:58,600
We will decide that on time.

227
00:12:58,950 --> 00:13:04,400
So it depends upon the type of the object which is present at the item takes.

228
00:13:04,440 --> 00:13:10,900
Okay so the item makes it really decide which print function will be called okay.

229
00:13:10,930 --> 00:13:16,110
So I hope the use case of what actual function and random polymorphism is now clear.

230
00:13:17,140 --> 00:13:17,950
Okay.

231
00:13:18,170 --> 00:13:18,590
Thank you.
