Polymorphism Internals: Compile-Time vs Runtime, Virtual Functions & VTables
Early binding vs late binding, operator/method overloading vs overriding, and pure virtual functions.
Mock tests for SSC, Banking, Railways, UPSC & state exams
Real exam pattern, negative marking and a worked solution for every question.
Polymorphism allows code to behave differently depending on runtime context or method signatures. It is the primary engine behind clean, extensible software architecture.
1. Compile-Time vs Runtime Polymorphism#
• Compile-Time Polymorphism (Static / Early Binding): The function call is linked to its executable code at compilation time. The compiler inspects the arguments and resolves the call statically. Examples: Method Overloading and Operator Overloading.
• Runtime Polymorphism (Dynamic / Late Binding): The actual implementation executed is resolved during program runtime based on the actual object type in memory. Example: Method Overriding.
2. Deep Comparison: Method Overloading vs Method Overriding#
This is the single most tested question in technical interviews (Question 13 & Tricky Question 3):
| Characteristic | Method Overloading | Method Overriding |
|---|---|---|
| Binding Time | Compile-Time (Static / Early Binding) | Runtime (Dynamic / Late Binding) |
| Scope | Occurs within the SAME class | Occurs between Superclass and Subclass |
| Method Signature | Same method name, but DIFFERENT parameter count, types, or order | EXACT SAME method name, return type, and parameter list |
| Return Type Rule | Return type CANNOT differentiate overloaded methods by itself | Return type must be identical (or covariant) |
| Private/Static Methods | Can overload private and static methods | CANNOT override private or static methods (static creates method hiding) |
Practise placement aptitude on the real test pattern
TCS NQT, Infosys, Wipro, Accenture and Cognizant style mocks with section-wise analysis.
3. Virtual Functions & Pure Virtual Functions#
• Virtual Function: A member function in a base class declared with the `virtual` keyword in C++. It tells the compiler to look up the function in the virtual method table (vtable) at runtime rather than binding it statically. In Java, all non-static, non-private, non-final methods are virtual by default!
• Pure Virtual Function: A virtual function with no implementation body in the base class (syntax: `virtual void render() = 0;`). A class containing at least one pure virtual function becomes an Abstract Class and cannot be instantiated.
| 1 | class Shape { |
| 2 | public: |
| 3 | // Pure Virtual Function (Abstract) |
| 4 | virtual void draw() = 0; |
| 5 | |
| 6 | // Regular Virtual Function |
| 7 | virtual double area() { return 0.0; } |
| 8 | }; |
| 9 | |
| 10 | class Circle : public Shape { |
| 11 | public: |
| 12 | void draw() override { |
| 13 | std::cout << "Drawing Circle\n"; |
| 14 | } |
| 15 | }; |
4. Operators That CANNOT Be Overloaded in C++#
Question 24 from the interview sheet: "Name the operators that cannot be overloaded in C++."
The 5 operators that cannot be overloaded are:
- 1. Scope Resolution Operator (::)
- 2. Ternary / Conditional Operator (? :)
- 3. Member Access or Dot Operator (.)
- 4. Pointer to Member Operator (.*)
- 5. sizeof operator (and typeid in modern C++)
C++ creator Bjarne Stroustrup explained that overloading operators like `.` or `::` would fundamentally alter C++ syntax parsing, leading to ambiguous expressions and breaking compiler memory lookup guarantees.
Test Your Concept Mastery
Verify your retention on: Polymorphism Internals: Compile-Time vs Runtime, Virtual Functions & VTables
Which operator CANNOT be overloaded in C++?
Ready for a full proctored assessment?
Take a free quick test on the real exam pattern, with a worked solution for every question.