Interview Questions Answers.ORG
Interviewer And Interviewee Guide
Interviews
Quizzes
Home
Quizzes
Interviews C Plus Plus Language Interviews:C++ Access ControlC++ COM ActiveXC++ ConstructorsC++ ContainersC++ Exception HandlingC++ FriendC++ InheritanceC++ Inline FunctionC++ New And DeleteC++ Operator OverloadingC++ Pointers & FunctionsC++ ProgrammerC++ ReferencesC++ Static DataC++ SyntaxC++ TemplateC++ Type CheckingC++ Virtual Functions
Copyright © 2018. All Rights Reserved
C++ Programmer Interview Question:
Explain how to create a reference variable in C++?
Submitted by: MuhammadAppending an ampersand (&) to the end of datatype makes a variable eligible to use as reference variable.
int a = 20;
int& b = a;
The first statement initializes a an integer variable a. Second statement creates an integer reference initialized to variable a
Take a look at the below example to see how reference variables work.
int main ()
{
int a;
int& b = a;
a = 10;
cout << "Value of a : " << a << endl;
cout << "Value of a reference (b) : " << b << endl;
b = 20;
cout << "Value of a : " << a << endl;
cout << "Value of a reference (b) : " << b << endl;
return 0;
}
Above code creates following output.
Value of a : 10
Value of a reference (b) : 10
Value of a : 20
Value of a reference (b) : 20
Submitted by: Muhammad
int a = 20;
int& b = a;
The first statement initializes a an integer variable a. Second statement creates an integer reference initialized to variable a
Take a look at the below example to see how reference variables work.
int main ()
{
int a;
int& b = a;
a = 10;
cout << "Value of a : " << a << endl;
cout << "Value of a reference (b) : " << b << endl;
b = 20;
cout << "Value of a : " << a << endl;
cout << "Value of a reference (b) : " << b << endl;
return 0;
}
Above code creates following output.
Value of a : 10
Value of a reference (b) : 10
Value of a : 20
Value of a reference (b) : 20
Submitted by: Muhammad
Copyright 2007-2024 by Interview Questions Answers .ORG All Rights Reserved.
https://InterviewQuestionsAnswers.ORG.
https://InterviewQuestionsAnswers.ORG.