Early binding and Late binding in C++

Early binding and Late binding

To begin with this further, we must understand a few things first.

Earlybinding

What exactly is binding?

For Functions –

Binding for functions means that wherever there is a function call, the compiler needs to know which function definition should it call?

  • This depends upon the signature of each function declaration & the arguments that it is taking.
  • Also, compiler needs to know when this function binding happens (at compile/run time).

For Variables

It needs to know which variable should be referred to as there may be variables with the same names but different types like int, float, double (in some cases this is allowed in C++)

Early Binding and Late Binding New

Early Binding

As we know we write code in a high-level language. At the time of compilation, the following happens –

The compiler converts this HLL code into a low-level language that computers can understand, mostly machine language.

  • In early binding, the compiler chooses a function declaration, out of many functions with different signatures/arguments at compile time itself.
  • Thus as the name suggests the binding happens very early before the program runs.

This is why early binding is an example of compile-time polymorphism

Code for Early Binding

#include<iostream>
using namespace std;

// Defining the parent class
class Parent 
{
    public: 
    void print() 
    { 
        cout << "Parent Class" << endl; 
    } 
};

// Defining the child classs 
class Child : public Parent 
{
    public:
    // print redeclared in child class
    void print()
    {  
        cout << "Child Class" << endl; 
}
};

int main()
{
Parent *parent = new Child();

// catch of the program is here
// also as we are dealing with pointers instead of . we need to use -> parent->print(); return 0; }

Output –

Parent Class

(Explanation given below)

Now, its obvious to think that the child class would be printed as eventually the address assigned is of the Child class right? since.

  •  Parent *parent = new Child(); is same as defining it as follows –
    • Parent *parent;
    • Child child;
    • parent = &child

And again visualising this way also leads to a conclusion that still child class should have been printed as the end address is of the child class right?

What actually happens?

Now, this isn’t the case obviously and a part of the reason is because of how C++ was written by its founder Bjarne Stroustrup in 1979. What exactly happens here is called static resolution.

This happens because the way  C++ was written which by default makes a static call at the time compilation itself and forces early binding. Now, print function is bounded at compile time.

Late Binding

Late binding in the above problem may be solved by using virtual keyword in the base class. Let’s see how this happens by using the above example itself, but only adding a virtual keyword.

Code

#include<iostream>
using namespace std;
 // Defining the parent class 

class Parent 
{
    public:
    // virtual keyword added here
    // to force late(run-time) binding
    // to ignore static resolution
    virtual void print() 
    { 
        cout << "Parent Class" << endl; 
    }
}; 

// Defining the child classs 
class Child : public Parent 
{
    public:
    // print redeclared in child class
    void print()
    {  
        cout << "Child Class" << endl; 
}
};

int main()
{
Parent *parent = new Child();

// catch of the program is here
// also as we are dealing with pointers instead of . we need to use -> parent->print(); return 0; }

Output –

Child class