Why polymorphism is needed in C++ - Performance Analysis
We want to understand how using polymorphism affects the time it takes for a program to run.
Specifically, does choosing polymorphism change how the program's work grows as input grows?
Analyze the time complexity of the following code snippet.
class Shape {
public:
virtual void draw() { /* base draw */ }
};
class Circle : public Shape {
public:
void draw() override { /* draw circle */ }
};
void renderShapes(std::vector<Shape*> &shapes) {
for (auto shape : shapes) {
shape->draw();
}
}
This code calls the draw function on many shapes using polymorphism to decide which draw runs.
- Primary operation: Loop over all shapes calling draw()
- How many times: Once per shape in the list
Each shape in the list causes one draw call, so work grows as the number of shapes grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 draw calls |
| 100 | 100 draw calls |
| 1000 | 1000 draw calls |
Pattern observation: The work grows directly with the number of shapes.
Time Complexity: O(n)
This means the time to run grows in a straight line as you add more shapes.
[X] Wrong: "Polymorphism makes the program slower in a way that changes how time grows with input size."
[OK] Correct: Polymorphism adds a small fixed cost per call but does not change the overall growth pattern, which depends on how many shapes you have.
Understanding how polymorphism affects time helps you explain design choices clearly and shows you know how code structure relates to performance.
"What if we replaced the loop with recursion calling draw on each shape? How would the time complexity change?"
Practice
Solution
Step 1: Understand polymorphism concept
Polymorphism means one interface can represent many forms or object types.Step 2: Identify the benefit in code flexibility
This allows calling the correct function automatically depending on the object type, making code flexible and reusable.Final Answer:
It allows one interface to work with different object types. -> Option AQuick Check:
Polymorphism = One interface, many types [OK]
- Thinking polymorphism improves speed directly
- Confusing polymorphism with code immutability
- Believing polymorphism forces identical function names
Solution
Step 1: Recall virtual function syntax
In C++, the keyword 'virtual' comes before the return type in the function declaration.Step 2: Match correct syntax
Only 'virtual void func();' matches the correct syntax for declaring a virtual function.Final Answer:
virtual void func(); -> Option CQuick Check:
Virtual keyword before return type [OK]
- Placing 'virtual' after function name
- Mixing order of keywords
- Omitting parentheses in declaration
class Base {
public:
virtual void show() { std::cout << "Base"; }
};
class Derived : public Base {
public:
void show() override { std::cout << "Derived"; }
};
int main() {
Base* ptr = new Derived();
ptr->show();
delete ptr;
return 0;
}Solution
Step 1: Understand virtual function behavior
The 'show' function is virtual in Base and overridden in Derived, so the call uses Derived's version.Step 2: Analyze pointer type and object
Pointer is Base* but points to Derived object, so ptr->show() calls Derived::show(), printing "Derived".Final Answer:
Derived -> Option DQuick Check:
Virtual function calls Derived version [OK]
- Assuming Base version prints because pointer type is Base*
- Confusing override keyword effect
- Ignoring virtual function behavior
class Animal {
public:
void sound() { std::cout << "Animal sound"; }
};
class Dog : public Animal {
public:
void sound() { std::cout << "Bark"; }
};
int main() {
Animal* a = new Dog();
a->sound();
delete a;
return 0;
}Solution
Step 1: Check function declaration in base class
The base class function 'sound' is not declared virtual, so polymorphism won't work.Step 2: Understand effect on function call
Because 'sound' is not virtual, calling a->sound() calls Animal::sound(), not Dog::sound().Final Answer:
Missing virtual keyword in base class function. -> Option AQuick Check:
Base function must be virtual for polymorphism [OK]
- Thinking inheritance alone enables polymorphism
- Using wrong pointer type for polymorphism
- Expecting return type change to fix polymorphism
Solution
Step 1: Identify polymorphism design pattern
Polymorphism requires a common interface with virtual functions to allow different behaviors.Step 2: Match design to polymorphism
Base class Shape with virtual draw() lets Circle and Square override draw(), enabling polymorphic calls.Final Answer:
Create a base class Shape with a virtual draw() method, and override draw() in Circle and Square classes. -> Option BQuick Check:
Virtual base method with overrides enables polymorphism [OK]
- Not using a base class for common interface
- Using static methods which don't support polymorphism
- Using separate global functions instead of class methods
Ready to prove it?
Pass the test and this pattern moves unknown
