What is Polymorphism in C++?
Understand polymorphism in C++ with clear examples of overloading, templates, and virtual functions, plus the difference between static and dynamic dispatch.
Expected Interview Answer
Polymorphism in C++ is the ability of a single interface or name to take many forms, so the same function call or operator behaves differently depending on the type it operates on.
C++ supports compile-time (static) polymorphism through function overloading, operator overloading, and templates, and runtime (dynamic) polymorphism through virtual functions accessed via base-class pointers or references. Static polymorphism is resolved by the compiler, while dynamic polymorphism is resolved at run time using the vtable. Together they let you write general code that adapts to many concrete types.
- Write generic code that works across many related types
- Extend systems with new types without changing existing callers
- Improves code reuse and maintainability
- Supports clean interface-driven design
- Enables both zero-cost compile-time and flexible run-time behavior
AI Mentor Explanation
The word 'play' means something different for each role: a batter plays a stroke, a bowler plays a delivery, a fielder plays a catch. One instruction, many forms of execution depending on the player's type — the same idea as one function name behaving differently across classes.
Step-by-Step Explanation
Step 1
Identify the shared interface
Define a common base class or overloaded name that many types will implement.
Step 2
Choose static or dynamic
Use overloading and templates for compile-time polymorphism, or virtual functions for run-time polymorphism.
Step 3
Implement per-type behavior
Each derived class or overload supplies its own version of the operation.
Step 4
Call through the common interface
Invoke the shared name so the appropriate implementation is selected by type.
Step 5
Rely on resolution
Let the compiler resolve static calls and the vtable resolve virtual calls at run time.
What Interviewer Expects
- Distinguishing compile-time from run-time polymorphism
- Naming mechanisms: overloading, templates, virtual functions
- Explaining how virtual dispatch works
- A concrete example of each kind
- Understanding the benefits for extensibility
Common Mistakes
- Treating polymorphism as only virtual functions and ignoring overloading and templates
- Confusing overloading (static) with overriding (dynamic)
- Forgetting that dynamic polymorphism needs pointers or references, not values
- Not mentioning the vtable when asked how run-time dispatch works
- Assuming templates incur runtime cost like virtual calls
Best Answer (HR Friendly)
“Polymorphism means one command can produce many different, appropriate behaviors depending on what it is acting on. In C++ it lets the same function name or operator do the right thing for many related types, which keeps code flexible and reusable.”
Code Example
#include <iostream>
// Compile-time polymorphism: function overloading
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
// Run-time polymorphism: virtual functions
class Animal {
public:
virtual void speak() const { std::cout << "...\n"; }
virtual ~Animal() = default;
};
class Dog : public Animal {
public:
void speak() const override { std::cout << "Woof\n"; }
};
class Cat : public Animal {
public:
void speak() const override { std::cout << "Meow\n"; }
};
void describe(const Animal& a) { a.speak(); } // resolves at run time
int main() {
std::cout << add(2, 3) << '\n'; // int overload
std::cout << add(2.5, 3.5) << '\n'; // double overload
Dog d; Cat c;
describe(d);
describe(c);
}Follow-up Questions
- What is the difference between compile-time and run-time polymorphism?
- How do templates provide static polymorphism?
- What is the difference between overloading and overriding?
- Why does dynamic polymorphism require pointers or references?
- What is the performance tradeoff between virtual calls and templates?
MCQ Practice
1. Which of these provides run-time polymorphism in C++?
Virtual functions are resolved at run time via the vtable; the other three are resolved at compile time.
2. Dynamic polymorphism in C++ requires calling through what?
Only pointers and references preserve the dynamic type; passing by value slices the object and loses overrides.
3. Function overloading is an example of which polymorphism?
Overload resolution happens at compile time, making it a form of static (compile-time) polymorphism.
Flash Cards
What is polymorphism? — The ability of one interface or name to take many forms, behaving differently depending on the type it acts on.
Two kinds of polymorphism in C++? — Compile-time (overloading, operator overloading, templates) and run-time (virtual functions).
How is run-time polymorphism achieved? — Through virtual functions called via a base-class pointer or reference, dispatched by the vtable.
Why can't dynamic dispatch work by value? — Passing by value slices the object to its base part, discarding the derived overrides.