C++ Polymorphism

Polymorphism is an important concept of Object-Oriented Programming. The word polymorphism means “many forms.” In C++, it allows the same function or operator to behave differently depending on how it is used.

For example, the + operator adds two numbers, but it can also be used to join two strings. Similarly, the same function name can work with different parameters.

There are two main forms of polymorphism in C++:

  • Compile-time polymorphism
  • Runtime polymorphism

Function overloading and operator overloading are examples of compile-time polymorphism, while function overriding with virtual functions is used for runtime polymorphism.

Function Overloading in C++

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

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

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

Now the same add() function can be called with two or three values.

cout << add(10, 20);
cout << add(10, 20, 30);

C++ decides which function to call based on the number or type of arguments.

This is called Function Overloading in C++.


Operator Overloading in C++

Operators such as +, -, and == normally work with built-in data types. C++ also allows you to define how an operator should work with objects. This is called operator overloading.

For example:

class Number
{
public:
    int value;

    Number(int v)
    {
        value = v;
    }

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

Now you can use + with objects:

Number a(10);
Number b(20);

Number c = a + b;

cout << c.value;

Output

30

The + operator has been given a meaning for objects of the Number class.


Function Overriding in C++

Function overriding happens when a derived class provides its own version of a function that already exists in the base class.

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

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

Here, both classes have a sound() function. The Dog class provides its own version.

This is called Function Overriding in C++.

However, to get runtime polymorphism through a base-class pointer or reference, the base function needs to be declared virtual.


Virtual Functions in C++

A virtual function allows C++ to decide at runtime which overridden function should be called.

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

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

Now:

Animal *a = new Dog();

a->sound();

delete a;

Output

Dog barks

Although a is an Animal pointer, it points to a Dog object. Because sound() is virtual, C++ calls the Dog version.

This is runtime polymorphism.

The override keyword is not required, but it is useful because it tells the compiler that you intend to override a base-class virtual function.


Pure Virtual Functions

A pure virtual function is a virtual function that has no implementation in the base class.

It is declared using = 0.

class Shape
{
public:
    virtual void draw() = 0;
};

Here, draw() is a pure virtual function.

A derived class must provide its own implementation:

class Circle : public Shape
{
public:
    void draw() override
    {
        cout << "Drawing Circle";
    }
};

Pure virtual functions are useful when the base class should define what a derived class must do, but should not provide the actual implementation.


Abstract Classes

A class containing at least one pure virtual function is called an abstract class.

For example:

class Shape
{
public:
    virtual void draw() = 0;
};

You cannot create an object directly from an abstract class:

Shape s;   // Error

Instead, you create an object of a derived class:

Circle c;
c.draw();

Abstract classes are useful when you want to create a common structure for several related classes.

Types of Polymorphism

The concepts can be grouped like this:

Compile-time polymorphism

  • Function overloading
  • Operator overloading

Runtime polymorphism

  • Function overriding
  • Virtual functions
  • Pure virtual functions
  • Abstract classes

Frequently Asked Questions (FAQs)

Q1. What does polymorphism mean in C++?

Polymorphism in C++ allows the same function, operator, or interface to behave differently depending on the situation. It is an important concept of object-oriented programming.

Q2. How is function overloading different from function overriding?

Function Overloading in C++ uses the same function name with different parameters, while Function Overriding in C++ occurs when a derived class provides its own implementation of a base class function.

Q3. Why is operator overloading used in C++?

Operator Overloading in C++ allows operators such as +, -, or == to work with user-defined objects in a meaningful way.

Q4. When should virtual functions be used in C++?

Virtual Functions in C++ are used when a derived class should provide its own implementation of a function and the correct version needs to be selected at runtime.

Q5. What is a pure virtual function in C++?

A pure virtual function is a virtual function declared with = 0. It requires derived classes to provide an implementation and is commonly used to create abstract classes.

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

Scroll to Top