OOP UE - Polymorphism
OOP UE - Polymorphism
• This vtable pointer or _vptr, is a hidden pointer added by the Compiler to the
base class. And this pointer is pointing to the virtual table of that particular class.
• Each object of a class with virtual functions transparently stores this _vptr.
because dPtr is a base pointer, it only points to the Base portion of d1. However, also note t
hat *__vptr is in the Base portion of the class, so dPtr has access to this pointer. Finally, not
e that dPtr->__vptr points to the D1 virtual table! Consequently, even though dPtr is of typ
e Base, it still has access to D1’s virtual table (through __vptr).
• what happens when we try to call dPtr->function1()?
int main()
{
D1 d1;
Base *dPtr = &d1;
dPtr->function1();
return 0;
}
• First, the program recognizes that function1() is a virtual function.
Second, the program uses dPtr->__vptr to get to D1’s virtual table.
Third, it looks up which version of function1() to call in D1’s virtual
table. This has been set to D1::function1(). Therefore, dPtr-
>function1() resolves to D1::function1()
return 0;
}
• Because there are only two virtual functions here, each virtual table
will have two entries (one for function1(), and one for function2()).
Remember that when these virtual tables are filled out, each entry is
filled out with the most-derived function an object of that class type
can call.
• The virtual table for Base objects is simple. An object of type Base can only
access the members of Base. Base has no access to D1 or D2 functions.
Consequently, the entry for function1 points to Base::function1(), and the
entry for function2 points to Base::function2().
• The virtual table for D2 is similar to D1, except the entry for function1
points to Base::function1(), and the entry for function2 points to
D2::function2().
What is Binding?
• Binding is a kind of mapping of a function call with the function’s
definition i.e. function’s address. For example,
When we make a function call like,
Obj.display();
• then before its execution, it gets bonded to display() function
definition i.e. function’s address, so that while code execution, correct
function should be called.
Static Binding Vs Dynamic Binding
• Static or Compile time or Early Binding:
By Default C++ Compiler do the early binding for all function calls i.e. while
linking when compiler sees a function call, then it binds that call with the
particular function’s address / definition.
• Dynamic or Run Time or Late Binding:
When we make a member function virtual then compiler performs run time
binding for that function i.e. any call to that virtual function will not be linked
to any function’s address during compile time. Actual function’s address to
this call will be calculated at run time. To resolve the actual function’s
address or definition at run time, C++ compiler adds some additional data
structure around virtual functions i.e.
• vTable
• vPointers
Overloading Vs Overriding
• Overloading
• Same scope but different signatures
• Early or static binding
• Overriding
• Same signatures but different scope
• Late or dynamic binding
References
• C++ How to Program By Deitel & Deitel
• The C++ Programming Language By Bjarne Stroustrup
• Object oriented programming using C++ by Tasleem Mustafa, Imran Saeed, Tariq
Mehmood, Ahsan Raza
• https://www.tutorialspoint.com/cplusplus
• https://www.learncpp.com/cpp-tutorial/125-the-virtual-table/
• https://thispointer.com/how-virtual-functions-works-internally-using-vtable-and-
vpointer/