click the Bell icon to get latest videos
from Ekeeda, Hello Friends in this video
we will study what is virtual function
and Polymorphism Polymorphism is an
ability to take more than one form it
means suppose if I am talking about the
plus operator then plus operator can be
used as an addition operator also and it
can be used as concatenation operator
also so when I use my plus operator in
the case of numbers I called it as
addition operator whereas when I use my
plus operator with the help of string
then I called it as Concatenation
Operator so it means my plus operator
has more than one form so in this video
we will discuss how actually the
polymorphism is implemented with the
help of programs. polymorphism comes
under a two type that is there are two
types of polymorphism number one is
called as compile time polymorphism and
the two is called as run time
polymorphism in the compiled name
polymorphism there are two types
function overloading and operator
overloading what is function overloading
that we have already studied when we use
a function more than once in a program
with the same name but with the
different number of parameters so
basically my function name will remain
the same but your signature will be a
different at every place then I called
it as function overloading because it in
this case your compiler invokes the
current function by matching the
parameters or by matching the types of
parameter you are using then I called it
as function overloading and this
information is known to the compiler at
the compile time at the compile time
compiler decides which function to
invoke hence it is called as compile
time polymorphism then there is one more
type of overloading that is operator
overloading operator overloading when I
use my operators with the user-defined
data type in this same way I used with
the built-in data type then I call it as
operator overloading this concept also
we have already discussed in the
previous video so these two types comes
under the category of compile time
polymorphism now what is runtime
polymorphism there
comes a situation where your function
name and parameters both are same then I
called it as Tran time polymorphism
because in this case compiler get
confused which function to call because
your function name and parameter I mean
your function name and signature
everything will be same at the every
place so to so to implement this concept
I use a feature called as virtual
function so let us have a look on to a
virtual function now see this example in
this case I have created a two class
that is Class A and Class B if you see
here Class A has a function whose name
is void Show and Class B again has a
function whose name is void show now in
both the cases the prototype is same and
the function name is also same so in
this case your compiler will get
confused and by default it will keep on
calling your function which is there
inside the base class but I don't want
this I want the functions which is there
in the derived class also to get called
so for this I will use a concept called
as virtual function now to study a
virtual function I have an example we
will say that example and then we will
understand how to use a virtual function
in this example I have created a class
base in this class base I have created
two functions that is void display at
void show as of now you just forget
about what is virtual you concentrate or
only this two function that is why
display and void show in the void
displayer displaying display base and in
the void show you are displaying show
based then I created one more class that
is class derived and this class derive
is a clear derived class of your class
based in this again there are two
functions display and show in this here
displaying display derive and in this
year displaying show derive now in this
case my prototype of this plane show is
same in your class base also in your
class derive also now in this case by
default only the functions which are
they are in the base class that will get
called it means you won't get answer of
the function which are there in the
derived class so to get the answer of
those functions also what I will do I
will create objects of both the classes
that is Class B and Class D and then you
will create a pointer to a base class so
basically what I do
here I created object B then I created
object D and then I created a pointer
and this pointer is a pointer to class
base now when your pointer base is
pointing to a base class it means what
you are calling the functions which are
there in the base class and when your
pointer is pointing to a derived class
then it means what you are calling to a
functions which are there in the derived
class but by default you will get an
answer of only a base class so to get
the answer of derived class what I will
do I will use a keyword called as
virtual in place of base class I mean
wherever there are the functions whose
name is same as a function in the
derived class I will make those function
as a virtual function in the base class
so why not make them as a virtual what
will happen when my base pointer is
pointing to a base you will get an
answer of the base class and when your
base pointing point is pointing to a
derive you will get answer of derived
class so that is the use of virtual
function if you don't make it as virtual
then by default you will keep on getting
the answer of base class so hence it is
necessary to use the virtual keyword
whenever I want the answer of base class
also and I need the answer of derived
class so this is the example of what
virtual function what we have studied
here there are two types of polymorphism
compiled and polymorphism and runtime
polymorphism now this question is very
important as per your example because
this question comes either for for max
or for 3 max if it comes for for max
then explanation along with the program
is needed if it comes for 3 marks then
explanation along with the example is
needed that is you have to show what is
compile time polymorphism and what is
runtime polymorphism both now there are
some rules for the virtual function
number one
our children must be a member of some
class that is if you are using a virtual
function then obviously it should be a
member of some class they cannot be a
static member your virtual function
cannot be a static member number three
they are accessed by using object
pointer so if you will remember in the
example what I did I created a pointer
to a base class without creating a
pointer you cannot access your virtual
function so that's why it is necessary
to create a pointer of a base class next
a virtual function can be a friend of
another class yes you can create a
virtual function and that virtual
function can be a friend of some other
class a virtual function in base class
must be defined even though it is not
used it means what so in the previous
example I had taken two functions that
is display and so even though you are
not making the use of display and show
you have to make it as virtual by to
make it as virtual to get the answer of
the right class if you will make it as
virtual you won't be getting a sort of
derived class so that's why it is
necessary to make it as virtual even
though if it no if it is not in the use
the prototype of the base class version
and the derived class version should be
same obviously if the prototype is
different then I won't consider it as a
runtime polymorphism then it will go
inside the category of what function
overloading so that's why your prototype
should be same in the case of base class
and in the derived class next is we
cannot have a virtual constructor but
yes we can have a virtual destructor
next a base pointer can point to any
type of derive object but reverse is not
to your base class can point to any type
of derive object but the reverse is not
true it means you cannot create a
pointer to a derived class and that
pointer cannot point to any base class
so it means while creating a pointer you
have to be very sure that you are
creating the pointer of only base class
when base pointer points to a derived
class incrementation and decrement ation
is only relative to its base type actual
function defined in the base class need
not be redefined in the derived class so
it means what the functions which are
there in the derived class we don't make
it make them as a virtual only the
functions of the base class are treated
as a virtual function so this was all
about the virtual function and the
runtime polymorphism thanks for watching
this video in the next video we will
study what is files for the latest video
please subscribe the channel
TQ
