C++

Static Binding and Dynamic Binding in C++

C++ is a well-known object-oriented programming language that supports both static and dynamic binding. These concepts play a crucial role in C++ program execution and understanding the difference between them is essential for efficient code development.

This article goes over the differences between dynamic and static binding in C++ as well as how to use each to create code that is as efficient as possible.

What is Static Binding in C++?

Static binding is an approach in which the function call is resolved by the compiler during the process of compilation. Depending on the provided type of object, the implementation of a function required to be called in this situation is decided at compile time. This means that the function call cannot be changed at runtime and keeps unchanged at compile time. This type of binding is commonly used in programming languages such as C++. There are two methods for achieving static binding:

Function Overloading: Function overloading occurs when multiple functions have a similar name and differ in the types or number of parameters that they accept. It is an attribute of object-oriented programming.

Operator Overloading: In operator overloading, users offer the operator a special meaning for performing operations on the objects of the class. As a result, we overload the desired operator. We can implement unary operator overloading on a single operand or binary operator overloading on two operands.

Example

Consider the following C++ program to demonstrate the principle of static binding.

#include <iostream>
using namespace std;
class Multiplication
{
    public:
    double multiply(float num1, float num2)
    {
        return num1 * num2;
    }
    double multiply(float num1, float num2, float num3)
    {
        return num1 * num2 * num3;
    }
};
int main()
{
    Multiplication obj;
    cout << "Result of multiplication is " << obj.multiply(9.786, 0.0009) << endl;
    cout << "Result of multiplication is " << obj.multiply(9.867, 2.980, 3.145632) << endl;
    return 0;
}

 

The multiply() function is overloaded in the above code having two and three float parameters. Even though the Multiplication class contains two functions with the same name, the function multiply() binds to the right function based on the parameters passed to those functions. This binding is done statically during the compilation process.

What is Dynamic Binding in C++?

In our program, there are times when the compiler does not have all the information needed to resolve the function call at build time. Runtime is when these function calls get connected. Dynamic binding is the name for this kind of binding. It often gets referred to as either run-time binding or late binding because everything is deferred until runtime. In simple words, dynamic binding refers to binding that occurs at runtime.

C++ virtual functions are used to execute it. A virtual function is a base class member function that a derived class or classes override (redefine). We utilize the keyword virtual to differentiate it while defining it in the base class.

Example

Consider the following C++ program to demonstrate the principle of dynamic binding.

#include <iostream>
using namespace std;
class base
{
    public:
    virtual void fun()
    {
        cout << "The function called is base class function.\n";
    }
};
class drive: public base
{
    public:
    void fun() {
        cout << "The function called is drive class function.\n";
    }
};
int main()
{
    base b;
    drive d;
    base *bPtr = &b;
    bPtr->fun();
    bPtr = &d;
    bPtr->fun();
    return 0;
}

 

We have a base class named base and a derived class named drive in the above code. The virtual function fun() inside the base class is replaced by a function within a derived class. It should be noticed that the dynamic nature of an object pointed to via bPtr determines what class’s function is going to be called. The fun() has become subject to dynamic binding because this data can only be gathered at runtime.

Conclusion

Binding establishes a connection between the function call as well as its definition. In C++, we have two forms of binding: static binding and dynamic binding. Static binding works during compilation and provides greater performance. The function overloading or the operator overloading can be used to implement static binding. Dynamic binding is more versatile and works during runtime. Virtual functions can be used to provide dynamic binding.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.