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

Operator Overloading

It is a type of polymorphism in which an operator is overloaded to give user-defined meaning to it

Overloaded operator is used to perform operation on user-defined data type

For example '+' operator can be overloaded to perform addition on various data types, like for Integer, String(concatenation) etc

Roles of an Object

Objects of user-defined types can play three different roles in a function

  • Host object
  • Parameter object
  • Returned object

Understanding objects in each role helps the process of writing overloading operators

1. Host object

It is the one that is pointed to by the pointer

It must be constant if the function is not supposed to change it; it must be non-constant otherwise

2. Parameter object

It must be passed to the member function in three ways:

  • pass-by-value
  • pass-by-reference
  • pass-by-pointer

3. Returned object

Functions may return an object in three ways:

  • pass-by-value
  • pass-by-reference
  • pass-by-pointer

Must use return-by-value if the returned object is created in the body of the function

Must use return-by-reference or by pointer if the returned object is passed as the parameter to the function

Overloading Principles

Operator overloading is the definition of two or more operations using the same operator

  • 14 + 20
  • 14.21 + 20.45

In the following example, the compiler changes the first operand to 14.0 to do the operation

Categories of Operators

  • non-overloadable
  • not recommended for overloading
  • Overloadable

Rules of Overloading

Precedence - No change to the precedence of the operator

Associativity - The operator keeps its associativity

Commutativity - No change to commutativity of the operator

Arity - Unary & binary operators are kept

No new operators - No invention of operators

No combination - No new operators by combining 2 or more operators

End of Lecture 5

Computer Programming

That's it!

Queries about this Lesson, please send them to: jmwaura@jkuat.ac.ke

*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