/*
* Copyright ©2026 Soham Pardeshi. All rights reserved.
* Permission is hereby granted to students registered for University of
* Washington CSE 333 for use solely during Summer Quarter 2026 for
* purposes of the course. No other use, copying, distribution, or
* modification is permitted without prior written consent. Copyrights
* for third-party components of this work must be honored. Instructors
* interested in reusing these course materials should contact the author.
*/
#include <iostream> // for std::cout, std::endl
#include <cstdlib> // for EXIT_SUCCESS
using std::cout;
using std::endl;
// A reference is a second NAME for an existing variable: one box, two
// names. Unlike a pointer, it has no address of its own and cannot be
// re-bound to a different variable after it is initialized.
int main(int argc, char** argv) {
int x = 5, y = 10;
int& z = x; // binds the name z to x (NOT a copy, NOT a pointer)
z += 1; // sets z (and therefore x) to 6
x += 1; // sets x (and therefore z) to 7
z = y; // does NOT re-bind z; copies y's VALUE (10) into x
z += 1; // sets z (and therefore x) to 11; y stays 10
cout << "x = " << x << ", y = " << y << ", z = " << z << endl; // 11, 10, 11
return EXIT_SUCCESS;
}
// Compile with:
// g++ -Wall -g -std=c++17 -o reference reference.cc