0% found this document useful (0 votes)
16 views

CMP 2103 - Virtual Functions

Uploaded by

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

CMP 2103 - Virtual Functions

Uploaded by

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

CMP 2103

Polymorphism
Polymorphism in C++ refers to the ability of a function to behave differently based on the
actual type of the object it is operating on, at runtime.

In the context of C++, polymorphism is generally achieved through inheritance and the use
of virtual functions.

Polymorphism is achieved through the base class pointer (Base* basePtr) pointing to a
derived class object (new Derived()).
Virtual functions
- Polymorphism is facilitated by marking functions in the base class as virtual.
- When a function is declared virtual in a base class, the derived class can
override it, and the most-derived version of the function is called, even when
accessed via a base class pointer or reference.
- Marking a function as virtual means the call to that function will be resolved at
runtime based on the actual type of the object.
Dynamic dispatch
- Dynamic dispatch is a mechanism used in programming to select which
implementation of a polymorphic function (typically a virtual function) to call at
runtime, based on the actual type of the object.
- It is a key component of runtime polymorphism in object-oriented
programming languages like C++.
How Dynamic Dispatch Works
- A function declared with the virtual keyword in the base class allows derived classes to override it.
- Each class with one or more virtual functions has a vtable.
- The vtable is essentially an array of function pointers, where each entry points to the appropriate version of
a virtual function for that class.
- Each object of such a class contains a hidden pointer called a vptr that points to the vtable of that class.
- When a virtual function is called on an object through a pointer or reference to the base class, the vptr is
used to look up the correct function pointer in the vtable.
- The appropriate function pointer is then invoked, ensuring that the most-derived version of the function is
executed.
- In short, dynamic dispatch allows you to use polymorphism effectively, enabling base class pointers or
references to call the correct version of overridden functions in derived classes, ensuring that your code is
flexible and extensible.
Example of Polymorphism
What is not polymorphism
Summary
Static Binding (Early Binding)

- The last example uses early binding(static binding)


- Functions that are not virtual are statically bound.
- The function call is resolved at compile time, based on the declared type of the pointer or object.

Dynamic Binding (Late Binding):

- Functions that are marked as virtual are dynamically bound.


- The function call is resolved at runtime, based on the actual type of the object being pointed to.
- This is only possible when using pointers or references to base classes.

Virtual Destructors: Always use virtual destructors in base classes to ensure proper resource cleanup when
deleting derived objects through base class pointers.
Summary
To achieve runtime polymorphism, you need to:

1. Mark the functions that you want to override in derived classes as virtual in
the base class. Use the override keyword in derived classes for clarity and to
ensure correctness.
2. Access the functions through a pointer or reference to the base class to
achieve dynamic binding.
Pure virtual Function
- A pure virtual function in C++ is a function that is declared in a base class but is required to be overridden
by any derived class that wants to be instantiated.
- Pure virtual functions play a crucial role in making a class abstract, which means you cannot create an
instance of that class directly.
- Pure virtual functions are used to enforce a common interface in derived classes, and they are a key
element in designing abstract base classes for polymorphism.

Syntax:

virtual void functionName() = 0;

The = 0 is what makes the function a pure virtual function. It means that this function has no implementation in the
base class and must be implemented by any derived class.
Abstract class
- A class that contains at least one pure virtual function is called an abstract base class.
- Abstract base classes cannot be instantiated, but they can be used as base classes for other
classes. This is useful for defining an interface that derived classes must follow.
- The Derived class must override the display() function to provide an implementation. If it doesn’t,
Derived itself will also be considered an abstract class and cannot be instantiated.
Real World Use Case
Shape Interface:

Imagine you are creating a geometry library, and you want to represent different
shapes like Circle, Square, and Triangle. You can define a Shape abstract base
class with pure virtual functions such as area() and perimeter(). Each derived
class (Circle, Square, etc.) must implement area() and perimeter().

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy