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;
}
Discussion:
3 comments Page 1 of 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.
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.
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.
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)
Sowmiya said:
7 years ago
Call by value does not change the variable value but call by reference change the value of variable.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers