1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
// maximum.h
// Definition of function template maximum.
template < class T > // or template< typename T >
T maximum( T value1, T value2, T value3 )
{
T maximumValue = value1; // assume value 1 is maximum
// determine whether value 2 is greater than maximumValue
if ( value2 > maximumValue )
maximumValue = value2;
// determine whether value3 is greater than maximumValue
if ( value3 > maximumValue )
maximumValue = value3;
return maximumValue;
} // end function template maximum
// Main.cpp
// Function template maximum test program.
#include <iostream>
using namespace std;
#include "maximum.h" // include definition of function template maximum
int main()
{
// demonstrate maximum with int values
int int1, int2, int3;
cout << "Input three integer values: ";
cin >> int1 >> int2 >> int3;
// invoke in version of maximum
cout << "The maximum integer value is: "
<< maximum( int1, int2, int3 );
// demonstrate maximum with double values
double double1, double2, double3;
cout << "\n\nInput three double values: ";
cin >> double1 >> double2 >> double3;
// invoke double version of maximum
cout << "The maximum double value is: "
<< maximum( double1, double2, double3 );
// demonstrate maximum with char values
char char1, char2, char3;
cout << "\n\nInput three characters: ";
cin >> char1, char2, char3;
// invoke char version of maximum
cout << "The maximum character value is: "
<< maximum( char1, char2, char3 ) << endl;
system("pause");
return 0;
}
|