1 / 16

Templates

Templates. Andy Wang Data Structures, Algorithms, and Generic Programming. Templates. Provide generic programming Generic classes Generic functions Generic algorithms (later lectures) Goal: Program reuse. Motivating Example. Find the largest element in an array

kamal
Télécharger la présentation

Templates

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

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

More Related