-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path60funcintemp.cpp
More file actions
58 lines (51 loc) · 1.16 KB
/
Copy path60funcintemp.cpp
File metadata and controls
58 lines (51 loc) · 1.16 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
#include<iostream>
#include<cstring>
#include<fstream>
using namespace std;
/*
FUNCTION TEMPLATES :
This enables us to create functions that can take parameters in a non specified datatype and can be given any value in the main
Syntax:
template <class T1, class T2,...>
{{return-type}} function_name( T1 arg1, T2 arg2,...)
{
body...
}
*/
template <class K1, class K2>
float Average(K1 a, K2 b)
{
float avg = (a + b)/2.0;
return avg;
}
template <class K1>
K1 function(K1 a, K1 b)
{
return a + b;
}
template <class K1>
void swapp(K1 &a, K1 &b)
{
K1 temp = a;
a = b;
b = temp;
}
int main()
{
// Doesn't require <> declaration or parameter passing
float a;
a = Average(4, 7.4);
cout<<"Average of given numbers is : "<<a<<endl;
float b = function(1, 5);
cout<<endl<<"b is : "<<b<<endl;
b = function(1.4, 9.5);
cout<<endl<<"b is : "<<b<<endl;
b = function('3', 'i');
cout<<endl<<"b is : "<<b<<endl;
b = function(1.0494847, 5.7123789);
cout<<endl<<"b is : "<<b<<endl;
int x = 2, y = 7;
swapp(x, y);
cout<<endl<<x<<endl<<y<<endl;
return 0;
}