Templates - PowerPoint PPT Presentation

About This Presentation
Title:

Templates

Description:

Templates. Provide generic programming. Generic classes. Generic functions ... Class Template Example 2. A class that holds a pair of items with different types ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 17
Provided by: csF2
Learn more at: http://www.cs.fsu.edu
Category:
Tags: templates | web

less

Transcript and Presenter's Notes

Title: Templates


1
Templates
  • Andy Wang
  • Data Structures, Algorithms, and Generic
    Programming

2
Templates
  • Provide generic programming
  • Generic classes
  • Generic functions
  • Generic algorithms (later lectures)
  • Goal
  • Program reuse

3
Motivating Example
  • Find the largest element in an array
  • int largest(const int iptr, unsigned int size)
  • int current_largest iptr
  • for ( size gt 0 --size, iptr)
  • if (current_largest lt iptr)
  • current_largest iptr
  • return current_largest

4
Motivating Example
  • Find the largest element in an array
  • float largest(const float iptr, unsigned int
    size)
  • float current_largest iptr
  • for ( size gt 0 --size, iptr)
  • if (current_largest lt iptr)
  • current_largest iptr
  • return current_largest

5
Motivating Example
  • Find the largest element in an array
  • double largest(const double iptr, unsigned int
    size)
  • double current_largest iptr
  • for ( size gt 0 --size, iptr)
  • if (current_largest lt iptr)
  • current_largest iptr
  • return current_largest

6
Motivating Example
  • int int_array20
  • float float_array30
  • double double_array40
  • cout ltlt largest(int_array, 20) ltlt ltlt
    largest(float_array, 30) ltlt ltlt
    largest(double_array, 40)

7
Function Template Example
  • Find the largest element in an array
  • template lttypename Tgt
  • T largest(const T iptr, unsigned int size)
  • T current_largest iptr
  • for ( size gt 0 --size, iptr)
  • if (current_largest lt iptr)
  • current_largest iptr
  • return current_largest

8
In-Class Exercise
  • Swap two values of the same type

9
Function Template Example 2
  • Swap two values of the same type
  • template lttypename Tgt
  • void swap (T t1, T t2)
  • T temp
  • temp t1
  • t1 t2
  • t2 temp

10
Class Template Example
  • A class that holds a pair of items
  • template lttypename Tgt
  • class Pair
  • public
  • T first
  • T second
  • Pair()
  • Pair(T t1, T t2)

11
Class Template Example
  • Constructors (in the same header file)
  • template lttypename Tgt
  • PairltTgtPair()
  • template lttypename Tgt
  • PairltTgtPair(T t1, T t2) first(t1), second(t2)

12
Class Template Example
  • Declarations
  • Pairltintgt intPair(1, 2)
  • Pairltfloatgt floatPair(1.1, 2.2)
  • Pairlt Pairltintgt gt pair(intPair, intPair)

13
Class Template Example 2
  • A class that holds a pair of items with
    different types
  • template lttypename T1, typename T2gt
  • class Pair
  • public
  • T1 first
  • T2 second
  • Pair()
  • Pair(T1 t1, T2 t2)

14
Class Template Example 2
  • Constructors
  • template lttypename T1, typename T2gt
  • PairltT1, T2gtPair()
  • template lttypename T1, typename T2gt
  • PairltT1, T2gtPair(T1 t1, T2 t2) first(t1),
    second(t2)

15
Template Code Files
  • Place template class definitions and
    implementation in the same header file
  • Protect files from nested inclusions
  • ifndef _YADDY_H
  • define _YADDY_H
  • endif _YADDY_H

16
Next Two Class Meetings
  • 9/15, 9/17
  • MCH 201
Write a Comment
User Comments (0)
About PowerShow.com