# Templates - PowerPoint PPT Presentation

1 / 16
Templates

## Templates

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

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 > 0; --size, ++iptr) { if (current_largest < *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 > 0; --size, ++iptr) { if (current_largest < *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 > 0; --size, ++iptr) { if (current_largest < *iptr) { current_largest = *iptr; } } return current_largest; }

6. Motivating Example int int_array[20]; float float_array[30]; double double_array[40]; cout << largest(int_array, 20) << “ “ << largest(float_array, 30) << “ “ << largest(double_array, 40);

7. Function Template Example • Find the largest element in an array template <typename T> T largest(const T *iptr, unsigned int size) { T current_largest = *iptr; for (; size > 0; --size, ++iptr) { if (current_largest < *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 <typename T> 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 <typename T> class Pair { public: T first; T second; Pair(); Pair(T t1, T t2); };

11. Class Template Example • Constructors (in the same header file) template <typename T> Pair<T>::Pair() { } template <typename T> Pair<T>::Pair(T t1, T t2) : first(t1), second(t2) { }

12. You need the space Class Template Example • Declarations Pair<int> intPair(1, 2); Pair<float> floatPair(1.1, 2.2); Pair< Pair<int> > pair(intPair, intPair);

13. Class Template Example 2 • A class that holds a pair of items with different types template <typename T1, typename T2> class Pair { public: T1 first; T2 second; Pair(); Pair(T1 t1, T2 t2); };

14. Class Template Example 2 • Constructors template <typename T1, typename T2> Pair<T1, T2>::Pair() { } template <typename T1, typename T2> Pair<T1, T2>::Pair(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