The derived
class in your program has a member function named func
that hides the inherited member function with the same name from base
. Similarly, the derived
class also a data member named x
that hides the inherited data member with the same name from base
.
Now, when you wrote:
d.func(); //this calls the derived class member function `func` implicitly passing the address of `d` to the implicit `this` parameter
In the above call, you're calling the derived
class' member function func
meanwhile implicitly passing the address of d
to the implicit this
parameter. The type of this
inside func
of derived
is derived*
.
Now, inside the member function func
of derived
the expression this->x
uses the data member x
of the derived class(and not the inherited member x
) because as explained above, it hides the inherited member from base. Thus we get the output 20
.
Next, cout<< this << endl;
just prints this
.
Next, the call expression base::func()
is encountered. Also note that base::func()
is equivalent to writing this->base::func()
. This has the effect that it calls the inherited member function named func
that was hidden due to the derived class having a function with the same name. Note in this call also, the same address is passed to the implicit this
parameter of func
of base class. The difference this time is that, this
inside base
's member function func
is of type base*
.
Now, inside the member function func
of base
, the x
in the expression this->x
refers to the base class' data member x
and hence prints the value 10
. Thus we get the output 10
.
Finally, we have cout<< this << endl;
inside base
's member function func
, which just prints the this
.