1 / 19

Chapter 3 Templates

Chapter 3 Templates. Bernard Chen Spring 2006. Objective. In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy stuff. 3.1 What is a Template.

boaz
Download Presentation

Chapter 3 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. Chapter 3 Templates Bernard Chen Spring 2006

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

  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, sorting & searching.

  4. 3.2 Function Templates • typedef: keyword for defining new type from an existing one.Ex: typedef double Object; Object a = 3.5; // double a = 3.5 Object b=3; // int b=3

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

  6. 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 <class Object> void swap(Object & lhs, Object & rhs) { Object tmp = lhs; lhs = rhs; rhs = tmp; }// figure 3.2 Function template example: Swap routine

  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 <class Comparable> void insertionSort(vector<Comparable> &a) { for( int p = 1; p < a.size() ; p++) { comparable tmp = a[p]; int j; for ( j=p ; j>0 ; j-- ) { if (temp < a[j-1]) { a[j]=a[j-1]; a[j-1]=tmp;} } } }

  9. 3.3 A Sorting Function Template template <class Comparable> void insertionSort(vector<Comparable> &a) { for(int p = 1; p < a.size(); p++) { Comparable tmp = a[p]; int j; for(j = p; j > 0 && tmp < a[j-1]; j--) a[j] = a [j –1]; a[j] = tmp; } }

  10. Insertion Sort example • The given insertionSort routine work for double, int, float but not char*, (primitive string), because operator “=” and “<” are undefined.

  11. 3.4 Class Templates • A class can be a template. • Example: vector is a class template • 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)

  12. A class template example: template <class Object> 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

  13. Typical template interface template<class object> class ClassName{ public: //public members private: //private member };

  14. Typical member implementation template <class object> ReturnType ClassName<object>::memberName(parameterList) /*const*/ { // member body }

  15. template<class Object> class MemoryCell { public: explicit MemoryCell(const Object & initVal = Object()); const Object & read() const; const write(const Object & x); private: Object storedValue; };// figure 3.10 template<class Object> class MemoryCell { public: explicit MemoryCell(const Object & initVal = Object()); const Object & read() const; const write(const Object & x); private: Object storedValue; };// figure 3.10 Separating the interface and implementation of the template

  16. Implementing the Vector Class Template • Provides: Indexing, resizing, copying, and index range checking capabilities • figure 3.14 vector.h • figure 3.15 vector.cpp

  17. 3.5 Templates of Templates: A Matrix Class • Figure 3.16 A complete matrix class • Matrix class is represented as a vector of vectors. • operator [] • No need for destructors, copy operator and assignment operator, (already exist in vector) • Note: There must be at least a space between “>” “>” in the declaration; otherwise compilers will confuse “>>” with the shift operator . Ex: vector<vector<Object> > array; // white space needed

  18. 3.6 Fancy Templates • Multiple template parameters template <class keyType, class valueType> Class Map{ . . . }; Ex: map<string,int> zipCodes; Note: class “map” is in STL

  19. Summary • Discussion of template facilities • Template for generic algorithms

More Related