160 likes | 363 Views
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
E N D
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 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; }
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; }
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; }
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);
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; }
In-Class Exercise • Swap two values of the same type
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; }
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); };
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) { }
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);
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); };
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) { }
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
Next Two Class Meetings • 9/15, 9/17 • MCH 201