Inheritance/Polymorphism  «Prev  Next»
Lesson 3 Virtual member functions and pure polymorphism
Objective Virtual member functions and how they are used.

C++ Pure Polymorphism

Virtual Member Functions and Polymorphism in C++
In C++, "virtual member functions" enable polymorphism, which is a key concept in Object-Oriented Programming (OOP) that allows derived classes to override base class methods and achieve dynamic (runtime) dispatch.
How Virtual Functions Enable Polymorphism
  1. Virtual Keyword: Declaring a function as virtual in a base class allows derived classes to override it while ensuring that function calls are resolved at runtime (instead of compile-time).
  2. V-Table (Virtual Table) Mechanism:
    • When a class has at least one virtual function, the compiler creates a virtual table (vtable).
    • The vtable is a lookup table that stores function pointers to overridden methods in derived classes.
    • Each object of a class with virtual functions has a virtual pointer (vptr) pointing to the corresponding vtable.
  3. Runtime Polymorphism:
    • When a virtual function is called via a base class pointer or reference, the actual function executed depends on the runtime type of the object (determined by the vtable).
    • This allows derived class implementations to be invoked dynamically.

Example of Virtual Function Achieving Polymorphism
#include <iostream>
using namespace std;

class Base {
public:
    virtual void show() { // Virtual function
        cout << "Base class show() function"<< endl;
    }
};

class Derived : public Base {
public:
    void show() override { // Overriding the base class function
        cout << "Derived class show() function" << endl;
    }
};

int main() {
    Base* ptr;   // Base class pointer
    Derived obj; // Derived class object
    ptr = &obj;  // Base pointer pointing to Derived object
    
    ptr->show(); // Calls Derived class method due to virtual function
    
    return 0;
}
Explanation of Output
Derived class show() function
  • Without virtual functions, ptr->show() would have called Base::show() due to early binding (compile-time binding).
  • With virtual functions, it invokes Derived::show() due to late binding (runtime polymorphism).
Key Benefits of Virtual Functions in C++
  1. Encapsulation and Code Reusability: Allows writing more modular and maintainable code.
  2. Dynamic Behavior: Enables function calls to be resolved at runtime based on object type.
  3. Extensibility: New derived classes can override methods without modifying existing code.

Conclusion Virtual functions achieve "polymorphism" by deferring function resolution to runtime using the vtable mechanism, allowing derived class methods to override base class methods dynamically.

Virtual Member Functions

C++ supports virtual member functions. These are functions declared in the base class and redefined in a derived class. When a virtual member function is redefined, it is overridden as opposed to overloaded. We will look at the difference between overriding and overloading later in this module. For now, just think of the difference as semantic.
  1. Virtual member functions can be overridden, and
  2. nonvirtual member functions can be overloaded.

A class hierarchy that is defined by inheritance creates a related set of user-defined types, all of whose objects may be pointed at by a base class pointer. By accessing the virtual function through this pointer, C++ selects the appropriate function definition at runtime.
This is a form of polymorphism called pure polymorphism.
  • OOP Design Methodology Inheritance should be designed into software to maximize reuse and to allow a natural modeling of the problem domain.
    The key elements of the OOP design methodology are as follows:
    1. Decide on an appropriate set of types.
    2. Design in their relatedness, and use inheritance to share code.
    3. Use virtual functions to process related objects polymorphically.

Polymorphism

Polymorphism is the provision of a single interface to entities of different types. A polymorphic type is a type whose operations can also be applied to values of some other type or types. There are several fundamentally different kinds of polymorphism. If a function denotes different and potentially heterogeneous implementations depending on a limited range of individually specified types and combinations, it is called ad hoc polymorphism. Ad hoc polymorphism[1] is supported in many languages using function overloading.
Polymorphism allows you to take advantage of the commonality between related classes, while still giving each class the flexibility to implement specific behavior. Using polymorphism, it is possible to build very flexible and extensible systems. Polymorphism (literally, having multiple shapes) describes a set of objects of different classes with similar behavior.
[1]ad hoc polymorphism: In programming languages, ad hoc polymorphism is a kind of polymorphism in which polymorphic functions can be applied to arguments of different types. A polymorphic function can denote a number of distinct implementations depending on the type of arguments to which it is applied.

SEMrush Software