+ 1
I dont know how this 20 is printed in the below code.
3 Answers
+ 1
Line g(b) works as follow:
- b has been previously created with the line B b(10), where it's member b.x was initialized with the parameter 10.
-g(b) attempts to cast type B into type A by calling the operator A method in class B
- this calls the A constructor with parameter b.x, which is 10, and initializes the resulting object's member i.
- Then the method show() of the classA object is called, that displays its member i,which is now 10
+ 3
line g(20) does the following:
As 20 is passed as parameter to the function g, the integer g is casted into class A.
So A constructor is called with parameter 20 to create object a,
this initializes the member a.i to 20.
Then the function A.show() prints i, which is 20.
+ 1
and that g(b); how that works ?
https://code.sololearn.com/c8KuTrP37yBZ/?ref=app