1 / 15

EEL 3801

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.

lcelia
Download Presentation

EEL 3801

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes

  2. 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.

  3. 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

  4. 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.

  5. 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.

  6. Template Functions template<class element1_type> void function_name(int n, float a, element1_type b) { function body .. ... }

  7. 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; }

  8. 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; }

  9. 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.

  10. 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)

  11. 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.

  12. 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>

  13. Template Classes - Example template <class T> class Stack { public: void push(const T &); . . private: int size; int top; T entry[100]; };

  14. Template Classes - Example template <class T> void Stack<T>::push(const T &item) { entry[++top] = item; } main() { Stack<float> floatstack; floatstack .push(2.3); . }

  15. 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 >

More Related