Function Templates - Example 10-2 - An overloaded max() function and a max function template
// File: ex10-2.cpp
#include <iostream>
#include <cstring>
using namespace std;
template <typename T> T Max(T a, T b)
{
return (a > b ? a : b);
}
char * Max(char* a, char* b)
{
return ((strcmp(a,b) > 0) ? a : b);
}
int main(void)
{
cout << Max(3,4) << endl;
cout << Max(4.55,1.23) << endl;
cout << Max(short(2),(short)3) << endl;
cout << Max('a','d') << endl;
cout << Max('N',Max('H','U')) << endl;
cout << Max("hey","hello") << endl;
cout << Max("HELLO","Hey") << endl;
cout << Max("hey","abc") << endl;
return 0;
}
/* MS Visual C++ 2008 Express edition output
4
4.55
3
d
U
hello
Hey
hey
*** Dev-C++ (gnu ver. 3.4.2) output
4
4.55
3
d
U
hey
HELLO
abc
*/