# C++ & OOP Internals: Virtual Tables (vptr/vtable), Constructors, Diamond Inheritance & STL
Exam: UGC NET Computer Science & Applications (Code 87) (UGC NET CS)
Subject: Unit 3: Programming in C, C++, Java, JavaScript & Assembly
Canonical URL: https://gateandnet.com/exams/ugc-net-cs/programming-languages/cpp-oop-virtual-functions-constructors-and-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 $\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 Container | Underlying Structure | Lookup | Insert / Erase | Notes |
| :--- | :--- | :---: | :---: | :--- |
| `std::vector` | Dynamic contiguous array | $\mathcal{O}(1)$ idx | Amortized $\mathcal{O}(1)$ end, $\mathcal{O}(n)$ mid | Best cache locality |
| `std::map` / `std::set` | **Red-Black Tree** (ordered) | $\mathcal{O}(\log n)$ | $\mathcal{O}(\log n)$ | Sorted iteration |
| `std::unordered_map` | Hash Table (chaining) | Avg $\mathcal{O}(1)$, Worst $\mathcal{O}(n)$ | Avg $\mathcal{O}(1)$ | Requires hash + `operator==` |
| `std::priority_queue` | Binary **Max-Heap** (by default!) | Top: $\mathcal{O}(1)$ | Push/Pop: $\mathcal{O}(\log n)$ | `make_heap` runs in $\Theta(n)$ |

## Solved Practice Questions & PYQs

### [UGC NET CS Dec 2024 | MCQ]
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?
**Correct Answer:** B
**Solution:** Without `virtual ~Base()`, static binding invokes only `~Base()` (officially undefined behavior in ISO C++ when deleting a derived object through a base pointer), leaking any resources owned by `Derived`!

### [UGC NET CS June 2024 | MCQ]
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`?
**Correct Answer:** B
**Solution:** Declaring `B1 : virtual public A` and `B2 : virtual public A` ensures the most-derived class `D` constructs a **single shared `A` base subobject**.

### [UGC NET CS Dec 2023 | MCQ]
Which C++ operators **CANNOT** be overloaded by a user-defined class?
**Correct Answer:** A
**Solution:** In C++, `::`, `.`, `.*`, `?:`, `sizeof`, `typeid`, and `alignof` **cannot be overloaded**, whereas `[]`, `()`, `->`, `new`, `delete`, and even the comma operator `,` *can* be overloaded!
