Lecture 5: Polymorphism

Computer Programming

J Mwaura

Polymorphism

Polymorphism means having many forms

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

Polymorphism gives the ability to assign a different meaning, or purpose, to an entity according to its context

  • Example; A young man at the same time can be a father, a husband, a student

Condition for Polymorphism

Polymorphic variables

  • Pointers
  • References

Exchangeable objects

Virtual functions

Analogy

A socket and plug-compatible devices

  • A socket accepts plug-compatible objects. In C++, a pointer or a reference can play this role
  • Devices, the plug-compatible objects. An object in an inheritance hierarchy plays this role
  • Power, for different devices that perform different tasks. This is done in C++ using virtual functions, modified by the keyword virtual

Binding

A function is split into two entities;

  • function call
  • function definition

Binding - refers to the process to be used for converting functions & variables into machine language addresses

Binding for functions, is associating the function call with the right function definition by the compiler

The binding is done either at compile time or at runtime

In case of one single function definition, there is no confusion. Whenever a function is called, the associated definition is executed

Binding

However, in inheritance its likely to have two functions with the same signature (overriding functions)

This means that the function has only one form of call but may have more than one definition

There are 2 cases of binding

  • Static or early
  • Dynamic or late

Static Binding

Happens at compile time (operations performed by a compiler)

Function definition & function call are linked during compile time

All information needed to call a function is available at the compile time

Its achieved using the normal function calls, function overloading, & operator overloading

Faster execution of a program

Dynamic Binding

Happens at the run time or execution time (program lifecycle phase)

Function calls are not resolved until runtime

All information needed for a function call cannot de determined at compile time

Its achieved using the virtual functions

Slower execution of a program

Flexible - since a single function can handle different type of objects at runtime

Types of Polymorphism

In C++ we have two types of polymorphism

  • Compile time Polymorphism - also known as static (or early) binding
  • Runtime Polymorphism - also known as dynamic (or late) binding

Compile Time Polymorphism

Function overloading & Operator overloading are perfect examples

Overloading is a polymorphism where several functions have the same name but with different number of parameters or the type of the parameters

Based on number of parameters passed during function call, compile determines which function to call

The function call is determined during compile time, thus called compile time polymorphism

Compile Time Polymorphism

Run Time Polymorphism

Overriding is a polymorphism where several functions have the same name, number of parameters & the type of the parameters

Function overriding - When child class declares a method, which is already present in the parent class; - child class overrides the parent class

The function call is determined at runtime, thus called run time polymorphism

Run Time Polymorphism

What's the difference?

Compile Time Polymorphism Run Time Polymorphism
The function to be invoked is known at the compile time The function to be invoked is known at the run time
Also known as overloading, static & early binding Also known as overriding, dynamic & late binding
Achieved by function & operator overloading Achieved by virtual functions & pointers
Provides fast execution as it is known at the compile time Provides slow execution as it is known at the run time
Less flexible as mainly all the things execute at the compile time More flexible as all the things execute at the run time

Concrete Classes

The classes we have designed so far are called concrete classes

Classes that can be used to instantiate objects

Abstract Classes

An abstract class is a class with at least one pure virtual function

Abstract class defines an interface

An abstract class cannot be instantiated because there is no definition for the pure virtual member functions

A pure virtual function is specified by placing = 0 in its declaration

The purpose of an abstract class is to provide an appropriate base class from which other classes can inherit

e.g. virtual double getArea (0) = 0;

Abstract Classes

End of Lecture 5

Computer Programming

That's it!

Queries about this Lesson, please send them to: jmwaura.uni@gmail.com

*References*

  • C++ Programming - An Object-Oriented Approach, 2019 Behrouz, Richard et.al
  • Accelerated C++ - Practical Programming by Example, 2000 Andrew, Barbara et.al
Courtesy of
Computer Programming