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;
}
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.
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