-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplates.cpp
More file actions
98 lines (69 loc) · 1.64 KB
/
templates.cpp
File metadata and controls
98 lines (69 loc) · 1.64 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
/** This will give Error because of ambiguity ( we are using namespace std and in that we have our standard "max" function. so compiler is confused which "max" to used. *****
template<typename T>
inline T const max ( T a , T b )
{
return ( a > b ) ? a : b ;
}
*/
/******* TEMPLATES for MAX , MIN , EQUAL *********/
template<typename T>
inline T const &max ( T &a , T &b )
{
return ( a > b ) ? a : b ;
}
template<typename T>
inline T const &min ( T &a , T &b )
{
return ( a < b ) ? a : b ;
}
template<typename T>
inline T const &equal ( T &a , T &b )
{
return ( a == b );
}
/********** Class Templates ***********/
template<class T>
class student
{
public:
T x , y , z;
};
/********** Template with 2 different typenames *******/
template<typename T1 , typename T2>
struct simplePair
{
T1 f;
T2 s;
simplePair(T1 a , T2 b ) { f = a ; s = b ; };
};
/******** Templates with a non - type ********/
template < size_t num_of_times >
void printit(const string& str)
{
for(int i = 0 ; i < num_of_times; i++)
cout << str << "\n";
}
int main()
{
/*********Basic Template ************/
cout << max ( 5 , 6 ) << "\n";
cout << max ( 5.1 , 62.01 ) << "\n";
cout << min( 5, 6 ) << "\n";
/********* Class template *********/
student<int> st1 ;
st1.x = 1;
st1.y = 2;
st1.z = 3;
student<string> st2;
st2.x = "JK";
/********* Using two typenames **********/
simplePair <string , int> pair1 ("Lemon" , 10 );
cout << pair1.f << " | " << pair1.s << "\n";
/********** Templates for a non - type ***********/
printit<5>("J1K7_7");
return 0;
}