Chapter 3 Templates - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Chapter 3 Templates

Description:

A function template is a design or pattern for a function which allows ... C Class Templates are used where we have multiple copies of code for different ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 16
Provided by: GSU50
Learn more at: https://www.cs.gsu.edu
Category:
Tags: chapter | templates | web

less

Transcript and Presenter's Notes

Title: Chapter 3 Templates


1
Chapter 3 Templates
  • Saurav Karmakar
  • Spring 2007

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

3
3.1 What is a Template
  • A mechanism for writing routines that work for
    arbitrary types w/o knowing these types (type
    independent).
  • Most likely be seen in generic algorithm
    implementations, i.e. find max, swapping, sorting
    searching.

4
3.2 Function Templates
  • typedef keyword for defining new type from an
    existing one.Ex
  • typedef double Object
  • void swap( Object lhs, Object rhs)
  • Object temp lhs
  • lhs rhs
  • rhs temp

5
3.2 Function Templates
  • A function template is not an actual function,
    instead its a design or a pattern, for what
    could become an actual function.
  • A function template is a design or pattern for a
    function which allows processors to generate an
    actual function from this design.

6
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
  • template ltclass Objectgt
  • void swap(Object lhs,
  • Object rhs)
  • Object tmp lhs
  • lhs rhs
  • rhs tmp
  • // figure 3.2

7
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

8
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

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

10
3.4 Class Templates
  • A class can be a template.
  • Example vector is a class template
  •  C Class Templates are used where we have
    multiple copies of code for different data types
    with the same logic.
  • If a set of functions or classes have the same
    functionality for different data types, they
    becomes good candidates for being written as
    Templates.
  • When possible, constant reference should be used
    instead of call by value because if object is a
    large object, making a copy could be inefficient.
    (or illegal if copy constructor is disable or not
    defined)

11
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

12
Typical template interface
  • templateltclass objectgt
  • class ClassName
  • public
  • //public members
  • private
  • //private member

13
Typical member implementation
  • template ltclass objectgt
  • ReturnType
  • ClassNameltobjectgtmemberName(parameterList)
    /const/
  • // member body

14
Interface of the template
  • 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

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