C++ Polymorphism

The word polymorphism may sound difficult, but the basic idea is simple. Polymorphism means “many forms.” In C++, polymorphism allows the same function or operation to behave differently in different situations.

For example, a Dog can make a barking sound, while a Cat can make a meowing sound. Both can have a function called sound(), but the function behaves differently.

What is C++ Polymorphism?

Let’s look at a simple example:

Animal
  ↓
 ┌───────┐
Dog     Cat
 ↓       ↓
Bark    Meow

Both Dog and Cat can have a function named sound(), but each one can do something different.

C++ mainly has two types of polymorphism:

  • Compile-time polymorphism
  • Run-time polymorphism

Let’s understand them one by one.

Compile-Time Polymorphism

Compile-time polymorphism happens when C++ decides which function to use during compilation. One common example is function overloading.

Function Overloading in C++

Function overloading means having multiple functions with the same name but different parameters.

#include <iostream>
using namespace std;

class Calculator {
public:
    int add(int a, int b) {
        return a + b;
    }

    int add(int a, int b, int c) {
        return a + b + c;
    }
};

int main() {
    Calculator calculator;

    cout << calculator.add(10, 20) << "\n";
    cout << calculator.add(10, 20, 30);

    return 0;
}

Output:

30
60

Both functions are named add(), but they take different numbers of parameters. C++ chooses the correct function based on the arguments we provide.

Operator Overloading in C++

We normally use operators like +, -, and == with numbers and other basic data types. Operator overloading allows us to use these operators with our own class objects.

For example, we can make the + operator add the values of two Number objects.

#include <iostream>
using namespace std;

class Number {
public:
    int value;

    Number operator+(Number n) {
        Number result;
        result.value = value + n.value;
        return result;
    }
};

int main() {
    Number n1, n2, result;

    n1.value = 10;
    n2.value = 20;

    result = n1 + n2;

    cout << result.value;

    return 0;
}

Output:

30

Here, n1 + n2 normally would not work with two objects. We overloaded the + operator to add their value.

Operator overloading is a form of compile-time polymorphism.

Run-Time Polymorphism

Run-time polymorphism happens when C++ decides which function to run while the program is running. It is commonly achieved using:

  • Inheritance
  • Function overriding
  • Virtual functions

Let’s understand this with a simple example.

Function Overriding C++

When a derived class provides its own version of a function that already exists in the base class, it is called function overriding.

#include <iostream>
using namespace std;

class Animal {
public:
    void sound() {
        cout << "Animal makes a sound\n";
    }
};

class Dog : public Animal {
public:
    void sound() {
        cout << "Dog barks";
    }
};

int main() {
    Dog dog;

    dog.sound();

    return 0;
}

Output:

Dog barks

The Dog class has its own sound() function, so it uses that version.

Virtual Functions

To properly use run-time polymorphism through a base class pointer or reference, C++ provides the virtual keyword.

#include <iostream>
using namespace std;

class Animal {
public:
    virtual void sound() {
        cout << "Animal makes a sound";
    }
};

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

int main() {
    Animal* animal = new Dog();

    animal->sound();

    delete animal;

    return 0;
}

Output:

Dog barks

Here, animal is an Animal pointer, but it points to a Dog object. Because sound() is declared as virtual, C++ calls the Dog version of the function.

The override keyword tells C++ that the derived class is intentionally replacing a virtual function from the base class.

Why Use Polymorphism?

Polymorphism is useful when different objects need to respond to the same function call in different ways.

For example:

Animal → sound()
   ↓
Dog    → Bark
Cat    → Meow
Cow    → Moo

The function name can remain sound(), while each class provides its own behavior. This makes large programs easier to organize and extend.

A Simple Example

Let’s create a small program using different animals.

#include <iostream>
using namespace std;

class Animal {
public:
    virtual void sound() {
        cout << "Animal makes a sound\n";
    }
};

class Dog : public Animal {
public:
    void sound() override {
        cout << "Dog says Woof\n";
    }
};

class Cat : public Animal {
public:
    void sound() override {
        cout << "Cat says Meow\n";
    }
};

int main() {
    Animal* animal1 = new Dog();
    Animal* animal2 = new Cat();

    animal1->sound();
    animal2->sound();

    delete animal1;
    delete animal2;

    return 0;
}

Output:

Dog says Woof
Cat says Meow

Both pointers use sound(), but the output is different because they point to different objects.

Key Points to Remember

  • Polymorphism means many forms.
  • It allows the same function or operation to behave differently.
  • Function overloading is an example of compile-time polymorphism.
  • Function overriding is used for run-time polymorphism.
  • The virtual keyword is important for run-time polymorphism.
  • The override keyword shows that a derived class is replacing a virtual function.
  • Polymorphism is commonly used with inheritance.

Mini Quiz

0%

C++ PROGRAMMING - QUIZ 16

1 / 5

Q1. What does polymorphism mean in C++?

2 / 5

Q2. Which polymorphism is commonly achieved using function overloading?

3 / 5

Q3. What is function overloading?

4 / 5

Q4. Which keyword is used to enable run-time polymorphism through a base class pointer or reference?

5 / 5

Q5. What does the override keyword indicate?

Your score is

The average score is 0%

0%

Mini Project

Mini Project: Payment System

Question:

Create a simple C++ program using polymorphism to calculate payments for different payment methods.

  • Create a base class Payment.
  • Add a virtual function pay().
  • Create two derived classes:
    • UPIPayment
    • CardPayment
  • Override the pay() function in both classes.
  • Use a base class pointer to call the appropriate function.
View Answer Code
#include <iostream>
using namespace std;

class Payment {
public:
    virtual void pay() {
        cout << "Processing payment\n";
    }
};

class UPIPayment : public Payment {
public:
    void pay() override {
        cout << "Payment made using UPI\n";
    }
};

class CardPayment : public Payment {
public:
    void pay() override {
        cout << "Payment made using Card\n";
    }
};

int main() {
    Payment* payment1 = new UPIPayment();
    Payment* payment2 = new CardPayment();

    payment1->pay();
    payment2->pay();

    delete payment1;
    delete payment2;

    return 0;
}

What’s Next?

In the next chapter, we will learn about C++ Friend Functions and see how a function can access the private and protected members of a class with special permission.


Frequently Asked Questions (FAQs)

Q1. What is polymorphism in C++?

Polymorphism in C++ allows the same function or operation to behave differently depending on the object or arguments involved.

Q2. What are the types of polymorphism in C++?

The two main types are compile-time polymorphism and run-time polymorphism.

Q3. What is function overloading in C++?

Function overloading allows multiple functions to have the same name with different parameters.

Q4. What is function overriding in C++?

Function overriding occurs when a derived class provides its own version of a function inherited from a base class.

Q5. Why is polymorphism used in C++?

Polymorphism allows related classes to respond differently to the same function call, making programs easier to extend and manage.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top