0
0
C++programming~15 mins

Reference declaration in C++ - Mini Project: Build & Apply

Choose your learning style9 modes available
Reference Declaration in C++
📖 Scenario: Imagine you have a number stored in a box. You want to create another name for the same box so you can use either name to change or see the number inside.
🎯 Goal: You will create a reference to an integer variable and then change the value using the reference. Finally, you will print the value to see the effect.
📋 What You'll Learn
Create an integer variable named original with the value 10.
Create a reference variable named ref that refers to original.
Change the value of original using the reference ref to 20.
Print the value of original.
💡 Why This Matters
🌍 Real World
References are used in C++ to avoid copying large data and to allow functions to modify variables directly.
💼 Career
Understanding references is important for writing efficient and clear C++ code in software development jobs.
Progress0 / 4 steps
1
Create an integer variable
Create an integer variable called original and set it to 10.
C++
Need a hint?

Use int to declare a number and assign 10 to original.

2
Create a reference to the integer
Create a reference variable called ref that refers to the variable original.
C++
Need a hint?

Use int& to declare a reference to original.

3
Change the value using the reference
Change the value of original to 20 using the reference variable ref.
C++
Need a hint?

Assign 20 to ref to change original.

4
Print the value of the original variable
Write a std::cout statement to print the value of original.
C++
Need a hint?

Use std::cout << original << std::endl; to print the value.