1 #include <iostream>
2 using namespace std;
3
4
5 void swap1(int n1, int n2)
6 {
7 int temp = n1;
8 n1 = n2;
9 n2 = temp;
10 }
11
12
13 void swap2(int& n1, int& n2)
14 {
15 int temp = n1;
16 n1 = n2;
17 n2 = temp;
18 }
19
20
21 void swap3(int* p1, int* p2)
22 {
23 int temp = *p1;
24 *p1 = *p2;
25 *p2 = temp;
26 }
27
28
29 void swap4(int* &p1, int* &p2)
30 {
31 int* temp = p1;
32 p1 = p2;
33 p2 = temp;
34 }
35
36 int main()
37 {
38
39 int num1 = 1;
40 int num2 = 2;
41
42 cout << "Before invoking the swap function, num1 is "
43 << num1 << " and num2 is " << num2 << endl;
44
45
46 swap1(num1, num2);
47
48 cout << "After invoking the swap function, num1 is " << num1 <<
49 " and num2 is " << num2 << endl;
50
51 cout << "Before invoking the swap function, num1 is "
52 << num1 << " and num2 is " << num2 << endl;
53
54
55 swap2(num1, num2);
56
57 cout << "After invoking the swap function, num1 is " << num1 <<
58 " and num2 is " << num2 << endl;
59
60 cout << "Before invoking the swap function, num1 is "
61 << num1 << " and num2 is " << num2 << endl;
62
63
64 swap3(&num1, &num2);
65
66 cout << "After invoking the swap function, num1 is " << num1 <<
67 " and num2 is " << num2 << endl;
68
69 int* p1 = &num1;
70 int* p2 = &num2;
71 cout << "Before invoking the swap function, p1 is "
72 << p1 << " and p2 is " << p2 << endl;
73
74
75 swap4(p1, p2);
76
77 cout << "After invoking the swap function, p1 is " << p1 <<
78 " and p2 is " << p2 << endl;
79
80 return 0;
81 }