C++ examples for template:template function
Definition of generic function type maximum
template <typename T> T maximum(T value1, T value2, T value3) { T maximumValue = value1;/*from w w w . j ava 2 s .c o m*/ if (value2 > maximumValue) maximumValue = value2; if (value3 > maximumValue) maximumValue = value3; return maximumValue; } #include <iostream> int main(int argc, const char *argv[]) { // demonstrate maximum with int values int int1, int2, int3; std::cout << "Input three integer values: "; std::cin >> int1 >> int2 >> int3; std::cout << "The maximum integer value is: " << maximum(int1, int2, int3) << std::endl; // demonstrate maximum with double values double double1, double2, double3; std::cout << "\nInput three double values: "; std::cin >> double1 >> double2 >> double3; std::cout << "The maximum double value is: " << maximum(double1, double2, double3) << std::endl; // demonstrate maximum with char values char char1, char2, char3; std::cout << "\nInput three characters: "; std::cin >> char1 >> char2 >> char3; std::cout << "The maximum character value is: " << maximum(char1, char2, char3) << std::endl; return 0; }