Loading...

Polymorphism in C++

Imagine your school morning assembly. The principal, a teacher, and a student may all walk up to the microphone, they all “speak,” but each speaks differently.

Same action, different behavior.

This is exactly what polymorphism means in programming:
One action, many behaviors.

Today, you’ll teach C++ how different objects can respond uniquely to the same function call.

The Project: A Speaking System

You will create:

  • A base class: Person with a function speak().
  • Two derived classes: Student and Teacher.
  • Each derived class overrides the speak() function to speak differently.

Output:

cpp
1
2
3
Person speaks.
Student: “Good morning!”
Teacher: “Please take your seats.”

This is your first experience with runtime polymorphism.

What Is Polymorphism?

Polymorphism allows different objects to respond differently to the same function call. It makes programs flexible and dynamic. Useful in large systems like games and apps.

Function Overriding

A derived class can rewrite (override) a function from the base class. This changes behavior while keeping the same function name. It enables “same function, different output.”

Example

cpp
1
void speak() { cout << "Student speaking"; }

Virtual Functions

A virtual function allows proper overriding during runtime. C++ chooses the correct function based on the object’s real type, not the pointer type. This is the foundation of runtime polymorphism.

Example

cpp
1
virtual void speak();

Dynamic Binding (Runtime Polymorphism)

The decision of which function to call is made at runtime. This allows flexible, extendable programs. Especially useful in systems with many object types.

Base Class Pointers

A base class pointer can point to derived objects. This lets you call overridden functions through a shared interface.

Example

cpp
1
2
Person* p = new Student();
p->speak();

Let’s Build the Speaking System

cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;

class Person {
public:
    virtual void speak() {
        cout << "Person speaks." << endl;
    }
};

class Student : public Person {
public:
    void speak() override {
        cout << "Student: \"Good morning!\"" << endl;
    }
};

class Teacher : public Person {
public:
    void speak() override {
        cout << "Teacher: \"Please take your seats.\"" << endl;
    }
};

int main() {
    Person* p1 = new Person();
    Person* p2 = new Student();
    Person* p3 = new Teacher();

    p1->speak();
    p2->speak();
    p3->speak();

    delete p1;
    delete p2;
    delete p3;

    return 0;
}

How It Works

  • speak() is virtual in the base class.
  • Derived classes override it with custom versions.
  • Base class pointers call the correct function at runtime.

Run it instantly in the DevsCall AI Code Runner to see polymorphism in action.

Learn Together with AI

Try these AI prompts:

  • “Explain polymorphism in simple words with a real-life example.”
  • “Write a base class Animal and override speak() for Dog and Cat.”
  • “Show how overriding works with virtual functions.”
  • “Create a menu-based polymorphism demo in C++.”

AI can help generate variations and deeper examples.

Practice Time

Write a C++ program that:

  1. Creates a base class Shape with a virtual function draw().
  2. Derives two classes: Circle and Square.
  3. Overrides draw() in each class with different messages.
  4. Uses a base class pointer to call all versions:

Expected output:

text
1
2
3
Drawing a shape.
Drawing a circle.
Drawing a square.

Ask AI for help:

“How do I use a base pointer to call overridden functions in C++?”

Frequently Asked Questions

Polymorphism allows different objects to respond to the same function call in different ways. It enables flexible, dynamic, and reusable code.

Overriding means redefining a base class function in a derived class using the same name and signature, but with different behavior.

Virtual functions ensure the correct overridden function is called at runtime, even when using a base class pointer.

Yes! A base pointer can reference any derived object, making it useful for polymorphic behavior and clean code design.

Without virtual, C++ performs static binding and calls the base class function even when the object is of a derived type.

No. Overriding changes behavior in derived classes. Overloading allows multiple functions with the same name but different parameters.

Still have questions?Contact our support team