// template8.cpp By: Aiman Hanna - ©1993-2005 Aiman Hanna // This program illustrates more on template functions. // Key Points: 1) Explicit type conversion (casting) #include template Type min( Type a, Type 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; // The following calls would be illegal // cout << " The minimum value of the two values x and w is: " << min( x, w ) // << endl << endl; // However, the call can be made as follows: cout << " The minimum value of the two values x and w is: " << min( x, w ) << endl << endl; // or as follows, with a warning, as expected, which is conversion from // 'double' to 'int', possible loss of data cout << " The minimum value of the two values x and w is: " << min( x, w ) << 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 The minimum value of the two values x and w is: 10 The minimum value of the two values x and w is: 10 */