|Ash said: (Aug 26, 2013)|
|Why output is 6, 8?|
|Satya said: (Aug 31, 2013)|
|Bix::main() creates an object and sets its variables x=6, and y=8. And then it calls Bix::show().
Bix::show() creates another object and sets its variables x=2, and y=4. And then it displays the object created by Bix::main() and not the one created by it.
Hence output is '6 8'.
|Lokesh said: (May 16, 2014)|
|Can you explain why it called the object of main rather than calling the object of show?|
|Bhairavee said: (Jul 29, 2014)|
|If we were to print b.x and b.y instead of x and y then result would have been 2 and 4 and not 6 and 8.
According to me, The object b which is created again when show is called is different from the object b accessing the show function. Hence, the values displayed are those defined by the object b of the class in the main function.
|Raghav said: (Feb 6, 2015)|
|Is there two different main functions? or both are same?|
|Harsha said: (Mar 15, 2015)|
|In show() method x and y are local and they are global to main method.
So main method values will be printing. If in show method if we print like b.x and b.y in show then we get 2 4 as op.
|Mitali said: (Oct 20, 2016)|
|x means this->x and y means this->y.
So, when x and y are printed they refer to the calling function and in the calling function the values of x and y were assigned to 6 and 8.
|Yash said: (Jul 19, 2017)|
|Is it possible to have a function name as main!?|
|Shelly said: (Jul 29, 2018)|
We can have a function name as main.
|Abraham said: (Jun 23, 2021)|
|Int x, y; is declared as private. Other objects can't access them like in the example above. It will give compile error.
Am I right? Please explain me.
Post your comments here:
Email : (optional)
» Your comments will be displayed only after manual approval.