-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuntemp.cpp
More file actions
36 lines (31 loc) · 730 Bytes
/
funtemp.cpp
File metadata and controls
36 lines (31 loc) · 730 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// funtemp.cpp -- using a function template
#include <iostream>
template <class Any> // or typename Any
void Swap(Any &a, Any &b);
// same to extend <T> in Java?
int main()
{
using namespace std;
int i = 10;
int j = 20;
cout << "i, j = " << i << ". " << j << ".\n";
cout << "Using compiler-generated int swapper: \n";
Swap(i, j);
cout << "Now i, j = " << i << ". " << j << ".\n";
double x = 24.5;
double y = 81.7;
cout << "x, y = " << x << ". " << y << ".\n";
cout << "Using compiler-generated double swapper: \n";
Swap(x, y);
cout << "Now x, y = " << x << ". " << y << ".\n";
return 0;
}
// function template defintion
template <class Any>
void Swap(Any &a, Any &b)
{
Any temp;
temp = a;
a = b;
b = temp;
}