∑GateAndNet
PlannerSign Up
INTERMEDIATEFrequently Tested 4 min read 4 views

C++ & OOP Internals: Virtual Tables (vptr/vtable), Constructors, Diamond Inheritance & STL

Compile-time vs runtime polymorphism, vptr/vtable dispatch, base-to-derived constructor vs reverse destructor order, virtual destructors, diamond virtual inheritance, and C++ STL time complexities.

1. Runtime Polymorphism: virtual, vtable, vptr & Object Slicing

  1. Without virtual (Static / Early Binding): Calling basePtr->show() resolves at compile time using the pointer’s static type (Base::show()), even if basePtr points to a Derived object!
  2. With virtual (Dynamic / Late Binding): Compiler adds a hidden pointer vptr (typically 8 bytes on 64-bit) to every polymorphic object, pointing to the class’s vtable of function pointers. Calling basePtr->show() dispatches Derived::show() at runtime in O(1)\mathcal{O}(1) indirect call!
  3. Why Base Destructors MUST Be virtual: If delete basePtr; is executed when ~Base() is non-virtual, only ~Base() runs and ~Derived() is skipped (leaking resources / undefined behavior)!
  4. Object Slicing Trap: Passing a Derived object by value void f(Base b) slices off the Derived subobject and copies only the Base part (so b.show() calls Base::show() even if virtual! Polymorphism requires pointers or references Base& / Base*).
cpp
#include <iostream>
struct Base {
    Base() { std::cout << "B+ "; }
    virtual void f() { std::cout << "Bf "; }
    virtual ~Base() { std::cout << "B- "; }
};
struct Derived : Base {
    Derived() { std::cout << "D+ "; }
    void f() override { std::cout << "Df "; }
    ~Derived() override { std::cout << "D- "; }
};
int main() {
    Base* p = new Derived();
    p->f();
    delete p; // Output: B+ D+ Df D- B-
}

2. Diamond Problem & C++ STL Container Complexities

In multiple inheritance D : public B1, public B2 where both B1, B2 inherit from A, D gets two ambiguous copies of A unless B1 and B2 inherit via virtual public A!

STL ContainerUnderlying StructureLookupInsert / EraseNotes
std::vectorDynamic contiguous arrayO(1)\mathcal{O}(1) idxAmortized O(1)\mathcal{O}(1) end, O(n)\mathcal{O}(n) midBest cache locality
std::map / std::setRed-Black Tree (ordered)O(log⁡n)\mathcal{O}(\log n)O(log⁡n)\mathcal{O}(\log n)Sorted iteration
std::unordered_mapHash Table (chaining)Avg O(1)\mathcal{O}(1), Worst O(n)\mathcal{O}(n)Avg O(1)\mathcal{O}(1)Requires hash + operator==
std::priority_queueBinary Max-Heap (by default!)Top: O(1)\mathcal{O}(1)Push/Pop: O(log⁡n)\mathcal{O}(\log n)make_heap runs in Θ(n)\Theta(n)
Key Formulas Summary
Constructor & Destructor Execution Order in Inheritance (Base -> Derived)
Construction: Base()→Member()→Derived(),Destruction: ∼Derived()→∼Member()→∼Base()\text{Construction: } \text{Base}() \to \text{Member}() \to \text{Derived}(), \qquad \text{Destruction: } \sim\text{Derived}() \to \sim\text{Member}() \to \sim\text{Base}()
Note: Member initializer lists execute in the order members are DECLARED in the class, NOT initializer-list order!
#C++#OOP#Virtual Functions#vtable#Polymorphism#STL#UGC NET CS#GATE

Practice Questions on This Chapter (3)

Q1UGC NET CS Dec 2024MCQMEDIUM
+2M • 0 Neg

In C++, class Derived publicly inherits from Base. Suppose Base* ptr = new Derived(); is deleted via delete ptr; when Base has a non-virtual destructor. What happens?

⚡ Click an option above to verify instantly
Q2UGC NET CS June 2024MCQEASY
+2M • 0 Neg

Which C++ mechanism resolves the Diamond Ambiguity Problem in multiple inheritance (D inherits from both B1 and B2, which both inherit from A) so D contains only one shared subobject of A?

⚡ Click an option above to verify instantly
Q3UGC NET CS Dec 2023MCQEASY
+2M • 0 Neg

Which C++ operators CANNOT be overloaded by a user-defined class?

⚡ Click an option above to verify instantly
Next Topic Java & JVM Architecture: String Pool, Integer Cache, Interfaces, Exceptions & Concurrency