-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtemplateFunc.cpp
56 lines (38 loc) · 859 Bytes
/
templateFunc.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
using namespace std;
/*
Templates are expended at compiler time. This is like macros. The difference is, compiler does type checking before template expansion.
The idea is simple, source code contains only function/class, but compiled code may contain multiple copies of same function/class.
*/
template<typename T>
T Swap(T *a, T *b) {
T temp = *a;
*a=*b;
*b=temp;
}
template<class T>
T max(T a, T b , T c) {
if(a > b && a > c) return a;
if(b>a && b>c) return b;
if(c>a && c>b) return c;
}
int main ()
{
// int a=10,b=20;
// string a1 = "Anish", b1 = "mrinal";
//
// //call by reference
// Swap(&a,&b);
//
// cout<<"a :"<<a<<" | "<<"b :"<<b<<endl;
//
// Swap(&a1,&b1);
//
//
// cout<<a1<<endl;
// cout<<b1<<endl;
cout<<max<int>(10,12,100);
cout<<endl;
cout<<max<char>('a','b','z');
return 0;
}