Signup/Sign In

Polymorphism and Method Overriding in C++

In this tutorial we will cover the concepts of Polymorphism in C++ and Function overriding in C++. We will also see both of these in action using simple code examples.


Polymorphism in C++

Polymorphism means having multiple forms of one thing. In inheritance, polymorphism is done, by method overriding, when both super and sub class have member function with same declaration bu different definition.


Method Overriding in C++

If we inherit a class into the derived class and provide a definition for one of the base class's function again inside the derived class, then that function is said to be overridden, and this mechanism is called Function Overriding


Requirements for Overriding a Function

  1. Inheritance should be there. Function overriding cannot be done within a class. For this we require a derived class and a base class.
  2. Function that is redefined must have exactly the same declaration in both base and derived class, that means same name, same return type and same parameter list.

Example of Function Overriding in C++

class Base
{
    public:
    void show()
    {
        cout << "Base class";
    }
};

class Derived:public Base
{
    public:
    void show()
    {
        cout << "Derived Class";
    }
}

In this example, function show() is overridden in the derived class. Now let us study how these overridden functions are called in main() function.


Function Call Binding with class Objects

Connecting the function call to the function body is called Binding. When it is done before the program is run, its called Early Binding or Static Binding or Compile-time Binding.

class Base
{
    public:
    void shaow()
    {
        cout << "Base class\n";
    }
};

class Derived:public Base
{
    public:
    void show()
    {
        cout << "Derived Class\n";
    }
}

int main()
{
    Base b;       //Base class object
    Derived d;     //Derived class object
    b.show();     //Early Binding Ocuurs
    d.show();   
}

Base class Derived class

In the above example, we are calling the overrided function using Base class and Derived class object. Base class object will call base version of the function and derived class's object will call the derived version of the function.


Function Call Binding using Base class Pointer

But when we use a Base class's pointer or reference to hold Derived class's object, then Function call Binding gives some unexpected results.

class Base
{
    public:
    void show()
    {
        cout << "Base class\n";
    }
};

class Derived:public Base
{
    public:
    void show()
    {
        cout << "Derived Class\n";
    }
}

int main()
{
    Base* b;       //Base class pointer
    Derived d;     //Derived class object
    b = &d;
    b->show();     //Early Binding Occurs
}

Base class

In the above example, although, the object is of Derived class, still Base class's method is called. This happens due to Early Binding.

Compiler on seeing Base class's pointer, set call to Base class's show() function, without knowing the actual object type.