0
0
C++programming~10 mins

Why polymorphism is needed in C++ - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why polymorphism is needed
Define base class pointer
Assign derived class object
Call virtual function
Derived class function runs
Program behaves correctly for different types
END
Polymorphism lets a program decide at runtime which function to run, so one pointer can work with many types and call the right function.
Execution Sample
C++
#include <iostream>
using namespace std;

class Animal {
public:
  virtual void sound() { cout << "Animal sound"; }
};
class Dog : public Animal {
public:
  void sound() override { cout << "Bark"; }
};

int main() {
  Animal* a = new Dog();
  a->sound();
  delete a;
  return 0;
}
This code shows a base class pointer pointing to a derived class object and calling the correct overridden function.
Execution Table
StepPointer TypeObject TypeFunction CalledOutput
1Animal*DogDog::sound()Bark
2Animal*AnimalAnimal::sound()Animal sound
3Animal*DogDog::sound()Bark
4END--Program ends
💡 Program ends after calling the correct function based on object type at runtime.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
a (Animal*)nullpoints to Dog objectpoints to Dog objectpoints to Dog object
Key Moments - 2 Insights
Why does the Dog's sound() run instead of Animal's when using an Animal pointer?
Because the function is virtual, the program checks the actual object type at runtime and calls Dog's sound(), as shown in execution_table step 1.
What if sound() was not virtual? Which function would run?
If sound() was not virtual, Animal's sound() would run even if the pointer points to Dog, shown by comparing step 2 in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what function is called at step 1?
AAnimal::sound()
BNo function called
CDog::sound()
DCompiler error
💡 Hint
Check the 'Function Called' column in execution_table row for step 1.
At which step does the Animal::sound() function run?
AStep 1
BStep 2
CStep 3
DNever
💡 Hint
Look at the 'Function Called' column in execution_table for Animal::sound().
If sound() was not virtual, what would happen at step 1?
AAnimal::sound() runs
BDog::sound() runs
CProgram crashes
DNo output
💡 Hint
Refer to key_moments explanation about virtual function behavior.
Concept Snapshot
Polymorphism allows one pointer to call different functions based on object type.
Use virtual functions in base class.
Derived classes override these functions.
At runtime, correct function runs.
Without virtual, base class function always runs.
This enables flexible and reusable code.
Full Transcript
Polymorphism is needed so that a program can decide which function to run based on the actual object type at runtime. In C++, this is done using virtual functions. A base class pointer can point to different derived class objects. When calling a virtual function through this pointer, the program runs the derived class's version, not the base class's. This behavior allows writing flexible code that works correctly with many types. If the function is not virtual, the base class version always runs, which is usually not what we want. The example shows an Animal pointer pointing to a Dog object. Calling sound() runs Dog's sound(), printing 'Bark'. This is polymorphism in action.