0% found this document useful (0 votes)
70 views

Static Vs Dynamic Binding in C++

Uploaded by

talhasultan734
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Static Vs Dynamic Binding in C++

Uploaded by

talhasultan734
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Static Binding Vs Dynamic Binding in C++

Binding helps in linking a bridge between a function call and its corresponding function definition.
When we create a function, we have two crucial things:

i. A function definition - defines a procedure to execute.


ii. A function call - invokes the respective function for implementation.

Now both the function definition and function calls are stored in the memory at separate addresses.
And our program can have more than one function for its smooth operation. Hence, we need a
technique to match the appropriate function call with its definition.

The process of matching a specific function call to its respective function definition is known as binding.
Let us understand the need for binding with a practical example. Suppose we have a class bind with two
member functions. The class definition, along with the driver code, is as follows:
#include <iostream>
using namespace std;
class bind {
public:
void fun1() {
cout << "Hi! This is fun1.\n";
}
void fun2() {
cout << "Hi! This is fun2.\n";
}
};
main() {
bind obj;
obj.fun1();
obj.fun2();
}
If there was no way of binding in C++, the two calls & definitions could’ve mixed up. Hence, binding is
needed to link a function call with its function definition accurately.

When this binding occurs at Compile time, we call it a Static Binding, and when this binding occurs at
Runtime, we call it a Dynamic Binding.

What is Static Binding?


Binding at compile time is known as static binding.

There is always a default setting for any tool or method. For binding, static binding in C++ is the default
route. Static binding ensures linking the function call and its function definition at compile-time only. It
is also why it is synonymous with compile-time binding or early binding.
There are two ways by which static binding can be achieved:
 Function overloading &
 Operator overloading.
Overloading is a technique through which the compiler uniquely identifies each function
definition when it is called or linked. It locates by matching the parameters passed to those
functions.

Function Overloading
When two or more functions have the same name but differ in the types or number of parameters they
accept, it is known as function overloading. It is a feature of object-oriented programming.

Now let us create a class with member functions only to depict static binding in C++. We will return a
different number of parameters to overload our member functions.
#include <iostream>
using namespace std;
class func {
public:
void stat(int a, int b) {
cout <<"first class parameters are " << a + b;
}
void stat(int a, int b, int c) {
cout <<"second class parameters are " << a + b + c;
} };
main() {
func obj;
int a, b, c;
a = 1;
b = 1;
c = 1;
obj.stat(a, b, c);
obj.stat(a, b);
}

Operator Overloading
Usually, we use operators to do mathematical calculations, but if we want to do operations on
the objects of a class, how can that be performed? It takes place using operator overloading. In operator
overloading, we give a special meaning to our operator to perform operations on objects of a class.
Hence, we overload our desired operator.

We can perform unary operator overloading that works on one operand only or binary operator
overloading that works on two operands. For this example, we will consider binary operator
overloading.

Let us try to add two numbers using a class. The class definition with the driver code is as follows:
#include <iostream>
using namespace std;
class Over_num {
int x, y;
public:
void input();
void input2();
Over_num operator + (Over_num & ob);
void print();
};
void Over_num::input() {
cout << " Enter the first number(operand): ";
cin >> x;
}
void Over_num::input2() {
cout << " Enter the second number(operand): ";
cin >> y;
}
void Over_num::print() {
cout << "The sum of two numbers is: " << x;
}

// Overload the binary (+) operator.


Over_num Over_num::operator + (Over_num ob) {
Over_num A;
A.x = x + ob.x;
return (A);
}
main() {
Over_num x1, y1, res;
x1.input();
y1.input2();

res = x1 + y1;
res.print();
}

What is Dynamic Binding?


Binding at runtime is known as dynamic binding.

There are instances in our program when the compiler cannot get all the information at compile time to
resolve a function call. These function calls are linked at runtime. Such a process of binding is called
dynamic binding. Since everything is postponed till runtime, it is also known as run-time binding or late
binding.

In C++, it is executed using virtual functions. A virtual function is a member function in the base class
that is overridden (re-defined) by its derived class(es). When declaring in the base class, we use the
keyword virtual to distinguish it.
Virtual Function
It is a function in the base class inherited by the derived class. The functions have the same
name, but their functionality can differ as required. We know how to access base class member
functions. But how do we access it in the derived class? We use a pointer or a reference to the base
class and access the derived class version of the function.
#include<iostream>
using namespace std;
class base {
public:
virtual void common() {
cout << "function of base class\n";
} };

class derived: public base {


public:
void common() {
cout << "function of derived class\n";
} };

main() {
base * ptr;
derived d;
ptr = & d;
// Virtual function, binded at runtime.
ptr -> common();
}
Dynamic binding in C++ runs successfully using a pointer. Here the pointer stores the location of the
derived class. The pointer ptr is assigned the address of object d of the derived class. Hence, it executes
the member function of that class.

You might also like