Q:

How to create and use a reference variable in C++?

0

How to create and use a reference variable in C++?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

Let see an example, where I am creating an integer variable and assigning 6 to it. In the second step, I am creating an integer reference and initializing it by data. Now you can see when I am changing the value of the reference, the value of the data is also changing.

#include <iostream>
using namespace std;
int main()
{
    //create an variable
    int data = 6;
    //rOffData refer to data
    int& rOffData = data;
    //print data and rOffData
    cout <<"rOffData = "  << rOffData << endl ;
    cout <<"data = "  << data << endl ;
    // Assign 27 to the rOffData
    rOffData = 27;
    //print data and rOffData
    cout << "After change value of rOffData" << endl;
    cout <<"rOffData = "  << rOffData << endl ;
    cout <<"data = "  << data << endl ;
    return 0;
}

Output:

rOffData = 6

data = 6

After change value of rOffData

rOffData = 27

data = 27

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Interview Questions and Answers(2022)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What is the difference between a pointer and a ref... >>
<< What is the difference between function overloadin...