Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Early binding and Late binding in C++

Early binding and Late binding

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

What exactly is binding?

  • For Functions – Binding for functions mean that  wherever there is a function call, the compiler needs to know which function definition should it be matched to. This depends upon the signature of each function declaration & the assignments that is taking. Also, compiler needs to know that when this matching between the function call and choosing the correct definition it is assigned to will happen.

Example –

  • myFunc(int x, int y) or myFunc(x) or myFunc(float x, float y), these are function declarations. The compiler needs to know if which declaration should it refer to whenever it sees a function call. Which one should it refer them to and also when it should make the decision. At runtime or compile time?

Similarly –

  • For Variables – It needs to know which variable should be referred to as there may be variables with same names but different type like int, float, double (in some cases this allowed in C++)
Early binding and Late binding in C++

Early Binding

Early binding is a phenomenon wherein the decision to match various function calls happens at the compile time itself and the compiler directly associates the link with addresses.

  • As we know we write code in high level language
  • At the time of compilation, the compiler converts this into low level language that computer can understand, mostly machine language
  • In early binding the compiler directly provides the address of function declaration instruction to the function call instruction
  • Thus as the name suggests the binding happens very early before the program runs.
  • Early binding is an example of compile time polymorphism

Problem statement – Pointers are interesting and they cause a lot of interesting deviations in programs

This program will explain you the concept of early binding in C++.

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

class Parent 
{
  public: 
   void print() 
   { 
      cout << "Displaying from parent class section" << endl; 
   } 
}; 

// Defining the child classs 
class Child : public Parent 
{
  public:
  // print redeclared in child class
    void print()
    {  
      cout << "Displaying from child class section" << 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 –

Displaying from parent class section

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

An 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?

Now, this isn’t the case obviously and a pat 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, which by definition on how C++ was written makes a static call at the time compilation itself and forces early binding. Now, print function is bounded at compile time.

  • So remember this whenever there are pointers involved
  • And base(parent) class object is pointed to derived class(child) address, then static resolution will happen.

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 virtual keyword.

 

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

class Parent 
{
  public: 
   virtual void print() 
   { 
      cout << "Displaying from parent class section" << endl; 
   } 
}; 

// Defining the child classs 
class Child : public Parent 
{
  public:
  // print redeclared in child class
    void print()
    {  
      cout << "Displaying from child class section" << 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; 
}

In this case the virtual keyword added in the base class tells the compiler to not to do the binding at compile time and not force static resolution. But, rather only do the binding at runtime and thus do late binding.

Output – 

Displaying from the child class section