Early Binding and Late Binding in C++
In C++, early binding and late binding are two important concepts that determine the method of resolving function calls during runtime. Early binding is also known as static binding or compile-time polymorphism. It refers to the process of resolving function calls at compile time. In contrast, late binding is also known as dynamic binding or runtime polymorphism. It refers to the process of resolving function calls at runtime.
In this blog, we'll discuss both early binding and late binding in detail and learn how to implement them in C++ with code, input, output, and syntax.
Early Binding
Early binding is a mechanism in which the compiler decides which function to call during the compile time. It happens when the function call is bound to the function definition at compile time. In C++, early binding is achieved through the use of function overloading and operator overloading.
Function Overloading
Function overloading is a feature in C++ that allows multiple functions with the same name to exist in a program. The compiler resolves the function call by checking the number and type of arguments passed to the function.
Here's an example of function overloading in C++:
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
float add(float a, float b) {
return a + b;
}
int main() {
int x = 5, y = 6;
float a = 5.5, b = 6.5;
cout<< "Integer Addition: " <<add(x, y) <<endl;
cout<< "Float Addition: " <<add(a, b) <<endl;
return 0;
}
Output:
Integer Addition: 11
Float Addition: 12
In this example, two functions named "add" are defined, one that takes two integer arguments and returns their sum, and another that takes two float arguments and returns their sum. During the compilation, the compiler checks the number and type of arguments passed to the function and resolves the function call accordingly.
Operator Overloading
Operator overloading is another feature in C++ that allows redefining the standard operators in a program to perform user-defined operations. It allows us to perform operations on custom data types in a similar manner as we do with built-in data types.
Here's an example of operator overloading in C++:
#include <iostream>
using namespace std;
class Complex {
private:
float real, imag;
public:
Complex() {
real = imag = 0;
}
Complex(float r, float i) {
real = r;
imag = i;
}
Complex operator+(Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() {
cout<< real << " + " <<imag<< "i" <<endl;
}
};
int main() {
Complex c1(5, 3), c2(2, 4);
Complex c3 = c1 + c2;
cout<< "c1 = ";
c1.print();
cout<< "c2 = ";
c2.print();
cout<< "c3 = ";
c3.print();
return 0;
}
Output:
c1 = 5 + 3i
c2 = 2 + 4i
c3 = 7 + 7i
In this example, the operator `+` is overloaded to add two complex numbers. The `operator+` function takes a constant reference to a `Complex` object and returns a `Complex` object that is the sum of the two complex numbers.
Late Binding
Late binding is also known as dynamic binding. It refers to the process of resolving function calls at runtime. In C++, late binding is achieved through the use of virtual functions.
Virtual Functions
Virtual functions are functions in C++ that can be overridden in derived classes. It allows the program to determine the function to call at runtime based on the actual object type rather than its declared type. Virtual functions are declared using the virtual keyword and are called using a pointer or reference to an object of the base class.
Here's an example of virtual functions in C++:
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() {
cout<< "Drawing Shape" <<endl;
}
};
class Circle : public Shape {
public:
void draw() {
cout<< "Drawing Circle" <<endl;
}
};
class Square : public Shape {
public:
void draw() {
cout<< "Drawing Square" <<endl;
}
};
int main() {
Shape *s;
Circle c;
Square sq;
s = &c;
s->draw();
s = &sq;
s->draw();
return 0;
}
Output:
Drawing Circle
Drawing Square