Chapter 3 Templates - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Chapter 3 Templates

Description:

Chapter 3 Templates Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy stuff Bugs 3 ... – PowerPoint PPT presentation

Number of Views:87
Avg rating:3.0/5.0
Slides: 23
Provided by: Ken1222
Learn more at: https://www.cs.gsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Chapter 3 Templates


1
Chapter 3Templates
2
Objective
  • In Chapter 3, we will discuss
  • The concept of a template
  • Function templates
  • Class templates
  • vector and matrix classes
  • Fancy stuff
  • Bugs

3
3.1 What is a Template?
  • A template is a mechanism for writing routines
    that work for arbitrary types without knowing
    these types.
  • For example Type independent algorithms and data
    structures
  • Templates are most likely seen in generic
    algorithm implementations, i.e. sorting
    searching.

4
3.2 Function Templates
  • typedef keyword for defining new type from an
    existing one.
  • Ex typedef double Object
  • Object a 3.5 // double a 3.5
  • A Function template is a design or pattern for a
    function which allows processors to generate an
    actual function from this design.
  • A function template is not an actual function,
    instead its a design or a pattern, for what
    could become an actual function.

5
Function template example Swap routine
  • typedef double Object
  • void swap(Object lhs,
  • Object rhs)
  • Object tmp lhs
  • lhs rhs
  • rhs tmp
  • // figure 3.1
  • Note swap is part of the STL
  • //Object must have copy
  • // constructor operator
  • template ltclass Objectgt
  • void Swap(Object lhs,
  • Object rhs)
  • Object tmp lhs
  • lhs rhs
  • rhs tmp
  • // figure 3.2

6
Swap routine used in main function
  • int main()
  • int x 5, y 7
  • double a 2, b 4
  • Swap (x,y) // swap(int,int)
  • Swap(x,y) //reuse previous instantiation
  • Swap(a,b) //Swap(double, double)
  • //Swap(x, b) // illegal no match
  • return 0
  • // figure 3.3

7
3.3 A Sorting Function Template
  • template ltclass Comparablegt
  • void insertionSort(vectorltComparablegt a)
  • for(int p 1 p lt a.size() p)
  • Comparable tmp ap
  • int j
  • for(j p j gt 0 tmp lt aj-1 j)
  • aj a j 1
  • aj tmp

8
Insertion Sort example
  • The given insertionSort routine work for double,
    int, float but not char, (primitive string),
    because operator and lt are undefined.
  • sorting demo(1)
  • sorting demo(2)

9
3.4 Class Templates
  • A class can be a template.
  • Example vector is a class template
  • A call by value should not be used for parameters
    of the template type use constant reference
    instead.

10
A class template example
  • template ltclass Objectgt
  • class MemoryCell
  • public
  • explicit MemoryCell(const Object initVal
    Object())
  • storedValue(initVal)
  • const Object read() const
  • return storedValue
  • void write(const Object x)
  • storedValue x
  • private
  • Object storedValue
  • // figure 3.8

11
  • Calling the class template from main
  • int main()
  • MemoryCellltintgt m
  • m.write(5)
  • coutltltCell contents are ltltm.read()ltltendl
  • return 0
  • When the interface is separated from the
    implementation, all member functions should be
    implemented as template functions

12
  • include MemoryCell.h
  • template ltclass Objectgt
  • MemoryCellltObjectgtMemoryCell
  • (const Object initVal)
  • storedValue(initVal)
  • templateltclass Objectgt
  • const Object MemoryCellltObjectgtread() const
  • return storedValue
  • template ltclass Objectgt
  • void MemoryCellltObjectgtwrite(
  • const Object x)
  • storedValue x
  • // figure 3.11
  • templateltclass Objectgt
  • class MemoryCell
  • public
  • explicit MemoryCell(const Object
    initVal Object())
  • const Object read() const
  • const write(const Object x)
  • private
  • Object storedValue
  • // figure 3.10

Separating the interface and implementation of
the template
13
  • templateltclass objectgt
  • class ClassName
  • public
  • //public members
  • private
  • //private member
  • // typical template interface
  • template ltclass objectgt
  • ReturnType
  • ClassNameltobjectgtmemberName(parameterList)
    /const/
  • // member body
  • // typical member implementation

14
Implementing the Vector Class Template
  • Provides Indexing, resizing, copying, and index
    range checking capabilities
  • figure 3.14 vector.h
  • figure 3.15 vector.cpp

15
3.5 Templates of Templates A Matrix Class
  • Figure 3.16 A complete matrix class
  • Matrix class is represented as a vector of
    vectors.
  • Overload operator for mutators and accessors
  • No need for destructors, copy operator and
    assignment operator, (already exist in vector)
  • Note There must be at least a space between gt
    gt in the declaration otherwise compilers will
    confuse gtgt with the shift operator .
  • Ex vectorltvectorltObjectgt gt array // white space
    needed

16
3.6 Fancy Templates
  • Multiple template parameters
  • template ltclass keyType, class valueTypegt
  • Class Map
  • . . .
  • Ex mapltstring,intgt zipCodes
  • Note class map is in STL

17
Default Template Parameters
  • template ltclass keyType, class valueType
    stringgt
  • Class Map
  • . . .
  • Mapltint, intgt m1 // keyType int, valueType
    int
  • Mapltintgt m2 // keyType int, valueType string
  • Default template parameters are widely used in
    STL
  • Note some compilers do not support default
    template parameters

18
Default Template Parameters
  • typename a new keyword can be used instead of
    class
  • template lttypename Objectgt
  • class memoryCell
  • . . .

19
3.7 Bugs Associated with Templates
  • Template STD changes frequently
  • gt incompatible compilers
  • Matching algorithm breaks when template involve
    (function template)
  • Nested templates are not widely supported
  • Static functions data members are often not
    handled correctly

20
Common mistakes
  • When a class template is instantiated, all needed
    operations must be available.
  • For instance, the insertion sort template needs
    to have operatorlt defined for whatever Comparable
    is instantiated. Otherwise, an error will be
    detected at link time. Clearly state what
    conditions must be satisfied by the template
    parameters
  • Use either reference or constant reference for
    parameter passing. (Page 115)

21
In class exercise
  • Write a function template to sort three
    Comparable objects.
  • When templates are used what types of errors are
    detected when the function template is scanned?
    What errors are detected when the function is
    instantiate?
  • Describe syntax for class templates.

22
Summary
  • Discussion of template facilities
  • Template for generic algorithms
Write a Comment
User Comments (0)
About PowerShow.com