Consider the following simple program as an example of runtime polymorphism. The main thing to note about the program is that the derived class’s function is called using a base class pointer.
The idea is that virtual functions are called according to the type of the object instance pointed to or referenced, not according to the type of the pointer or reference.
In other words, virtual functions are resolved late, at runtime.
What is the use?
Virtual functions allow us to create a list of base class pointers and call methods of any of the derived classes without even knowing kind of derived class object.
For example: Consider an employee management software for an organization.
Let the code has a simple base class Employee , the class contains virtual functions like raiseSalary(), transfer(), promote(), etc. Different types of employees like Manager, Engineer, etc. may have their own implementations of the virtual functions present in base class Employee.
In our complete software, we just need to pass a list of employees everywhere and call appropriate functions without even knowing the type of employee. For example, we can easily raise the salary of all employees by iterating through the list of employees. Every type of employee may have its own logic in its class, but we don’t need to worry about them because if raiseSalary() is present for a specific employee type, only that function would be called.
Like globalRaiseSalary(), there can be many other operations that can be performed on a list of employees without even knowing the type of the object instance.
Virtual functions are so useful that later languages like Java keep all methods as virtual by default.
How does the compiler perform runtime resolution?
The compiler maintains two things to serve this purpose:
- vtable: A table of function pointers, maintained per class.
- vptr: A pointer to vtable, maintained per object instance (see this for an example).
Compiler adds additional code at two places to maintain and use vptr.
1) Code in every constructor. This code sets the vptr of the object being created. This code sets vptr to point to the vtable of the class.
2) Code with polymorphic function call (e.g. bp->show() in above code). Wherever a polymorphic call is made, the compiler inserts code to first look for vptr using base class pointer or reference (In the above example, since pointed or referred object is of derived type, vptr of derived class is accessed). Once vptr is fetched, vtable of derived class can be accessed. Using vtable, address of derived derived class function show() is accessed and called.
Is this a standard way for implementation of run-time polymorphism in C++?
The C++ standards do not mandate exactly how runtime polymorphism must be implemented, but compilers generally use minor variations on the same basic model.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Runtime Polymorphism in various types of Inheritance in C++
- Dynamic Method Dispatch or Runtime Polymorphism in Java
- Can virtual functions be private in C++?
- Can virtual functions be inlined?
- Can static functions be virtual in C++?
- Virtual functions in derived classes
- Pure Virtual Functions and Abstract Classes in C++
- Array in Python | Set 1 (Introduction and Functions)
- Forward List in C++ | Set 1 (Introduction and Important Functions)
- Difference between Virtual function and Pure virtual function in C++
- What happens when a virtual function is called inside a non-virtual function in C++
- Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
- Polymorphism in C++
- Mathematical Functions in Python | Set 1 (Numeric Functions)
- Difference between Inheritance and Polymorphism
- Virtual Function in C++
- Virtual Destructor
- Virtual base class in C++
- Pure virtual destructor in C++
- Advanced C++ | Virtual Constructor
Improved By : brain56