-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This program is used to learn the function template and display
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//The function template and display | ||
#include <iostream> | ||
//define the function | ||
template <typename T> | ||
void Swap(T &a, T &b); | ||
struct job | ||
{ | ||
char name[20]; | ||
double salary; | ||
int floor; | ||
}; | ||
//define the display | ||
template <>void Swap<job>(job &j1, job &j2); | ||
void Show(job &j); | ||
using namespace std; | ||
int main() | ||
{ | ||
//cout.precision(4); | ||
//cout.setf(ios::fixed); | ||
int i = 10, j = 20; | ||
cout << "i,j=" << i << "," << j << endl; | ||
cout << "Using compiler generated int swapper:" << endl; | ||
Swap(i, j); | ||
cout << "Now i,j=" << i << "," << j << endl; | ||
job s1 = { "Susan Yaffee",73000.60,7 }; | ||
job s2 = { "Sidney Taffee",78060.72,9 }; | ||
cout << "Before job swapping:" << endl; | ||
Show(s1); | ||
Show(s2); | ||
Swap(s1, s2); | ||
cout << "After job swapping:" << endl; | ||
Show(s1); | ||
Show(s2); | ||
cin.get(); | ||
return 0; | ||
} | ||
//struct of the exchange | ||
template <typename T> | ||
void Swap(T &a, T &b) | ||
{ | ||
T temp; | ||
temp = a; | ||
a = b; | ||
b = temp; | ||
} | ||
//the exchange of variables in different structs | ||
template <>void Swap<job>(job &j1, job &j2) | ||
{ | ||
double t1; | ||
int t2; | ||
t1 = j1.salary; | ||
j1.salary = j2.salary; | ||
j2.salary = t1; | ||
t2 = j1.floor; | ||
j1.floor = j2.floor; | ||
j2.floor = t2; | ||
} | ||
void Show(job &j) | ||
{ | ||
cout << j.name << ":$" << j.salary << " on floor " << j.floor << endl; | ||
} |