1: //swap1.cpp
2: //Illustrates an overloaded Swap function.
4: #include <iostream>
5: #include <string>
6: using namespace std;
9: void Swap
10: (
11: int& first, //inout
12: int& second //inout
13: )
14: /**<
15: Swaps the values in two variables.
16: @pre <em>first</em> and <em>second</em> have been initialized.
17: @post The values in <em>first</em> and <em>second</em have
18: been interchanged.
19: */
20: {
21: int temp = first;
22: first = second;
23: second = temp;
24: }
27: void Swap
28: (
29: double& first, //inout
30: double& second //inout
31: )
32: /**<
33: Swaps the values in two variables.
34: @pre <em>first</em> and <em>second</em> have been initialized.
35: @post The values in <em>first</em> and <em>second</em have
36: been interchanged.
37: */
38: {
39: double temp = first;
40: first = second;
41: second = temp;
42: }
45: void Swap
46: (
47: string& first, //inout
48: string& second //inout
49: )
50: /**<
51: Swaps the values in two variables.
52: @pre <em>first</em> and <em>second</em> have been initialized.
53: @post The values in <em>first</em> and <em>second</em have
54: been interchanged.
55: */
56: {
57: string temp = first;
58: first = second;
59: second = temp;
60: }
63: int main()
64: {
65: int a = 7, b = 9;
66: double x = 2.3, y = 7.4;
67: string firstName = "John", lastName = "Henry";
69: cout << "\na is " << a << " and b is " << b << ".\n";
70: Swap(a, b);
71: cout << "Now a is " << a << " and b is " << b << ".\n\n";
73: cout << "x is " << x << " and y is " << y << ".\n";
74: Swap(x, y);
75: cout << "Now x is " << x << " and y is " << y << ".\n\n";
77: cout << "firstName is " << firstName
78: << " and lastName is " << lastName << ".\n";
79: Swap(firstName, lastName);
80: cout << "Now firstName is " << firstName
81: << " and lastName is " << lastName << ".\n\n";
83: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
84: }