// template1.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program provides an introduction to template functions. In fact, the // program does not really illustrate template functions, rather it explains why // template functions could be needed. // // Notice the similarity between the three overloaded functions below. #include // free function that finds and returns the minimum value of two integers. int min( int a, int b ) { return a < b ? a : b; // same as: if ( a < b ) return a; else return b; } // free function that finds and returns the minimum value of two "double" // variables. // Notice that this is an overloaded function. double min( double a, double b ) { return a < b ? a : b; // same as if ( a < b ) return a; else return b; } // free function that finds and returns the smaller of two characters. // Notice that this is an overloaded function. char min( char a, char b ) { return a < b ? a : b; // same as if ( a < b ) return a; else return b; } int main( ) { int x = 10, y = 5; double w = 13.2, z = 21.9; char ch1 = 'f', ch2 = 'k'; cout << " The minimum value of the two integers x and y is: " << min( x, y ) << endl << endl; cout << " The minimum value of the two doubles w and z is: " << min( w, z ) << endl << endl; cout << " The minimum value of the two characters ch1 and ch2 is: " << min( ch1, ch2) << endl << endl; return 0; } // The output of the program /* The minimum value of the two integers x and y is: 5 The minimum value of the two doubles w and z is: 13.2 The minimum value of the two characters ch1 and ch2 is: f */