When an object's static type is used to associate it with a member function, this is known as static binding (understand the type of its class).
When a pointer or reference is associated with a member function based on the dynamic type of the object, this is known as dynamic binding (understand the instance of the variable at runtime).
Before continuing, keep in mind that dynamic binding only works with pointers, references, and virtual functions for the base class.
Because everything needed to call the function is known at compile time, the first call is a static binding (also known as early binding).
Derived1 d1(1, 10);
d1.display_data();
You already know that the d1 instance is a Derived1 automatic variable, and that it will call the Derived1::display data method ().
The first condition is incorrect: d1 is neither a pointer nor a reference.
The second condition isn't acceptable:
There is no virtual Derived1::display data.
The second call is for
Base1* d2 = new Derived1(2, 20);
d2->display_data();
We can see that the variable d2's declared type is Base1, but the instance is Derived1 (it is correct because of inheritance thus Derived1 is a Base1 class).
However, you have no idea whether the display data method it will call is from Base1::display data or Derived1::display data.
Because we have the d2 of type pointer Base1*, the first condition is satisfied.
Because Base1::display data is not virtual, the second condition is incorrect.
Because it is still a static binding, the function with the declared type will be called, so the code will call Base1::display data.
The third call is for
// case 3
Derived2 d3(3, 30);
d3.display_data();
This will result in a static binding, which will then be used to call the Derived3:: method.
display data
The first requirement is not met: d3 is neither a pointer nor a reference.
The second condition is acceptable:
It is virtual to use Derived2::display data.
This is the fourth call.
This time it's a dynamic binding:
Base2* d4 = new Derived2(4, 40);
d4->display_data();
The first requirement is satisfied: d4 is a pointer.
The second condition is acceptable:
It is virtual to use Derived2::display data.
At runtime, instead of calling the method from the declared type base2, the method will be called from the declared instance.
Derived2::display data