The base class handle b1 is pointing to derived class object d1, so b1.a & b1.b prints the values of child class object which are inherited from base class.
When executing the statement
b1 = d1;, you are copying the handle to the derived class object stored in the class variable d1 to the class variable b1. The handle to the base class object that was in b1 is lost, and now both b1 and d1 reference the same derived class object.
When you print d1.a and d1.a, you are printing the same class member.
In reply to prasham98:
When executing the statement
b1 = d1;, you are copying the handle to the derived class object stored in the class variable d1 to the class variable b1. The handle to the base class object that was in b1 is lost, and now both b1 and d1 reference the same derived class object.
When you print d1.a and d1.a, you are printing the same class member.
Hi Dave,
With following logic
b1 = d1; // now b1 and d1 are pointing to d1
Now if just add variable c to derived class
int c = 10;
And try to print c through b1, I get error “Could not find member ‘c’ in class ‘b’”