C++ Programming - References - Discussion

Discussion Forum : References - General Questions (Q.No. 15)
15.
Which of the following statement is correct?
A reference is declared using * operator.
Once a reference variable has been defined to refer to a particular variable it can refer to any other variable.
A reference must always be initialized within classes.
A variable can have multiple references.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
4 comments Page 1 of 1.

Shrawan said:   3 years ago
@All.

According to me, the coding part is;

#include <iostream>
using namespace std;
int main()
{
int x=20,a=10;
int &b=a;
cout<<"\n\n Value of b = "<<b;
b=x;
cout<<"\n\n Value of b = "<<b;
cout<<"\n\n Value of a = "<<a;

cout<<"\n\n address of b = "<<&b;
cout<<"\n\n address of a = "<<&a;
cout<<"\n\n address of x = "<<&x;

return 0;
}

Krushna said:   5 years ago
How? Explain in detail.

Rishabh. said:   8 years ago
@Arko.

It is a eference to the same variable only the value stored is changing.

Arko / Judhajit said:   1 decade ago
Option B is also correct.(According to this working code)

#include<iostream.h>
int main()
{
int x=20,a=10;
int &b=a;
cout<<"\n\n Value of b = "<<b;
b=x;
cout<<"\n\n Value of b = "<<b;
return 0;
}

OUTPUT:

Value of b = 10.
Value of b = 20.

Hence, "Once a reference variable(variable 'b' in this case) has been defined to refer to a particular variable(variable 'a') it can refer to any other variable(variable'x')." - is TRUE!.
(1)

Post your comments here:

Your comments will be displayed after verification.