Hello everyone.
So till now we have discussed in detail about inheritance.
I hope that part is now clear.
Now in this video I want to discuss next topic of Opes.
Note This polymorphism so polymorphism means many forms okay.
So in terms of code we can see polymorphism means there a set of code behave differently in different
situation.
Okay.
So there are two types of polymorphism.
So first one is the compiler that polymorphism and the second one is runtime polymorphism.
So compile time polymorphism means how a set of code will behave outside of court will behave will we
decided at the compile time Okay and then type one on one with him how the set of code will behave will
be decided at the current time okay.
So in this we do our focus is on compile time polymorphism and the next we deal we will cover in time
polymorphism as well okay.
So compile time polymorphism to compile time polymorphism can be achieved using movies.
So the first way to achieve compile them polymorphism is function overloading which we have already
started.
And the second way to achieve compile time polymorphism is operator overloading which we have also started
in OBS in details.
Okay so what is the meaning of function overloading.
So it means that different functions with the same name but destructor will be different.
Okay so we will have many functions we will have many functions.
Those function will have the same name but destructor will be different.
Okay so let's see in code.
Okay.
So I have this called so as we can see here we have many functions with the same name based function
test function test function all the three functions have the same name but then the modified arguments
are different.
So if I will call best then it will call this function because a number of 5 arguments are do.
If I will write test 1 then this function will be called okay because of the number of arguments.
So we are deciding at the compile them which function will be called.
Okay.
So we are deciding that the compiler which function will be called.
That's why we can use function overloading to achieve compile time polymorphism.
Okay.
So what will happen if I create one more function.
Suppose I have these three functions and now I am creating one more function by then I will the function
is void and the function name is same as test.
Okay the function name is best and we also have a function in test.
So among these two functions only the return type is different here there than there was in PJ and here
there than diapers void everything the same.
Okay.
Same function name and the number of arguments of 0.
Okay.
We are not receiving any argument.
So one thing that I wondered earlier is we cannot differentiate between these two function with the
help of return type.
Okay.
You don't type with the help of return type.
We cannot differentiate between these two functions.
Okay.
If I will call from the mean test then the compiler will get confused which function to call this one
or this one.
Okay so there will be ambiguity.
The compiler will get confused among these two functions.
So these two functions cannot be created in one file.
Okay.
We cannot differentiate with the help of return type.
If you want to differentiate if you want to differentiate then with the help of function name we can
differentiate with the help of function name the number of arguments and the type of argument but not
with the return type.
Okay now now we have already know where this function overloading.
Now let's see what is operator overloading.
So if you remember we have discussed about operator overloading in detail in section only.
Okay.
So let's take the example of plus operator.
Okay so let's take the example of plus operator.
So when I write two plus three output will be five.
So here plus adds to integer.
When I write three point five plus let's say one point five.
And I would probably be five point zero.
So plus operator add to the doubles here if I write correctly a plus character B then word plus operator
will lower it will convert these two values in goods ask I value so this will be 97 and this will be
98.
So plus add two characters here.
So we have only one operator which is the plus operator and plus operators behaving differently in different
situation.
Okay.
If you remember the complex number class we have overloaded the plus operator for complex number class.
Then if I write seven plus you do then here the plus operator is working differently.
Okay.
So we are deciding at the compile time only that how the operator will work.
Okay.
So with the help of operator overloading we can achieve compile time polymorphism.
Okay.
So I hope these two are very clear how we are achieving compile them but in my wisdom we are deciding
at the compile them only that what a set of code will do.
Okay.
Now there is a third way.
There is a territory also to achieve the compile type polymorphism and it is called Method overwriting
or we can save function overwriting.
Okay.
Matador function overwriting.
Now let us try to understand what is the meaning of function overwriting.
Okay.
Now okay so where does functional writing so let's say I have two classes Class A and Class B B in that
it's class A.
And so is the parent class and being taught based class and b d child that I have to class.
Okay now suppose Class E has a brain function
and we have also created a brain function inside Class B.
We also use brain function of its own.
So what it will have.
So what will happen this brain function will override this brain function.
Okay now let us understand with the help of code.
Okay
so I have this class so I have a class with can and I have a class got so inside the class we call I
have a brain function and inside the car class so called class.
First of all it in that it's from the class and it is and it is also having a brain function.
Okay so what will happen this brain function will override this print function.
Now what I want to convey here is let's say we are creating objects so I am creating object of the class
and let's see I am also creating the object of kind of class.
Okay so what do you with the order of the constructor call.
So we have already discussed about the order of constructor called but let's say let's revise it again.
Let's recap.
So in this case what will happen only DV Could a class constructor will be called and in this case first
the vehicle's less constructor will be called and then the of class constructor will be called.
Okay so what is the structure of working class.
So this is some political class and inside and it and it has print function so this is called class
and it has the class.
It has print function and the Car class also has brain function of its own.
Okay so now if I run this file what will happen first of all the class constructor will be called and
in this case first class and then called class constructor but I have not created any constructor so
we will not be able to see the messages.
Okay.
Now what will happen if I call the print function.
So if I write we don't print and if I write see dark print then what do we think what will happen.
So we already know we don't print it will call the print function of the vehicle class so the output
will be vehicle.
And if I call you Lord print what will happen it will call the print function of the God class.
Okay so the output will be God okay.
So we are deciding at the compile time which function to be called.
Okay.
I am repeating myself.
We are deciding at the compile time or leaders which function will be called.
If we have multiple options available to us.
So in this case I have multiple options available to us.
I have two print functions so I am deciding at the compile them only that this print function would
be called.
Now how I am able to decided to compile them because we can see here this week it belongs to vehicle
object.
This is a vehicle object.
So when I write we document it will call the print function of the vehicle class.
Now here I am writing C document.
Now C is an object of God class so it will call the print function of the God class.
So that's why this print function will be called.
So with the help of type of object we can decide which function will be called.
Okay now let's win this fight.
Okay so first of all let's write we don't print and see Dortmund
no alerts on this file
so the output this vehicle and God.
We don't print and see Dortmund.
Now if the car doesn't have any print function then we will call.
Okay.
So what I want to say here is if I do not have let's see the car plus do not have been functional for
John so we know what will happen.
So if the car doesn't have brain function of its own then we will call the print function of the vehicle
class.
Okay.
And if because class doesn't have brain function also then it will throw it in.
Okay so this land throws at it.
If the vehicle doesn't have print function.
Okay now let's take one more example.
Okay.
Now here we are creating the objects statically.
Now what will happen if I create the object manually.
So we could start we let's say V1 because V has really been created.
Now this is new we call okay.
So what will happen now.
So what is happening here is that this line what will happen.
So this is when and this even points to a vehicle object.
So this is a vehicle object and this is a vehicle point that even is a vehicle pointer size will be
eight bytes okay.
Now what if I create another object let's say I am creating what if I will right.
Because start we do.
Okay.
So what will happen.
So a view to point to a vehicle point that will be created.
And it will point to a random garbage location.
Okay.
And if we want to initialize if we want to initialize this vehicle pointer Then we can pointed towards
any vehicle.
Okay.
What I want to say here is let's say I am creating one motor vehicle vehicle pointer now this vehicle
pointer is not pointing towards a vehicle it is pointing towards item then garbage location.
And if we want to initialize this point that we can point it towards any vehicle for example we can
write we do is we do is containing the address of we basically we do is pointing towards will.
Okay.
So what will happen when I write we do is so when I write we do is pointing towards we.
So we do we do is the pointer and we Wednesday actually call object.
So we do is pointing towards even okay.
We can also.
So what we can also do here is so let's come and get out so we do use a vehicle pointer but we can also
do something like this.
We do know points toward the car glass object.
OK.
We do this pointing towards the car glass object.
So this is my God a glass object.
See the name is C. And this is we call and this is the brain function.
This is the brain function.
OK.
So what I did here is we do is now pointing towards the car glass object.
OK.
So this now this will be the scenario.
OK.
So if it is pointing towards the car glass object.
So what we do is a basic glass pointer.
So this is a basic glass pointer and see this is the right glass object.
OK.
So they raised the glass object.
So what I want to say here is a basic glass point that can point to words are derived does object but
divisive as that is not true.
Okay.
So when I write we do is pointing toward C..
So what is the meaning.
So basically just pointer can point to it.
Child the glass object but the vice versa is not allowed.
Okay now I'm explaining the whole code again.
Okay so what is the scenario.
Okay so we call we first of all this line we call we.
So what is the scenario.
Let's say this is we call view object.
This has brain function and let's say it addresses 300.
Okay.
Now Ghazi so it inherits from the working class so this is C object.
This is V.
This is the brain function and this is the brain function.
Okay.
And let's say that this is 400 view dot paint.
Pseudo argument then I'm creating and then I'm creating the object dynamically.
Okay so this is this is V1 and this is my dynamic object.
This is what we call object.
Okay.
This we call object has print function and let's say it said this is hundred.
So we even condensed hundred.
And it is pointing.
Okay.
It is pointing towards this dynamic object.
Then I created an added dynamic object so it is not a dynamic object.
It's a point that only okay because last pointer.
So let's say the name is we do.
And then v2 is pointing towards that derived class object.
So basically it said this is for it.
So it will contain four rendered.
And this is the scenario.
Okay.
So this is the complete scenario.
Now what will happen if I try and if I try to write this code V2 arrow print.
Okay so what will be the output.
So when I write V to Arrow print.
So we have two options either this print function will be called or this print function will be called.
Now what will happen.
So if I will write we want not print then it is very simple.
So even out of print then it is very simple We only have one option that is this print function.
Okay so this print function will get called but here we do at open.
We have two options.
This print and this print.
And why does the scenario.
Because a basic last point that is pointing towards are derived class object.
So which print function will be called.
Okay.
So what do you think.
Which print function will be called.
So one thing that is important here whenever we do something like this we do is M percent of C..
So what happened is whenever we do something like this.
This means so with the help of we do we can only access those properties that are present in the base
class.
Okay.
Although we do as a base class point and it is pointing towards a derived class object but with the
help of we do we can only access those properties that are present in the base class.
So that means this print function will be called.
Okay so this print function will be called Always remember this.
Okay make it a point a base class pointer whenever a base class point is pointing towards or drive class
object.
And if you are using we do it to excess something then we can access only those properties that are
present in the base class.
Okay.
Now listen this file.
Okay so let's come on out this one and this one so we what we are doing here is I am calling the print
function truly do Okay so I am writing here a note we can only access basic class properties properties
that are present in the base class.
Okay
so that's way the output of this line will be this print function will get called output we biblical.
Okay let's turn our file okay so this is trying I said it.
Okay so the mistake is spelling mistake
and similarly we did a spelling mistake okay.
So now disconnect
so the output is we call okay so we can only access only that we can access only those properties are
not present in the base class.
Okay.
Now here it is the example of a compiled and polymorphism.
Now why it is an example of compile time polymorphism so basically which function will be called We
are taking that decision at the compile time only.
Okay.
So I am repeating myself.
So when we write we do arrow print.
So this is an example of compile time wildfire wisdom.
Now why it is an example of compile time polymorphism because we are deciding at the compile time which
function will be called.
So we have two choices for the print function first in the Guard class print function and the second
was that we could last print function.
So we do first of all I don't care to whom we do is pointing.
So we do is pointing towards the car glass the car glass derived basically derived object but I don't
care.
Okay so I don't care to only do it pointing since we do is a type of vehicle.
So I will call only vehicle print function blandly.
Okay.
I will call you to print function vehicle print function blindly so we are deciding at the compile time
only which function will be called.
That's right.
This is an example of compile time polymorphism.
Okay.
Thank you.