150 likes | 172 Views
EEL 3801. Part VIII Fundamentals of C and C++ Programming Template Functions and Classes. Function Overloading. Facilitates generalization and reuse of the code. Permits functions with the same name to co-exist as long as they have a difference in the arguments or arguments types.
E N D
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes
Function Overloading • Facilitates generalization and reuse of the code. • Permits functions with the same name to co-exist as long as they have a difference in the arguments or arguments types. • Compiler knows which one is the correct one to call based on the arguments supplied by the caller.
Function Overloading • The operations have to be similar, but to different types of data. • Nevertheless, various functions have to be written. • It would be better if only one function were to be written that had a variable data type. • These are called Template Functions
Template Functions • Can be used to write only one function that performs identical operations to different types of data • array of int’s, float’s, char’s, etc. • Function is defined with a “placeholder” for the type of data to be operated on. • The type is specified when the function is called.
Template Functions • The syntax for template functions uses the keyword template, followed by a list of the placeholders which represent the types to be specified later, and their label. • These placeholder and label pairs are contained within angle brackets (< >), each preceded by the keyword class. • Then follows the function definition.
Template Functions template<class element1_type> void function_name(int n, float a, element1_type b) { function body .. ... }
Template Functions- Example #include <iostream.h> template<class T> void printArray(T *array,const int count) { for (int i=0; i<count; i++) cout << array[i] << “ “; cout << endl; }
Template Functions- Example main() { const int aCount=5, bCount=4,cCount=6; int a[aCount] = {1,2,3,4,5}; float b[bCount] = {1.1,2.2,3.3,4.4}; char c[cCount]= “HELLO”; //6th pos=null printArray(a,aCount); //int template printArray(b,bCount); //float template printArray(c,cCount); //char template return 0; }
Template Functions- Example • Note that the type was specified implicitly when an int data type was passed, as in: printArray(a, aCount); • Likewise, the float type was specified implicitly when the array b was passed: printArray(b, bCount); • Same for the character string.
Template Functions • More than one data type can be specified through the “placeholder”. • Other data types can be explicitly defined directly in the function (e.g., aCount, bCount, and cCount)
Template Classes • Permit the generalization of the classes so that similar classes containing different data types for the same members, do not have to be defined more than once. • An example is declaring a class that contains an array whose type may differ, depending on what the user wants to put in it.
Template Classes • Also called parameterized types because they require a parameter to specify how to customize the generic class. • For example, an array of int’s, an array of float’s, etc. • The class definition is preceded by: template <class T>
Template Classes - Example template <class T> class Stack { public: void push(const T &); . . private: int size; int top; T entry[100]; };
Template Classes - Example template <class T> void Stack<T>::push(const T &item) { entry[++top] = item; } main() { Stack<float> floatstack; floatstack .push(2.3); . }
Template Classes • There can be more than one parameter per template class. • They can be expressed as follows: template < class S, class T, class R >