Solution:
Binding mentions to the process of converting identifiers into addresses. Binding takes place either in run time or at compile time. There are two types of binding in C++. One is Static/Early binding and the other one is dynamic/late binding.
Static Binding: Static/Early binding says by its own name. In this binding, the compiler directly associates an address to the function call. It substitutes the call with a machine language guidance that tells the mainframe to jump to the address directly of the function. Let’s have an example of Static Binding
#include<iostream>
using namespace std;
class BaseClass{
public:
void show(){
cout<<" Displaying in BaseClass \n";
}
};
class Derived: public BaseClass{
public:
void show(){
cout<<"Displaying in DerivedClass \n";
}
};
int main(void){
BaseClass *bc = new Derived;
bc->show();
return 0;
}
Output should be
Displaying in BaseClass
Dynamic Binding: Dynamic Binding is also known as runtime polymorphism. Late binding let the compiler identifies the kind of object at runtime then meets the call with the right function definition. This type of binding can be achieved by declaring a virtual function.
#include<iostream>
using namespace std;
class BaseClass{
public:
virtual void show(){
cout<<"Displaying in BaseClass \n";
}
};
class Derived: public BaseClass{
public:
void show(){
cout<<"Displaying in Derived \n";
}
};
int main(void){
BaseClass *bc = new Derived;
bc->show();
return 0;
}
In this case, the output should be
Displaying in Derived
This might help you.