Template Functions (pages 911-918) by gigi12

VIEWS: 4 PAGES: 2

									                                 Template Functions (pages 911-918)

We saw that we could use the same name for multiple functions provided that the functions each had a
unique parameter list (function overloading)

We might overload a function if multiple functions used the same algorithm, but depended on data of
different types. (e.g. DebugPrint( ))

We had to write 3 function implementations for the DebugPrint( ) function even though the BODY of
each function was identical. We could have saved ourselves some typing by using a template function
to overload the function.

A template function allows the compiler to generate the defintion of a function by allowing
the type to be represented as a parameter. (In other words, the type of the parameter/s is passed to the
function in addition to the actual arguments for the function at the time the function is invoked).

From the text: Function Template: A C++ language construct that allows the compiler o generate
multiple versions of a function by allowing parameterized data types.

Template Function Implementation: (angle brackets ARE part of the syntax)
      //MUST be declared globally (before main())
      template <class Atype>
      void DebugPrint (Atype data) {
             cout << “Debugging” << data << endl;
      }

Template Function Call: (angle brackets ARE part of the syntax)
      DebugPrint<int>(value);

Example program:

       //template function implementation
       template <class Atype>
       void DebugPrint (Atype data) {
              cout << “Debugging” << data << endl;
       }

       int main( ) {

               int value;                            //variable declarations
               float average;
               string name;

               …      //program statements to assign values to value, average, name,
               DebugPrint<int>(value);
               Debug Print<float>(average);
               DebugPrint<string>(name);
               …      //rest of the program
               return 0;
       }
Another program:

#include <iostream>
#include <string>

//MUST be declared globally (before main())
template <class Atype>
void DebugPrint (Atype data) {
        cout << "Debugging " << data << endl;
}

template <class whatEver>
whatEver max (whatEver a, whatEver b)
{
        if ( a >= b ) return a;
        else return b;
}


int main( ) {

        int value1, value2;                  //variable declarations
        float average1, average2;
        string name1, name2;
//program statements to assign values to value, average, name,
        value1 = 5; value2 = 23;
        DebugPrint<int>(value1);
        DebugPrint<int>(value2);

         average1 = (5.1 + 89.4 -34.6)/ (float) value1;
         average2 = (5.1 + 89.4 -34.6)/ (float) value2;
         DebugPrint<float>(average1);
         DebugPrint<float>(average2);

         name1 = "Bobo Edsgar Poe";
         name2 = "Coco E. Butter";
         DebugPrint<string>(name1);
         DebugPrint<string>(name2);

         cout << "The max is " << max<int>(value1, value2) << endl;
         cout << "The max is " << max<float>(average1, average2) << endl;
         cout << "The max is " << max<string>(name1, name2) << endl;


         /**rest of the program
         *
         **/

         return 0;
}

								
To top