-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic_programming_lesson9.cpp
67 lines (45 loc) · 1.34 KB
/
generic_programming_lesson9.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
57
58
59
60
61
62
63
64
65
66
67
//Note-1:
/* Wouldn't it be nice to just write a function once and use it over and over for different variable types?
Well, actually, we can do that.
C++ allows us to create generic functions using templates.
*/
/*
The syntax for templates for functions:
The function declaration:
template <typename T> //tell the compiler we are using a template
//T represents the variable type. Since we want it to be for any type, we
//use T
T functionName (T parameter1,T parameter2, ...);
The function definition:
template <typename T>
T functionName (T parameter1,T parameter2,...)
{
function statements;
}
*/
//very good explanation: https://www.youtube.com/watch?v=ttOM3HulFyU
//Note-2:
/* when the types are different.
We will still use the same form, we will just need to tell
the compiler that different variable types are allowed.*/
#include<iostream>
using namespace std;
template <typename T, typename U>
T getBigger(T input1, U input2);
int main()
{
int a = 5;
float b = 6.334;
int bigger;
cout<<"Between "<<a<<" and "<<b<<" "<<getBigger(a,b)<<" is bigger.\n";
cout<<"Between "<<a<<" and "<<b<<" "<<getBigger(b,a)<<" is bigger.\n";
return 0;
}
template <typename T, typename U>
T getBigger(T input1, U input2)
{
if(input1 > input2)
return input1;
return input2;
}
//I will continue on that subject