1: // Filename: PTR_SWAP.CPP 2: // Purpose: Illustrates the passing of pointer parameters. 4: #include <iostream> 5: using namespace std; 7: #include "PAUSE.H" 10: void Swap(/* inout */ int& a, /* inout */ int& b) 11: // Pre: "a" and "b" have been initialized. 12: // Post: The values of "a" and "b" have been swapped. 13: { 14: int temp = a; 15: a = b; 16: b = temp; 17: } 20: void Swap(/* inout */ int* ap, /* inout */ int* bp) 21: // Pre: "ap" and "bp" contain the addresses of integer values. 22: // Post: The integer values in the locations pointed to by ap 23: // have been swapped. 24: { 25: int temp = *ap; 26: *ap = *bp; 27: *bp = temp; 28: } 31: int main() 32: { 33: cout << "\nThis program show the connection between pointer parameters" 34: << "\nand reference parameters." 35: << "\nStudy the source code and the output simultaneously.\n\n"; 37: int m = 7; 38: int n = -3; 40: cout << endl; 41: cout << "Original values of m and n: " << m << " " << n << endl; 42: Pause(29); 44: // This call uses the Swap with reference parameters: 45: Swap(m, n); 46: cout << "Swapped values of m and n: " << m << " " << n << endl; 47: Pause(29); 49: // This call uses the Swap with pointer parameters: 50: Swap(&m, &n); 51: cout << "And then swapped back again: " << m << " " << n << endl; 52: Pause(29); 54: return 0; 55: }