Late Binding in C++ (Dynamic Binding)

Late binding nothing but the most popular run polymorphic technique method overloading

  • We call it as late binding because method calls or identified during the execution(runtime). Among multiple methods in overriding, the method call is binded(attached or matched) with the required method definition at runtime
  • We call it as late because identification of method calls happen not at compilation but during the execution

Definition of LateBinding: Whenever the child class is not satisfied with the implementation of the parent class behavior then it can redefine that particular method with its own definition

C ++ program to demonstrate method overriding

class parent //parent class
{

public:

void property()
{
cout<<"land+cash+gold\n";
}

void marriage()
{
cout<<"arranged marriage\n";
}

};

class child:public parent //child class
{

public:

void marraige()//own definition of child
{
cout<<"love marraige\n";
}
//contains property() from parent and its own marraige()

};

main()
{
child c;
c.property();
c.marriage();
parent p;
p.property();
p.marriage();
}

O/P

love marraige
land+cash+gold
land+cash+gold
land+cash+gold
  • Here year child class inherits two methods from parent ,property() and marriage() child is satisfied with property() but not with marriage() .hence child class had given its own implementation for marriage()
  • If the child class don’t inherit parent method he will not get the property().that’s why there will inheritance during overriding