Object-Oriented Programming with C++: Understanding Memory, Polymorphism, and the Virtual Function Table
Published: October 2026 • Author: Sarthak Pawar
C++ is one of the most powerful and demanding programming languages in existence. It gives you direct control over memory that higher-level languages like Python and Java abstract away. This power makes C++ the language of choice for game engines, operating systems, database engines, embedded systems, and high-frequency trading systems. But that power comes with responsibility—you must understand exactly what happens in memory when you create objects, call functions, and manage inheritance hierarchies.
The Object Memory Model — What Actually Happens When You Create an Object
When you declare Circle c(5.0);, the compiler allocates memory on the stack for the object. The size of this memory block equals the sum of all non-static data member sizes, plus alignment padding, plus (if the class has any virtual functions) an additional hidden 8-byte pointer called the vptr (virtual pointer). The constructor is then called to initialize the data members.
When you write Circle* c = new Circle(5.0);, the memory is allocated on the heap instead. The new operator calls operator new (which calls malloc internally) to reserve memory, then calls the constructor. Crucially, heap-allocated objects must be manually freed with delete c;—otherwise you get a memory leak. In modern C++, you should use smart pointers (std::unique_ptr, std::shared_ptr) to handle this automatically, but understanding raw pointers is required for the SY syllabus.
The Virtual Function Table (vtable) — How Runtime Polymorphism Works
When you declare a class with virtual functions, the compiler creates a vtable (virtual function table) — a static array of function pointers, one per virtual function, specific to each class in the hierarchy. Every object of a class with virtual functions contains a hidden vptr (virtual pointer) as its first member, pointing to its class's vtable.
When you call a virtual function through a base class pointer, here is what happens at runtime: (1) The CPU dereferences the object pointer to read the vptr, (2) The CPU uses the vptr to find the vtable for the actual (dynamic) type of the object, (3) The CPU indexes into the vtable to find the correct function pointer, (4) The function is called through that pointer. This is "late binding" or "dynamic dispatch" — the function to call is resolved at runtime, not compile time.
class Animal { public: virtual void speak() { std::cout << "..." << std::endl; } virtual ~Animal() {} // ALWAYS virtual destructor in base class }; class Dog : public Animal { public: void speak() override { std::cout << "Woof!" << std::endl; } }; class Cat : public Animal { public: void speak() override { std::cout << "Meow!" << std::endl; } }; // This is runtime polymorphism in action: Animal* animals[] = {new Dog(), new Cat(), new Dog()}; for (Animal* a : animals) { a->speak(); // Calls Dog::speak or Cat::speak via vtable at runtime delete a; // Virtual destructor ensures correct cleanup }
Why You Must Always Write a Virtual Destructor in Base Classes
This is one of the most common bugs in C++ code and a classic interview question. Consider the code: Animal* a = new Dog(); delete a;. If the destructor in Animal is NOT virtual, the compiler will call Animal::~Animal() (the base class destructor) and NOT Dog::~Dog(). If Dog's destructor frees resources (like memory, file handles, or network connections), those resources will never be freed — a memory leak or resource leak. Making the base class destructor virtual ensures the correct derived class destructor is called through the vtable mechanism.
The Rule of Three (and Five) — Copy Constructors and Assignment Operators
If your class manages a resource (raw pointer, file handle, network socket), you must explicitly define: (1) Destructor, (2) Copy Constructor, (3) Copy Assignment Operator. This is called the "Rule of Three" in C++. If you don't, the compiler generates default versions that perform a shallow copy — copying the pointer value but not the pointed-to data. When two objects then try to delete the same pointer, you get undefined behavior (usually a crash). In modern C++11 and later, the Rule extends to Five, adding: (4) Move Constructor, (5) Move Assignment Operator. For SY exams, mastering the Rule of Three is sufficient.
🔗 Related Study Resources
- SY Semester 3 Notes Library — OOP with C++ Unit-wise Answer Keys
- DSA Semester 3 Complete Roadmap — Apply C++ pointers to linked lists and trees
- SPPU Autonomous ETE Preparation Strategy — Exam writing tips for code questions