C++ Programming - References - Discussion

Discussion Forum : References - Programs (Q.No. 18)
18.
Which of the following statement is correct about the program given below?
#include<iostream.h> 
class IndiaBix
{
    int x, y; 
    public:
    void SetValue(int &a, int &b)
    {
        a = 100;
        x = a;
        y = b;
        Display();
    }
    void Display()
    {
        cout<< x << " " << y; 
    }
};
int main()
{
    int x = 10;
    IndiaBix objBix;
    objBix.SetValue(x, x);
    return 0;
}
The program will print the output 100 10.
The program will print the output 100 100.
The program will print the output 100 garbage.
The program will print two garbage values.
It will result in a compile time error.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
3 comments Page 1 of 1.

Sowmiya said:   7 years ago
Call by value does not change the variable value but call by reference change the value of variable.

Tarun N said:   8 years ago
Actually, when we are passing the value of 'x', the function rather taking the value, it takes the address of 'x'. So, therefore a, b are two references to the same variable. Therefore when we change the value of 'a' to 100, it reflects on the variable it is referenced to i.e., 'x'.

As, we know that a and b are references to the same variable i.e., x whose value is modified to 100, thus the output is 100 100.
(1)

Naresh Garg said:   10 years ago
According to the Turbo C compiler, it will print the value 100, 100.

So simple passing the value 10, 10 after that it is override the 10 to 100 and assign to x but that value of b is 100 because the value is same i.e. 10, 10 it picked the previous value.

If it is passed second parameter y = 11 it will print the 100, 11.

Post your comments here:

Your comments will be displayed after verification.