// template5.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program illustrates how template functions can be overloaded. // // Key Points: 1) Overloading template functions. #include // Free template function that expects two values of a given type and returns the // summation of these two values. template T Add( T v1, T v2 ) { return v1 + v2; } // Free template functions that expects three values of a given type and returns the // summation of these three values. Notice that this function is an overloaded // function. template T Add( T v1, T v2, T v3 ) { return v1 + v2 + v3; } int main() { int i1 = 3, i2 = 6, i3 = 12; double d1 = 2.5, d2 = 5.3, d3 = 9.6; cout << " The total of i1 and i2 is: " << Add( i1, i2 ) << endl << endl; cout << " The total of d1 and d2 is: " << Add( d1, d2 ) << endl << endl; cout << " The total of i1, i2 and i3 is: " << Add( i1, i2, i3 ) << endl << endl; cout << " The total of d1, d2 and d3 is: " << Add( d1, d2, d3 ) << endl << endl; return 0; } // The output of the program: /* The total of i1 and i2 is: 9 The total of d1 and d2 is: 7.8 The total of i1, i2 and i3 is: 21 The total of d1, d2 and d3 is: 17.4 */