Programs

Dynamic Binding in C++: Explanation, Functions & Implementation

Introduction

This article is going to walk you through the working of dynamic binding in C++. You are expected to have a basic knowledge of the working of inheritance and static binding in C++. This is because some limitations of static binding are removed by dynamic binding. We will show you, along with proper and simple codes, how dynamic binding is useful.

What is Dynamic Binding?

By binding, we generally mean linking one object with another. In terms of programming languages, binding is not very different. It is the linking of a function definition and a function call. But why ‘dynamic’? In simple words, dynamic binding is just delaying the choice or selection of which function to run until its runtime. Using dynamic binding in C++, we choose which function we can execute based on the underlying type of the object.

This flexibility was not afforded to programmers with static binding. The static binding used to happen at compile time and not run time. Additionally, static binding made linkage of function call and definition during compile time, whereas, as you now know, dynamic binding does not do that until run time.

Dynamic binding allows us to ignore the type differences by providing us with the flexibility in choosing which type of function we need at that instant of runtime. On examining this statement, we can understand that dynamic binding allows us to handle different objects using just a single function name. This reduces code complexity and also enables the programmer to have an easier time while debugging.

Virtual Functions

We have talked about dynamic binding in C++ a lot. But how to proceed with that? To implement dynamic binding in your C++ code, you need to use ‘virtual functions’. So what are virtual functions?

This article will not be going extensively into virtual functions as they are important enough for a separate article. But you will be provided with enough information about virtual functions to understand dynamic binding clearly.

Virtual functions are special member functions to which calls made through a pointer (or reference) are resolved at run time, based on the object’s type with the pointer. In simple words, if you define a virtual function in a base class, you can override the function in derived classes.

This is the power of virtual functions. In C++, you define a virtual function using the keyword ‘virtual’ at the beginning of the function definition. You may not have to repeat the keyword virtual for every derived class of a base class because once a function is declared ‘virtual’, it will remain ‘virtual’ in all of the derived classes.

C++ Implementation

That’s about virtual functions. Now let us look into a program to understand dynamic binding in C++ :

Program #1:

We see two classes:- Class A is the ‘base’ class, whereas Class B is the ‘derived class’. Inside both the classes, are one function with the same name ‘display’. In parent class, there is another function named final_print() calling the display() function. In the main() function, we make two different objects for two different classes, calling the same display() function.

Let us see the output of the above program.

Output for Program #1:

As expected, the output executes the base class’s display() function two times as it is defined at compile time (static binding). But this is not the output we wanted. 

Now to convert the above code into a dynamic binding one, we need to use virtual functions. Let us see how to do that.

Program #2:

We just add the ‘virtual’ keyword for both of our display() functions in Class A and Class B, thus making them virtual functions. That is the only change needed. Just like before, we call the same final_print() function using two different objects. Let us see what output we get now.

Output for Program #2:

This is the output we wanted, and we got it using the dynamic binding of the functions. 

Conclusion

In this article, we went through the concept of dynamic binding in C++. To summarize, the dynamic binding allows us flexibility by calling a single function to handle different objects. As evident from our code, we called the same function display() by making two different objects to give us two different outputs. This is possible because dynamic binding happens at runtime, unlike static binding which happens at compile time.

If you’re interested to learn more about full-stack development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Prepare for a Career of the Future

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN FULL STACK SOFTWARE DEVELOPMENT
APPLY NOW

Leave a comment

Your email address will not be published.

Accelerate Your Career with upGrad

Our Popular Software Engineering Courses

×