0% found this document useful (0 votes)
0 views5 pages

Pointer & Reference 3

The document explains how to create and use references in C++, including syntax and examples. It highlights that a reference acts as an alias for a variable, allowing access and modification of the original variable. Key points include the placement of the ampersand sign, the necessity of initializing references, and the immutability of a reference to point to a different variable once established.

Uploaded by

daudndihagati
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views5 pages

Pointer & Reference 3

The document explains how to create and use references in C++, including syntax and examples. It highlights that a reference acts as an alias for a variable, allowing access and modification of the original variable. Key points include the placement of the ampersand sign, the necessity of initializing references, and the immutability of a reference to point to a different variable once established.

Uploaded by

daudndihagati
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Thank you for printing our content at [Link].

Please check back soon for new


contents.
o/learn/master-cpp?
[Link]&utm_medium=referral&utm_audience=ORGANIC-
Search...
(/)
C++ References
paign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_floating_button)
[Link]

In C++, we use a reference to create an alias for a variable. We can use the
reference variable to access or modify the variable.

Create a C++ Reference


We use the ampersand sign to create a reference. For example,

string& ref_city = city;

Here,

string - datatype of the variable

& - denotes we are creating a reference

ref_city - name of the reference variable

city - the variable for which reference is created


Example:
Thank C++
you for printing our Reference
content at [Link]. Please check back soon for new
contents.
o/learn/master-cpp?
#include <iostream>
[Link]&utm_medium=referral&utm_audience=ORGANIC-
Search...
(/)
paign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_floating_button)
using namespace std; [Link]

int main() {

string city = "Paris";

// create a reference to the variable


string& ref_city = city;

// display the variable


cout << "Variable Value: " << city << endl;
cout << "Reference Value: " << ref_city << endl;

return 0;
}

Run Code (/cpp-programming/online-compiler)

Output

Variable Value: Paris


Reference Value: Paris

In the above example, we have used the reference variable ref_city to display
the value of the variable city .

Modify Variables Using References


We can modify a variable by simply assigning a new value to the reference
variable. For example,
Thank you for printing our content at [Link]. Please check back soon for new
#include <iostream>
contents.
o/learn/master-cpp?
using namespace std;
[Link]&utm_medium=referral&utm_audience=ORGANIC-
Search...
(/)
paign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_floating_button)
int main() { [Link]

string city = "Paris";

// create a reference to the variable


string& ref_city = city;

// display the variable


cout << "Variable Value = " << city << endl;
cout << "Reference Value = " << ref_city << endl;

// modify the variable using reference


ref_city = "New York";

// display the variable


cout << endl << "After Modification: " << endl;
cout << "Variable Value = " << city << endl;
cout << "Reference Value = " << ref_city << endl;

return 0;
}

Run Code (/cpp-programming/online-compiler)

Output

Variable Value: Paris


Reference Value: Paris

After Modification:
Variable Value: = New York
Reference Value: New York

Important Points
1. Placement of the & sign
We
Thank can
you for place
printingthe sign with
our &content data type or with a variable
at [Link]. whileback
Please check creating a new
soon for
contents.
reference. However, the standard practice is to use the sign along with the data
o/learn/master-cpp?
type. For example,
[Link]&utm_medium=referral&utm_audience=ORGANIC-
Search...
(/)
paign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_floating_button)
[Link]
// create a variable
string city = "Paris";

// valid but not a standard practice


string &ref_city = city;

// valid and a standard practice


sring& ref_city = city;

2. References Initialization

We must initialize references at the time of declaration.

// create a variable
string city = "Paris";

// incorrect code [reference not initialized]


string& ref_city;
ref_city = city;

// correct code
string& ref_city = city;

3. Reference With a New Variable


Once
Thank weprinting
you for createour
a reference
content at to a variable, it cannot Please
[Link]. be changed to refer
check back soontofor new
contents.
another variable. For example,
o/learn/master-cpp?
[Link]&utm_medium=referral&utm_audience=ORGANIC-
Search...
(/)
paign=course_promotion&utm_content=interests_learn_cpp&utm_term=nav_floating_button)
#include <iostream>
[Link]
using namespace std;

int main() {

string city1 = "Paris";

// create a reference to the variable


string& ref_city = city1;

// display the variable


cout << "city1 = " << city1 << endl;
cout << "ref_city = " << ref_city << endl;

string city2 = "New York";

// trying to modify the ref_city reference variable to refer to city2


// but it assigns the value of city2 to the variable city1
ref_city = city2;

// display the variables


cout << endl << "city1 = " << city1 << endl;
cout << "city2 = " << city2 << endl;
cout << "ref_city = " << ref_city << endl;

return 0;
}

Run Code (/cpp-programming/online-compiler)

Output

city1 = Paris
ref_city = Paris

city1 = New York


city2 = New York
ref_city = New York

You might also like