-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSession18.cpp
More file actions
43 lines (32 loc) · 826 Bytes
/
Session18.cpp
File metadata and controls
43 lines (32 loc) · 826 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
37
38
39
40
41
42
43
// Templates in General
#include<iostream>
using namespace std;
// You create a template so that it can accommodate any data type
template <typename T> // this is syntax to create a template. T can be any name of your choice
// without template we need to create 3 methods to compare int floats and characters like below:
/*int getMax(int a, int v.begin(), v.end()){
if (a>b){
return a;
}else{
return b;
}
}
Similarly for float and char...
*/
// But with template we create a single method and different data types can be accommodated
T getMax(T a, T b){
if (a>b){
return a;
}else{
return b;
}
}
int main(){
int iMax = getMax(10, 20);
float fMax = getMax(2.2, 3.3);
char cMax = getMax('A','Z');
cout<<"iMax: "<<iMax<<"\n";
cout<<"fMax: "<<fMax<<"\n";
cout<<"cMax: "<<cMax<<"\n";
return 0;
}