// ptr17.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program is a simple example of the use of References. In specific, // the program shows how references and pointers could be related // Key Points: References and Pointers #include #include void UselessSwap( int a , int b ) { int tmp = a; a = b; b = tmp; } void RealSwapByPointer( int *a, int *b ) { int tmp = *a; *a = *b; *b = tmp; } void RealSwapByReference( int& a , int& b ) { int tmp = a; a = b; b = tmp; } int main() { int x = 10, y = 7; int *x_ptr = &x, *y_ptr = &y; cout << " x is: " << x << "\n"; cout << " y is: " << y << "\n"; UselessSwap( x , y ); cout << " The value of x after Useless Swap is: " << x << "\n"; cout << " The value of y after Useless Swap is: " << y << "\n\n"; RealSwapByPointer( x_ptr , y_ptr ); cout << " The value of x after Swap using Pointers is: " << x << "\n"; cout << " The value of y after Swap using pointers is: " << y << "\n\n"; RealSwapByReference( x , y ); cout << " The value of x after Swap using References is: " << x << "\n"; cout << " The value of y after Swap using References is: " << y << "\n"; return 0; } // The following is the result of running the program /* x is: 10 y is: 7 The value of x after Useless Swap is: 10 The value of y after Useless Swap is: 7 The value of x after Swap using Pointers is: 7 The value of y after Swap using pointers is: 10 The value of x after Swap using References is: 10 The value of y after Swap using References is: 7 */