EEL 3801 - PowerPoint PPT Presentation

About This Presentation
Title:

EEL 3801

Description:

EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes – PowerPoint PPT presentation

Number of Views:120
Avg rating:3.0/5.0
Slides: 16
Provided by: ave103
Learn more at: http://www.eecs.ucf.edu
Category:
Tags: eel

less

Transcript and Presenter's Notes

Title: EEL 3801


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 ints, floats, chars, 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 (lt gt), each preceded by
    the keyword class.
  • Then follows the function definition.

6
Template Functions
  • templateltclass element1_typegt
  • void function_name(int n, float a, element1_type
    b)
  • function body ..
  • ...

7
Template Functions- Example
  • include ltiostream.hgt
  • templateltclass Tgt
  • void printArray(T array,const int count)
  • for (int i0 iltcount i)
  • cout ltlt arrayi ltlt
  • cout ltlt endl

8
Template Functions- Example
  • main()
  • const int aCount5, bCount4,cCount6
  • int aaCount 1,2,3,4,5
  • float bbCount 1.1,2.2,3.3,4.4
  • char ccCount HELLO //6th posnull
  • 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 ints, an array of
    floats, etc.
  • The class definition is preceded by
  • template ltclass Tgt

13
Template Classes - Example
  • template ltclass Tgt
  • class Stack
  • public
  • void push(const T )
  • .
  • .
  • private
  • int size
  • int top
  • T entry100

14
Template Classes - Example
  • template ltclass Tgt
  • void StackltTgtpush(const T item)
  • entrytop item
  • main()
  • Stackltfloatgt 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 lt class S, class T, class R gt
Write a Comment
User Comments (0)
About PowerShow.com