-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program102.cpp
32 lines (26 loc) · 1.11 KB
/
Program102.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
/*PROGRAM USING OVERLOADING FUNCTION TEMPLATES*/
#include <iostream>
// Maximum of two int values
int const& max(int const& a, int const& b) {
return a < b ? b : a;
}
// Maximum of two values of any type
template <typename T>
T const& max(T const& a, T const& b) {
return a < b ? b : a;
}
// Maximum of three values of any type
template <typename T>
T const& max(T const& a, T const& b, T const& c) {
return max(max(a, b), c);
}
int main() {
std::cout << max(4, 5, 6) << std::endl; // Calls the template for three arguments
std::cout << max(8.1, 9.1, 0.1) << std::endl; // Calls max<double> (by argument deduction)
std::cout << max('c', 'd') << std::endl; // Calls max<char> (by argument deduction)
std::cout << max(7, 42) << std::endl; // Calls the non-template for two ints
std::cout << max<>(7, 42) << std::endl; // Calls max<int> (by argument deduction)
std::cout << max<double>(7, 42) << std::endl; // Calls max<double> (no argument deduction)
std::cout << max('c', 55.5) << std::endl; // Calls the non-template for two ints
return 0;
}