0% found this document useful (0 votes)
5 views25 pages

Lecture 9 (Polymorphism)

Uploaded by

Arifur Rahaman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views25 pages

Lecture 9 (Polymorphism)

Uploaded by

Arifur Rahaman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Polymorphism in C++

Polymorphism
• The word polymorphism means having many
forms. Typically, polymorphism occurs when
there is a hierarchy of classes and they are
related by inheritance.
• C++ polymorphism means that a call to a
member function will cause a different
function to be executed depending on the
type of object that invokes the function
Virtual Function

• A virtual function is a function in a base class that is declared


using the keyword virtual.
• Defining a virtual function in a base class, with another
version in a derived class, signals to the compiler that we
don't want static linkage for this function.
• What we do want is the selection of the function to be called
at any given point in the program to be based on the kind of
object for which it is called.
• This sort of operation is referred to as dynamic linkage, or
late binding.
Virtual functions ensure that the correct function is called for an
object, regardless of the expression used to make the function call.
Explanation
• Runtime polymorphism is achieved only through a pointer (or reference) of base
class type. Also, a base class pointer can point to the objects of base class as well
as to the objects of derived class. In above code, base class pointer ‘bptr’
contains the address of object ‘d’ of derived class.
• Late binding(Runtime) is done in accordance with the content of pointer (i.e.
location pointed to by pointer) an Early binding(Compile time) is done according
to the type of pointer, since print() function is declared with virtual keyword so it
will be binded at run-time (output is print derived class as pointer is pointing to
object of derived class ) and show() is non-virtual so it will be binded during
compile time(output is show base class as pointer is of base type ).
• NOTE: If we have created virtual function in base class and it is being overrided
in derived class then we don’t need virtual keyword in derived class, functions
are automatically considered as virtual functions in derived class.

Ref: https://www.geeksforgeeks.org/virtual-function-cpp/
Example
Cont.
Cont.
The reason for the incorrect output is that the
call of the function area() is being set once by
the compiler as the version defined in the base
class. This is called static resolution of the
function call, or static linkage - the function call
is fixed before the program is executed. This is
also sometimes called early binding because the
area() function is set during the compilation of
the program.
Cont.
Cont.
• This time, the compiler looks at the contents of the
pointer instead of it's type.
• Hence, since addresses of objects of tri and rec classes
are stored in *shape the respective area() function is
called.
• As you can see, each of the child classes has a separate
implementation for the function area().
• This is how polymorphism is generally used. You have
different classes with a function of the same name, and
even the same parameters, but with different
implementations.
Virtual Function

• A virtual function is a function in a base class that is declared


using the keyword virtual.
• Defining in a base class a virtual function, with another
version in a derived class, signals to the compiler that we
don't want static linkage for this function.
• What we do want is the selection of the function to be called
at any given point in the program to be based on the kind of
object for which it is called.
• This sort of operation is referred to as dynamic linkage, or
late binding.
Pure Virtual Functions

• It's possible that you'd want to include a


virtual function in a base class so that it may
be redefined in a derived class to suit the
objects of that class, but that there is no
meaningful definition you could give for the
function in the base class.
Cont.

You might also like