References 


Contents of this section:

  1. A simple program demonstrating references
  2. Function: passing by references

1. A simple program demonstrating references

    Code Listing: 

// File name: ~ftp/pub/class/cplusplus/Reference/RefSimple.cpp
// Purpose:   A simple program demonstrating the use of reference

#include <iostream>
using namespace std;

int main()
{
	int Len, Wid;     // declare int variables

	// Create references to int variables.
        // Now rLen and Len are aliases to each other,
        // and rWid and Wid are also aliases to each other.
	int &rLen = Len;
	int &rWid = Wid; 
	
	// Initialized the two int variables
	Len = 10;         // rLen is also initialized to be 10
	Wid = 20;         // rWid is also initialized to be 20

	// Printing out the values for int and int references
	cout << "Len is: " << Len << ", and  Wid is: " << Wid << endl;
	cout << "rLen is: " << rLen << ", and rWid is: " << rWid << endl;
	cout << endl;
	
	// Printing out the address of int and references to int
	cout << "Address of Len is: " << &Len << endl;
	cout << "Address of rLen is: "  << &rLen << endl;
	if(&Len == &rLen)
	{
		cout << "Address of Len is equal to address of rLen!" << endl;
	}
	cout << "Address of Wid is: " << &Wid << endl;
	cout << "Address of rWid is: "  << &rWid << endl;
	if(&Wid == &Wid)
	{
		cout << "Address of Wid is equal to address of rWid!" << endl;
	}

	return 0;
}

    Running session:

mercury[39]% CC -LANG:std RefSimple.cpp -o RefSimple
mercury[40]% RefSimple
Len is: 10, and  Wid is: 20
rLen is: 10, and rWid is: 20

Address of Len is: 2147429924
Address of rLen is: 2147429924
Address of Len is equal to address of rLen!
Address of Wid is: 2147429932
Address of rWid is: 2147429932
Address of Wid is equal to address of rWid!
mercury[41]% 

2. Function: passing by references

Code listing:

// File name: ~ftp/pub/class/cplusplus/Reference/RefFunction.cpp
// Purpose:   Demonstrates pass by reference in a function

#include <iostream>
using namespace std;

void swap(int &i, int &j);  // function prototype for swapping two values

int main()
{
	int NumOne = 0;
	int NumTwo = 0;

	cout << "Please enter two integers: " << endl;

	cout << "Enter value for NumOne: " ;
	cin >> NumOne;

	cout << "Enter value for NumTwo: " ;
	cin >> NumTwo;

	cout << "Before swapping, NumOne is: " << NumOne << endl;
	cout << "Before swapping, NumTwo is: " << NumTwo<< endl;

	swap(NumOne, NumTwo);

	cout << "After swapping, NumOne is: " << NumOne << endl;
	cout << "After swapping, NumTwo is: " << NumTwo<< endl;

	return 0;
}

// function definition for swap()

void swap(int &i, int &j)
{
	int temp;

	temp = i;
	i = j;
	j = temp;

}

Running session:

mercury[51]% CC -LANG:std RefFunction.cpp -o RefFunction
mercury[52]% RefFunction
Please enter two integers: 
Enter value for NumOne: 10
Enter value for NumTwo: 20
Before swapping, NumOne is: 10
Before swapping, NumTwo is: 20
After swapping, NumOne is: 20
After swapping, NumTwo is: 10
mercury[53]% 


Last modified: Sunday, 25-Mar-2012 17:40:55 CST
Copyright 2000 Department of Computer Science, University of Regina.

 CS Dept Home Page

Teaching Material

C++ Index Page