Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism

Binding

C++ Object Oriented Programming

Explain the following ‒ Early binding and late binding.

Binding

•  Binding means connecting function calls to appropriate function definition. The function call can be associated with its definition at compile time or at run time. Depending upon it there are two types of binding defined ‒ 1) Early binding or static binding and 2) Late binding or dynamic binding.


1. Early Binding

This is a type of binding in which the function call is associated with the function definition at the compile time only. Hence this type of binding is also called as early binding. Following example illustrates this concept ‒

/*****************************************************************

Program to demonstrate the static binding concept

********************************************************************/

#include<iostream>

using namespace std;

class Base

{

public:

void display()

{

      cout<<"\n In base class"<<endl;

}

};

class Derived: public Base

{

public:

void display()

{

      cout<<"\n In derived class"<<endl;

}

};

int main()

{

      Base b;

      Derived d;

      b.display();

      d.display();

      return 0;

}

Output

In base class

In derived class

Program Explanation

In above program, we have declared the single level inheritance. That is the Derived class is derived from the base class Base. In both of these classes we have declared a function by the same name as display. This function will get appropriately called when it is called using its object. This process of calling the correct function at correct time is called the early binding.

 

2. Late Binding

• C++ provides facility to specify that the compiler should match function calls with the correct definition at the run time; this is called late binding or dynamic binding. This type of binding is called the late binding because the compiler can not resolve the call to appropriate function late until the run time.

Late binding is achieved using virtual functions.

• Following program illustrates this concept ‒

/**********************************************************************

Program to demonstrate the dynamic binding concept

*************************************************************************/

#include<iostream>

using namespace std;

class Base

{

public:

virtual void display()

{

       cout<<"\n In base class"<<endl;

}

};

class Derived: public Base

{

public:

void display()

{

       cout<<"\n In derived class"<<endl;

}

};

int main()

{

       Base *ptr;

       Base b;

       Derived d;

       ptr=&b;

       ptr‒>display();

       ptr=&d;

       ptr‒>display();

       return 0;

}

Output

In base class

In derived class

Program Explanation

Base class pointer points to derived class object. And a function is declared virtual in base class, then the matching function is identified at run‒time. If we do not associate the keyword virtual to the display() function of base class then even though the address of derived class is assigned to the base class pointer ptr, it will select the display function of base class only. Thus late binding or dynamic binding is achieved by means of virtual function.

 

Difference between Early binding and Late binding


Early binding

1. Static binding happens at the compile time.

2. Static binding is also called as early binding.

3. There is no use of virtual function in this type of binding.

4. It is more efficient than the late binding as extra level of indirection is involved indirection is in in late binding.

Late binding

1. Late binding happens at run time.

2. Late binding is also called as dynamic binding.

3. The virtual function is used in dynamic binding.

4. It more flexible than the early binding.

 

Review Question

1. Explain the following ‒ Early binding and late binding.

 

Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism : Tag: Oops, Computer Programming : C++ Object Oriented Programming - Binding


Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation