Object-oriented programming
C++
Dr. Irtiqa Amin
Assistant Professor
Learning
Outcomes
• Covering the basic concepts of
Virtual Function
Run-time Polymorphism
Virtual function in C++
A virtual function (also known as virtual methods) is a member function that is
declared within a base class and is redefined (overridden) by a derived class. When
you refer to a derived class object using a pointer or a reference to the base class, you
can call a virtual function for that object and execute the derived class's version of the
method.
Virtual functions ensure that the correct function is called for an object, regardless of
the type of reference (or pointer) used for the function call.
They are mainly used to achieve run-time polymorphism.
Functions are declared with a virtual keyword in a base class.
The resolving of a function call is done at runtime
Syntax
We can create a virtual function inside a class
using the virtual keyword
Example
Explanation
sound() is a virtual function in the Animal base class.
The derived classes Dog and Cat override the sound() function.
A base class pointer is used to call the function.
At runtime, C++ determines the actual object type (Dog or Cat) and
invokes the correct function using dynamic dispatch.
Pure Virtual function in C++
A virtual function is called a pure virtual function if it does not have any
implementation and is assigned = 0.
You cannot create objects of an abstract class.
Any class with at least one pure virtual function is called an abstract class.
Syntax
Difference between early and late binding
Feature Early Binding Late Binding
Static Binding / Compile- Dynamic Binding / Run-
Also Known As
time Binding time Binding
When Binding Occurs At compile time At runtime
Compiler resolves the Decision is made at
Function Call Resolution
function call runtime using vtable
Normal functions, Virtual functions (in base
Used With
overloaded functions class)
Slightly slower due to
Performance Faster (no overhead)
dynamic dispatch
More flexible; supports
Flexibility Less flexible
polymorphism
Function overloading, Virtual functions,
Example
operator overloading polymorphism
Code Example void display(); virtual void display();
Compile-time
Polymorphism Type Run-time polymorphism
polymorphism
Reference/object type is Actual object type at
Object Type Considered
used runtime is used
Syntax
Runtime Polymorphism
Definition Key Concepts
Run-time polymorphism is a feature Uses base class pointers/references to call
of C++ that allows a function to
derived class methods
behave differently based on the
Requires virtual functions
object that invokes it at run time. It
is achieved using inheritance and Binding happens at runtime, not compile
virtual functions.
time
Enables method overriding
Example
Why it works
The function sound() is virtual in the base class.
At runtime, the actual type of the object (Dog or Cat) is used
to resolve the method.
This is run-time polymorphism, also known as dynamic
dispatch.
Virtual Base Class
Purpose
Used in multiple inheritance to avoid duplication of base class members
when a class is inherited more than once via different paths.
Why it's used?
Without virtual, class D would have two copies of A, causing ambiguity.
Abstract Class
Definition
A class that contains at least one pure virtual function (= 0) is
called an abstract class. It cannot be instantiated.
Use?
Defines a base class interface that derived classes must implement.
Pointer to Object
Definition
You can create a pointer to an object of a class
and use it to access members.
this pointer
Definition
this is a pointer that points to the current object inside a member
function.
Use?
Used to refer to current object, especially when parameter names
shadow member names.
Pointer to Derived Class in C++
Definition
A pointer to a derived class is a pointer that holds the address of a derived
class object. It can access:
Only base class members if it's a base class pointer.
All members (including derived ones) if it's a derived class pointer.
Scenario Type of Pointer Accessible Members Notes
Base* ptr = new Base class Only base class members
Upcasting
Derived(); pointer (unless virtual)
Derived* ptr = new Derived class Both base and derived class Direct
Derived(); pointer members access
Derived* ptr = Risky if basePtr doesn't actually Down
Typecasting
(Derived*)basePtr; point to Derived casting
Example
Upcasting Example (Base pointer pointing to Derived)
Vs
Down-casting (Unsafe without checks)
Task to do!
A program to demonstrate the pure virtual functions.
You will try all the programs mentioned in this and the
previous PPT.
That’s all for now…