// MaxTmplt.cpp - An example of defining a template function. // // Written by Wayne Pollock, Tampa Florida 2000 #include #include using namespace std; template< class T > T max ( T a, T b ) { if ( a > b ) return a; else return b; } int main () { cout << setprecision( 2 ) << setiosflags( ios::fixed | ios::showpoint ); float f1 = 1.1, f2 = 2.2; float f3 = max( f1, f2 ); cout << f3 << endl; // Prints "2.20" int x = 1, y = 2; cout << max( x, y ) << endl; // Prints "2" char ch = 'a'; // int z = max( x, ch ); // Error: no match for "max(int, char)". // cout << z << endl; return 0; }
Send comments and mail to the WebMaster. |