C++ Programming - References - Discussion
Discussion Forum : References - Programs (Q.No. 19)
19.
Which of the following statement is correct about the program given below?
#include<iostream.h>
class IndiaBix
{
int x, y;
public:
void SetValue(int &xx, int &yy)
{
x = xx ++;
y = yy;
Display();
}
void Display()
{
cout<< x << " " << y;
}
};
int main()
{
int x = 10;
int &y = x;
IndiaBix objBix;
objBix.SetValue(x , y);
return 0;
}
Discussion:
4 comments Page 1 of 1.
Pravin Patil said:
8 years ago
Answer is B because.
In main, x = 10, and y is refer value of x Therefore y = 10.
then objBix.SetValue(x , y) means we pass the x and y value to the SetValue function.
SetValue(int &xx, int &yy) &xx refers value of x and &yy refers value of y i.e &xx= 10, & yy = 10.
Then we enters into functioncSetValue,
function contains
x = xx ++; here post increment of xx++ means for x it holds the value 11 and displays xx = 11 and x = 10.
y = yy; then yy becomes yy = 11 and y = 11
Display(); then display method is called
Display method contains cout<< x << " " << y; therfore it displays x= 10 and y = 11.
Note: X =11 and Y = 11 both are same when we came back in main. Values are display before came in main that's why the answer is B.
In main, x = 10, and y is refer value of x Therefore y = 10.
then objBix.SetValue(x , y) means we pass the x and y value to the SetValue function.
SetValue(int &xx, int &yy) &xx refers value of x and &yy refers value of y i.e &xx= 10, & yy = 10.
Then we enters into functioncSetValue,
function contains
x = xx ++; here post increment of xx++ means for x it holds the value 11 and displays xx = 11 and x = 10.
y = yy; then yy becomes yy = 11 and y = 11
Display(); then display method is called
Display method contains cout<< x << " " << y; therfore it displays x= 10 and y = 11.
Note: X =11 and Y = 11 both are same when we came back in main. Values are display before came in main that's why the answer is B.
Keerthana Naik said:
9 years ago
Still confusing. Can anybody please explain why is not option C?
(1)
Monika said:
1 decade ago
Its not option C because we are printing the value of x and why of class not of main using display.
Main is passing the address of x which is in main. So when x = xx++ is executing the value of x of main is 11 not the value of x of class.
So the x of class remains 10. After x = xx++ the value of x of main becomes 11.
But why of main or yy is referring to x of main so it becomes 11. And the expression y = yy; stores 11 in why of class.
Using display code is printing value of x and why of class not of main.
Main is passing the address of x which is in main. So when x = xx++ is executing the value of x of main is 11 not the value of x of class.
So the x of class remains 10. After x = xx++ the value of x of main becomes 11.
But why of main or yy is referring to x of main so it becomes 11. And the expression y = yy; stores 11 in why of class.
Using display code is printing value of x and why of class not of main.
Juthi said:
1 decade ago
Why the answer is not option C? Please give me some answer. I'm confused.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers