I dont know how this 20 is printed in the below code. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
6th May 2018, 3:36 PM
S. Surya Narayanan
S. Surya Narayanan - avatar
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
7th May 2018, 6:14 AM
ifl
ifl - avatar
+ 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.
7th May 2018, 12:25 AM
ifl
ifl - avatar
+ 1
and that g(b); how that works ? https://code.sololearn.com/c8KuTrP37yBZ/?ref=app
7th May 2018, 1:45 AM
S. Surya Narayanan
S. Surya Narayanan - avatar