1: //swap.cpp
2: //Illustrates function overloading and the standard
3: //algorithm for exchanging the values of two variables.
5: #include <iostream>
6: #include <iomanip>
7: using namespace std;
10: void DescribeProgram();
11: void Swap(int& i1, int& i2);
12: void Swap(char& c1, char& c2);
13: void Pause();
14: void Pause(int indentLevel);
17: int main()
18: {
19: DescribeProgram();
21: //The (deliberate) naming of the following variables here in "main"
22: //and the naming of the formal function parameters in the function
23: //definitions below emphasizes that formal function parameters may
24: //(or may not) have the same names as the corresponding actual
25: //parameters in subsequent parmeter calls.
26: int i1, i2;
27: char first, second;
29: cout << "Enter two integers: ";
30: cin >> i1 >> i2; cin.ignore(80, '\n'); cout << endl;
31: cout << "In the order entered, the integers are: ";
32: cout << i1 << ", " << i2 << endl;
33: Pause(7);
34: Swap(i1, i2); //Actual/formal parameters have same names
35: cout << "And here are the two integers reversed: ";
36: cout << i1 << ", " << i2 << endl;
37: Pause(7);
39: cout << endl;
41: cout << "Enter two characters: ";
42: cin >> first >> second; cin.ignore(80, '\n'); cout << endl;
43: cout << "In the order entered, the characters are: ";
44: cout << first << ", " << second << endl;
45: Pause();
46: cout << endl;
47: Swap(first, second); //Actual/formal parameters have different names
48: cout << "And here are the two integers reversed: ";
49: cout << first << ", " << second << endl;
50: Pause();
52: cout << endl;
53: }
56: void DescribeProgram()
57: //Pre: The cursor is at the left margin.
58: //Post: The program description has been displayed,
59: // preceded and followed by at least one blank line.
60: {
61: cout << "\nThis program illustrates the standard \"two-value "
62: "exchange algorithm\" and\nfunction overloading. It is "
63: "necessary to study both the source code and\nthe output "
64: "simultaneously, but in the meantime, just follow "
65: "instructions.\n\n";
66: }
69: void Swap(/* inout */ int& i1, /* inout */ int& i2)
70: //Pre: "i1" and "i2" have been initialized.
71: //Post: The values of "i1" and "i2" have been swapped.
72: {
73: int temp = i1;
74: i1 = i2;
75: i2 = temp;
76: }
79: void Swap(/* inout */ char& c1, /* inout */ char& c2)
80: //Pre: "c1" and "c2" have been initialized.
81: //Post: The values of "c1" and "c2" have been swapped.
82: {
83: char temp = c1;
84: c1 = c2;
85: c2 = temp;
86: }
89: void Pause()
90: //Pre: The input stream cin is empty.
91: //Post: The program has displayed a one-line message beginning in
92: // column 1 and then paused, and the user has pressed Enter.
93: {
94: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
95: }
98: void Pause(/* in */ int indentLevel)
99: //Pre: The input stream cin is empty, and 0 <= indentLevel <= 50.
100: //Post: The program has displayed a one-line message beginning in
101: // column indentLevel+1 and then paused. The user has pressed
102: // Enter, and a blank line has been inserted in the output stream.
103: {
104: cout << setw(indentLevel) << "" << "Press Enter to continue ... ";
105: cin.ignore(80, '\n');
106: cout << endl;
107: }