-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintroduction_to_templates
43 lines (25 loc) · 1005 Bytes
/
introduction_to_templates
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
#include <iostream>
using namespace std;
/* templates can be used to create generic function and class which can use any type of data to process operations.
syntax :
template <class T> => we can use "typename" instead of class
T function_name(T argument1, T argument2) {
do some operations;
}
Note : In the above function we can pass any type of data that's the main purpose of templates, createing generic programs
*/
template <class T>
T generic_addition(T number1, T number2){
return number1 + number2;
}
// main function
int main() {
//calling the function
int integer1 = 10;
int integer2 = 20;
float float1 = 10.3;
float float2 = 20.4;
//passing the arguments inside the same function
cout << "The integer addition value is " << generic_addition(integer1, integer2) << endl;
cout << "The float addition value is " << generic_addition(float1, float2);
}