• Home
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • React.js
    • Introduction to React
    • React Developer
  • Python
    • Introduction to Python
    • Python Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer
  • Zig
    • Introduction to Zig
    • Zig Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer
  • Interactive Training
  • Pricing
  • Brainstorm
STEMTrainingGrounds
  • Courses
    • Home
    • JavaScript
      • Introduction to JavaScript
      • JavaScript Developer
    • TypeScript
      • Introduction to TypeScript
      • TypeScript Developer
    • React
      • Introduction to React
      • React Developer
    • Python
      • Introduction to Python
      • Python Developer
    • C++
      • Introduction to C++
      • C++ Developer
    • C Language
      • Introduction to C
      • C Developer
    • Rust
      • Introduction to Rust
      • Rust Developer
    • Zig
      • Introduction to Zig
      • Zig Developer
    • Linux Shell
      • Introduction to the Linux Shell
      • Linux Shell Developer
  • Interactive Training
  • Pricing
  • Reading Grounds

Quick Links

  • About Us
  • Pricing
  • Partnership
  • Brainstorm
  • Terms
  • Privacy
  • Refunds
  • Featured on DanielLaunches

Courses

  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • React
    • Introduction to React
    • React Developer
  • Python
    • Introduction to Python
    • Python Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer
  • Zig
    • Introduction to Zig
    • Zig Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer

Newsletter

Subscribe to our free monthly newsletter, for a quick update on Python, JavaScript, and React news

© 2025 - 2026 STEMTrainingGrounds. All Rights Reserved.

C++ Developer -

Lesson 2 of 46

Lesson Progress: 0%

Lesson Progress: 0%
Lesson Incomplete
← Previous: Vectors
Lesson 2 of 46
Next: Pointers →

A reference in C++ is an alias for an existing variable. Once you bind a reference to a variable, you can use the reference name just like the original. References cannot be null and must be initialized when declared. They provide a simpler and safer alternative to pointers for many tasks. This lesson covers declaring references, modifying values through references, and how multiple references can point to the same variable. Understanding references is an important step toward writing effective C++ code.

Code Example
#include <iostream>
using namespace std;

int main() {
    int x = 10;
    int& ref = x;
    cout << x << " " << ref;
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • A reference is declared using int& ref = x — the & after the type marks it as a reference.
  • ref and x refer to the same memory location.
  • Printing both shows the same value because they are the same variable.
  • A reference must be initialized when it is declared.
Code Example
#include <iostream>
using namespace std;

int main() {
    int x = 10;
    int& ref = x;
    ref = 25;
    cout << x;
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Assigning to a reference changes the original variable.
  • Writing ref = 25 updates x because ref is just another name for x.
  • The reference itself cannot be changed to refer to a different variable.
  • References make code more readable by avoiding pointer syntax.
Code Example
#include <iostream>
using namespace std;

int main() {
    int x = 7;
    int& r1 = x;
    int& r2 = x;
    r1 = 14;
    cout << r2;
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Multiple references can be bound to the same variable.
  • r1 and r2 are both aliases for x.
  • Changing r1 updates x, and reading r2 reflects the new value.
  • All references to the same variable share one memory location.
Code Example
#include <iostream>
using namespace std;

int main() {
    double pi = 3.14;
    double& ref = pi;
    cout << ref;
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • References work with any data type, including double.
  • The reference ref acts exactly like the original pi.
  • No copying occurs — ref directly accesses the same memory.
  • Using references with large objects avoids expensive copy operations.
Code Example
#include <iostream>
using namespace std;

int main() {
    string msg = "Hi";
    string& ref = msg;
    cout << ref;
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • References can be used with any type, including string.
  • The reference ref is an alias for msg.
  • Printing ref outputs the value of the original msg.
  • This shows that references work for user-friendly types too.
Code Example
#include <iostream>
using namespace std;

int main() {
    int x = 5;
    int& ref = x;
    x = x + 10;
    cout << ref;
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Changing the original variable is visible through the reference.
  • After x = x + 10, x becomes 15, so ref outputs 15.
  • The reference stays bound to the same variable at all times.
  • Any modification to the original is immediately reflected through the reference.

Test Incomplete

What is a reference in C++?

Question #

1/15

Score

0/0 - 0.0 %