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.
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.
That’s about virtual functions. Now let us look into a program to understand dynamic binding in C++ :
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.
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.
Learn Software Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
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 Executive PG Program 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.
What is C++ programming language?
C++ is a general-purpose programming language. It is one of the most widely used programming languages in the world. C++ is an enhanced version of C language and provides facilities that facilitate object-oriented programming. C++ is a general-purpose programming language while C is a special purpose programming language. C++ was created as an extension of C to support object-oriented programming and is used to develop desktop applications, applications for mobile devices, embedded and real-time systems, and web applications and services. C++ is an imperative, procedural, multi-paradigm, compiled, general-purpose programming language. It supports structured programming, object-oriented programming, and generic programming. It has a dynamic type system and explicit memory management.
What is object-oriented programming?
OOP is the way to create reusable, easy to maintain and readable code. In short, OOP is a programming paradigm which is based on designing the software around the real-world objects. For example, in Java, if you want to design a software that deals with books, you create a class called Book and then create separate instances (objects) of this class representing a single book. The same applies to other classes which you need to create in the software.
What is dynamic binding in C++?
Dynamic binding associates the meaning of an identifier in a program text with the definition for that identifier found at runtime. This meaning association is called binding. It is the fundamental principle of object-oriented programming. Dynamically bound identifiers are the branches and leaves of the object-oriented class hierarchy. They are dynamic because they are resolved at run-time, not at compile-time. The branches and leaves of the object-oriented class hierarchy are dynamic because they are bound to the classes derived from them.