Hello everyone.
So in the last video we started discussing about the volume of freedom.
So we have discussed about compiled them polymorphism.
Now let's discuss about the runtime polymorphism.
So what is the meaning offering them polymorphism.
So it means that I will take the decision at runtime.
Okay.
What operations you want to perform will be decided at different time.
So in the last two year deal we did something like this
so in the last review we did something like this.
We created a vehicle pointer and then the vehicle pointer was pointing towards the derived class object
so we can write like this also.
So we can start we do this is new called okay.
So base class pointer is pointing towards a god class object.
Okay.
And when we did something like this when we are calling the print function then what will happen.
So the car and vehicle both have brain functions.
So when I am writing with two arrow print.
So the vehicle print function will be called Why Do We Call brain function.
Because through we do we can only call the properties through we do we can only access those properties
that are present in vehicle class.
Okay.
So why do we call print function will be called because we are deciding at compile time only that this
we do is an object of type vehicle essentially does it off type vehicle.
That's why we will call the vehicle brain function.
Okay.
Now what I want is I want to take decision at runtime.
Okay.
So I want when I write to Arrow print then instead of vehicle print function car class print function
should we call Okay white car class.
Because we do this pointing towards a car class object.
Okay.
So we do is pointing towards a car class object.
That's why I want that.
When I write we do it or print guard last spring function should be called.
Okay.
Now this is runtime polymorphism because I am one too big decision at one time.
Okay now to achieve that in polymorphism we need to take help of virtual functions.
Okay.
So first of all let us try to understand what our virtual functions okay.
So our goal is very simple.
What is our goal I want when I write we do arrow print then instead of the kill brain function I want
to call the print function of the card class.
Okay.
And via the print function of calculus.
Because we do is pointing towards a kind of class object.
Okay.
Now to achieve this we need runtime polymorphism and to achieve that in time polymorphism.
We need two big help of virtual functions.
So now let us discuss about.
Now let us discuss what that virtual functions.
So virtual functions are those functions that are present in the base class.
And that has been overridden in the derived class.
Okay.
So these function will be present in the base class and then we will override laws function indeed derived
class.
Okay.
So how to make a function of virtual function.
So in order to make a function of virtual function we need to write the keyword virtual.
Okay.
So where do we do.
I want to make this print function as a virtual function.
So I have to write the keyword virtual okay.
So now this function is a virtual function why because I have written the keyword virtual and also this
function has been overwritten in the derived class.
Okay.
So what is the meaning what we will achieve by writing the keyword virtual.
Okay.
So the virtual keyword with the help of virtual keyword.
I am telling the compiler to take the decision at runtime.
Okay I will tell the compiler to dig with the editor in time at which print function to be called.
Okay.
So what will happen that in time I will go and check which type of object view two points to.
Okay so at the time when I when I am writing the statement.
So when the compiler will reach here what will happen at the right time.
Compiler will go and check what type of what type of object view points to.
So we two points to discard class object since we are two points to car class object then called Class
print function will be called.
Okay.
Now why I am taking this with at one time because I have written the keyword module and also this function
has been overwritten in the car class.
Okay.
And they're derived class.
Now when I will run this file what will happen this car class brain function will be called so let's
send the file so we can see here the output is God.
Okay.
So I am repeating myself again.
So what will happen at that time.
I will go and check what this voodoo object points to.
If it is pointing to a car class then countless print function will be called.
If it is pointing to a vehicle class then we call class brain function will be called.
Okay now let's see one more example.
Now what will happen if I if I will remove the print function of the car class.
Okay.
So what will happen what will the output of this line we do it opened.
Okay so let's say in our file so the output is coming out to be we call.
Okay.
Okay so the output is coming out to be we call.
Now let us try to understand.
So when I write we do error print now which print function will be called.
So we do is of type we call.
But since I have written virtually in the base class so I have to take the decision at runtime.
So we will check at the wrong time.
So this is God.
This is we call this is print function and this is the print function of the card.
So we do is pointing towards a card class object.
So this time this print function will be called.
Okay.
Now then I will remove the print function from the Card class.
Then what will happen again.
I will check whether the Card class has a print function.
No it doesn't have.
So I will go to its parent class its parent class has print function.
So this time this print function of the weekly class will be called.
Okay.
And output will be weakened if the vehicle last doesn't have brain function then this line will throw
us at it.
Okay.
Now what will happen if I comment the print function of the vehicle.
Okay.
Now let's.
So we have print function inside the car class but we do not have print function for the vehicle class.
Okay.
So if I will run this file what will be our output.
I think we will go down at it.
Okay.
Now let us try to understand why so we are getting an edit.
And if you will see that it so class recall no name to print.
Okay so what it is saying here is we do not have print function inside that we call class.
OK.
We do not have print function inside that we call it lies and we all know we can only access those properties
that are present in difficult class through V2.
Okay.
I am repeating myself.
We can only access those properties through we do that are present in difficult class and vacant class
doesn't have any print function.
Okay.
So in this way with the help of virtual function we can achieve runtime polymorphism.
Okay now let us try to.
Now let's discuss about the use case of virtual function why we will need virtual function and why we
need runtime polymorphism.
Okay
so suppose we have all organization in organization we have different type of employees and what we
want is we want to convert the salaries of employees at the end of the month and the formula for calculating
the salaries for different employee is different.
Okay so suppose we have four type of employees we have a chart we have manager
we have engineers
and others.
Okay.
So we have four types of employees and we want to calculate the salary for all the four employees and
the salary formula is different for different employee.
Okay.
So how we can solve this problem.
So what we will do let's say I will make a class EMPLOYEE Okay.
All the same features all the common features of the four employees.
It will be present in the Employee class.
Then we will create four classes H.R. manager engineer and that's okay.
So we will use inheritance here.
Okay.
So H.R. manager engineers have said that they will inherit from the Employee class.
Okay.
So we are using inheritance here and all the common features of the employees are present in the Employee
class.
Okay.
Now I have a calculate salary function.
Okay.
So I have a function calculate salary.
Okay.
So each class will have this function each other will have conferred salary function.
Manager will also have converged salary function ingenious and others.
Okay so all the four classes will have this function.
Okay so called wrote Sally function is a convertible function is a common function.
Okay now let's say I have one manager in the company in the organization.
Let's say we have we have one nature and we have three managers let's say we have 20 engineers and that
staffers.
Let's say 4.
So in total we have condemned employees okay.
Now what we will do we will create an array of pointers.
Okay.
So what I will write so we will write I want to create array of pointers so employ that was start let's
say.
Name is E..
This is new employee
start and guaranteed I'll give I don't date because we have to get employees.
Okay so this is area of point those.
Okay.
So we have Eddie.
I'll say he's 28 so this is 0 1 2 3 and so on
so we know that the base class pointer can point to a child class object.
Okay.
So this is an area of pointer and a base class pointer can point to a child class object.
So the index is 0.
It will point to a charter class object.
Okay index 1 it will point to a manager class object simply manager and manager.
And then we will have in juniors and finally at those.
Okay so why it is so because because we know that the base class point that can point towards a child
class object.
Okay.
So the index 0 is pointing towards a tag class object and next one is pointing towards manager class
object and so on.
Okay.
Now I want to calculate this entity so vote calculating this I leave what do we do we will run a loop
or this area okay we will run a loop or disarray and inside the loop what I will write I will write
something like this E I l calculate salary I will call this function yay annual calculate salary.
So we will write inside the loop e and can get salary.
Okay now suppose this employee class it also has a good salary function.
So this employee class also has a calculate salary function and that function is a virtual function.
Okay that converts a little function inside the Employee class is a virtual function.
So when I will write the statement the I had a good salary.
What will happen.
We will take the decision at runtime that which function will be called.
Okay.
Since we have written what Juliette and all the four classes and all the four classes have called cell
will function.
So at this line what will happen.
We will take the decision at runtime that which calculate salary function we have to call.
Okay so at that time what will happen.
Which function to be called.
It will depend upon the type of the object.
Okay so for index 0 for index 0.
What will happen.
So index 0 is pointing towards a chart class object.
Okay so for index 0 it Jared calculates every function will be called for index 1 manager calculate
salary function will be called and so on.
Okay so which function will be called.
We will decide that on time.
So it depends upon the type of the object which is present at the item takes.
Okay so the item makes it really decide which print function will be called okay.
So I hope the use case of what actual function and random polymorphism is now clear.
Okay.
Thank you.