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; 
}
The program will print the output 10 10.
The program will print the output 10 11.
The program will print the output 11 11.
The program will print the output 11 10.
It will result in a compile time error.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
4 comments Page 1 of 1.

Juthi said:   1 decade ago
Why the answer is not option C? Please give me some answer. I'm confused.

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.

Keerthana Naik said:   9 years ago
Still confusing. Can anybody please explain why is not option C?
(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.

Post your comments here:

Your comments will be displayed after verification.