So our next up against polymorphism will chat functions and abstract classes, so this is most like
kind of a special topic we are covering today.
So let us deep dive into polymorphism answer.
So among one polymorphism concept you have already discussed in our previous section, which was basically
on all our learning, like we had different operators, right?
And we now are using these operators for a different purpose.
So like we had, let's celebrate that.
But we changed the meaning of the plus operator in our previous classes.
So that's an example of what a polymorphism which means polymorphism means.
Multiple forms like the same thing is achieved differently.
So we had a list operator and we were using that cluster operator for both adding numbers and also concatenating
some strings.
So that's an example for that polymorphism, which means like the same operator was used in different
forms.
Similarly, we do have one more concept in C++, which is called runtime polymorphism.
So for achieving runtime polymorphism, we usually use our feature in C++ GUI virtual function.
So let's discuss what is meant by individual function.
First ever virtual function, a simple function we just declared within a base class and redefined by
a class.
Suppose we have two classes Class A and Class B, so Class A is the bad and class, and Class B is actually
that derived class or inheriting the properties of that class and supports classes having a property
on a function card.
So that's a function.
And in class AA, it's having a meaning like we want to change the meaning while we are impacting the
same function to Class B.
OK.
So for that purpose, we use a concept like virtual function.
What we will do is like we will actually define our show function as it will actually function inside
the bat and class.
It will be like virtual show.
And this will gosse that we can change the definition of this show function being headed that same show
of function in under Class B.
So that's that symbol for it with a gel function.
And in order to make a function virtual, we use a key word like what?
OK?
And when actually the function definition is taken into consideration.
It's only during the runtime, usually in C++.
Most of the things most of the meanings of the function, most of that are like definitions, resolution
of variables.
Everything will take place at compile time.
But if we are using a virtual function, the meaning of that virtual function is derived or let that
runtime.
So that's the difference between a normal function and it will actually function.
So I have already discussed this topic, so we have polymorphism, which means processing the same thing
in different ways.
OK.
And the polymorphism can be divided into two.
One is compile time polymorphism, and it's having two separate divisions, as I discussed already function,
overloading and operating our learning, which we have already covered in our previous section.
Now we have one more type of polymorphism, which is called runtime polymorphism, which will actually
depend on the work functions concept.
So let's dive into the demo right now so that you will be able to understand how the gel functions work.
So I will explain this program to you.
So it's having two classes one classes, card bays, glass and another classes derived class, and you
can see a single inheritance happening out here like glass derived is actually inheriting the properties
of base class.
And we do have dual functions inside our base class one, this virtual white brain function and virtual
function.
And what happens when you execute brain function in base class?
It will print brain based class that will be the output.
And what happens when you bring the show function from the base class?
It will just print show based class.
OK.
And now we are deriving that and we are redefining that.
But if you look at here this white print, it's actually printing this derived class and show is also
redefined out yet and it will be printing the entire class.
OK.
It will be showing that at.
That will be the output of this function.
What is happening is like we are actually creating a wind up for our very own glass.
OK.
We are creating our point effort for their glass out here and we are actually printing the function,
OK, using the brain function of that child object.
OK.
So it will be like that object is what drives glass and it will be printing the value for the glass
so that the rind glass object is D. So it's equal to the entire brain, and this is equal to the brain
because this is a pointer of an object that we have already discussed.
So you will be at mass any.
You can access this drive glass and access that brain by using this index so it will be actually binding
to this glass and it will be executing this print.
And now we do have one more thing like show function, which is also like printing on that order here,
right?
It will be like printing show derived glass, but it's also inside the building of the glass.
But what happens when you execute this function?
So that's something we need to know, right?
So let me switch to desktop now you have to compile this fire.
So we have the syntax C++, the ritual FNC, dark CBB.
Now it's come by.
Now let's bring it by or out to get out of dodge.
Slash it out.
See if you know what is on here.
The drive glass is actually printing that using the print function out here, but many also here.
The show function is not showing that the right glass.
This should be the output of the show function, but it's just printing that show based glass.
That means it's actually printing the show function or here.