Definition of function template maximum : function definition « Function « C++ Tutorial






#include <iostream>
using std::cout;
using std::cin;
using std::endl;


template < class T >  
T maximum( T value1, T value2, T value3 )
{
   T maximumValue = value1; 

   if ( value2 > maximumValue )
      maximumValue = value2;

   if ( value3 > maximumValue )
      maximumValue = value3;

   return maximumValue;
}


int main()
{
   int int1, int2, int3;

   int1 = 1;
   int2 = 2;
   int3 = 3;

   cout << "\nThe maximum integer value is: " << maximum( int1, int2, int3 );
      

   double double1, double2, double3;

   double1 = 1.1;
   double2 = 2.2;
   double3 = 3.3;

   cout << "\nThe maximum double value is: "<< maximum( double1, double2, double3 );

   char char1, char2, char3;

   char1 = 'a';
   char2 = 'b';
   char3 = 'c';

   cout << "\nThe maximum character value is: "<< maximum( char1, char2, char3) << endl;
   return 0;
}
The maximum integer value is: 3
The maximum double value is: 3.3
The maximum character value is: c








7.1.function definition
7.1.1.This program contains two functions: main() and f().
7.1.2.Define function to add two parameters together
7.1.3.The function call stack and activation records
7.1.4.Functions that take no arguments.
7.1.5.Compare pass-by-value and pass-by-reference with references
7.1.6.Default arguments with parameters
7.1.7.Definition of function template maximum
7.1.8.Functions in an expression: order of evaluation