C Templates Gordon College CPS 212 Overview n

  • Slides: 14
Download presentation
C++ Templates Gordon College CPS 212

C++ Templates Gordon College CPS 212

Overview n Templates n n n Definition: a pattern for creating classes or functions

Overview n Templates n n n Definition: a pattern for creating classes or functions as instances of the template at compile time Easily create a large range of related functions or classes Terms: n n n Function template - the blueprint of the related functions Template instance - a specific function made from a function template (instantiating the template) Type of programming: n n Generic programming Template metaprogramming

Class Templates n Class templates n n Allow type-specific versions of generic classes Format:

Class Templates n Class templates n n Allow type-specific versions of generic classes Format: template <class T> class Class. Name{ definition } n Need not use "T", any identifier will work n To create an object of the class, type Class. Name< type > my. Object; Use Example: Listor< double > double. Listor;

Class Templates n Template class functions n Declared normally, but preceded by template<class T>

Class Templates n Template class functions n Declared normally, but preceded by template<class T> n Generic data in class listed as type T Binary scope resolution operator used : : n Template class function definition: template<class T> My. Class< T >: : My. Class(int size) { my. Array = new T[size]; } n n Constructor definition - creates an array of type T

Class Template Example (see template_class_demo) template <typename T> class store { public: store(const T&

Class Template Example (see template_class_demo) template <typename T> class store { public: store(const T& item = T()); T get. Value() const; void set. Value(const T& item); friend ostream& operator<< (ostream& ostr, const store<T>& obj) { ostr << "Value = " << obj. value; return ostr; } private: T value; }; Class Declaration

Class Template Example (see template_class_demo) // use an initialization list to assign value template

Class Template Example (see template_class_demo) // use an initialization list to assign value template <typename T> store<T>: : store(const T& item): value(item) {} // return the current value template <typename T> T store<T>: : get. Value() const { return value; } // assign the argument as the new data member value template <typename T> void store<T>: : set. Value(const T& item) { value = item; } Class Implementation

Class Template Example (see template_class_demo) int main() Output { The values stored by the

Class Template Example (see template_class_demo) int main() Output { The values stored by the objects are: // declare three different types of store objects Value = 5 store<int> int. Store(5); Value = 2. 718 store<double> real. Store(2. 718); Value = Template store<string> str. Store("Template"); The concatenation: cout << "The values stored by the objects are: " << endl; Value = Template Class cout << int. Store << endl; cout << real. Store << endl; cout << str. Store << endl; cout << "The concatenation: " << endl; // access current value str. Store and concatenate " Class" // update the value in str. Store with the new string str. Store. set. Value( str. Store. get. Value() + " Class" ); cout << str. Store << endl; return 0; } Class Usage

Class Templates and Non-type Parameters n n Can use non-type parameters (T) in templates

Class Templates and Non-type Parameters n n Can use non-type parameters (T) in templates n Can have default argument n Treated as const Example: template< class T = int, int elements > Listor< double, 100 > most. Recent. Sales. Figures; n Declares object of type: n n Listor<100> Listor< double, 100> This may appear in the class definition: T stack. Holder[ elements ]; //array n n to hold stack Creates array at compile time, rather than dynamic allocation at execution time

Function Templates n n n Defines a pattern for any number of functions whose

Function Templates n n n Defines a pattern for any number of functions whose definitions depend on the template parameters. Can overload a function template with a non-template function or with other function templates. Used throughout the STL n sort, copy, for_each, etc.

Function Template Example template <typename T> void selection. Sort(T arr[], int n) { int

Function Template Example template <typename T> void selection. Sort(T arr[], int n) { int small. Index; // index of smallest element in the sublist int pass, j; T temp; // pass has the range 0 to n-2 for (pass = 0; pass < n-1; pass++). . . Function Declaration

Function Template Example int itest[20]={4, 7, 8, 9, 2, 12, 3, 2, 100, 34,

Function Template Example int itest[20]={4, 7, 8, 9, 2, 12, 3, 2, 100, 34, 54, 67, 54, 4, 5, 87, 89, 4, 5, 23}; string stest[10]={"this", "a", "test", "I", "hope", "it", "works", "It", "worked"}; for(int i = 0; i<20; i++) cout<<" "<<itest[i]; cout<<endl; for(int i = 0; i<10; i++) cout<<" "<<stest[i]; cout<<endl; cout << "SORTED" <<endl; output selection. Sort(itest, 20); 4 7 8 9 2 12 3 2 100 34 54 67 54 4 5 87 89 4 5 23 this is a test I hope it works It worked for(int i = 0; i<20; i++) SORTED cout<<" "<<itest[i]; 2 2 3 4 4 4 5 5 7 8 9 12 23 34 54 54 67 87 89 100 I It a hope is it test this worked works cout<<endl; selection. Sort(stest, 10); for(int i = 0; i<10; i++) cout<<" "<<stest[i]; Function Usage cout<<endl;

Operator Templates n n Create operator template in the same manner as function templates

Operator Templates n n Create operator template in the same manner as function templates Rules for deducing argument types apply n Example: Template<typename T> Bigint operator+(const Bingint& x, const T& y); Bigint a; a = a + 42; //argument type deduction a = operator+<long>(a, 42); //explicit argument type

Operator Template Example template <class T> class Array { private: T* p. Type; int

Operator Template Example template <class T> class Array { private: T* p. Type; int its. Size; public: Array(int its. Size = Default. Size); Array(const Array &rhs); ~Array(){delete [] p. Type; } Array& operator=(const Array&); T& operator[](int offset){ return p. Type[offset]; } const T& operator[](int offset) const { return p. Type[offset]; } int Get. Size() const {return its. Size; } }; Operator Declaration

Operator Template Example Array<int> the. Array; Array<Animal> the. Zoo; Animal *p. Animal; for(int i=0;

Operator Template Example Array<int> the. Array; Array<Animal> the. Zoo; Animal *p. Animal; for(int i=0; i<the. Array. Get. Size(); i++) { the. Array[i] = i*2; p. Animal = new Animal(i*3); the. Zoo[i] = *p. Animal; delete p. Animal; } for(int j=0; j<the. Array. Get. Size(); j++) { cout << "the. Array[" << j << "]: "; cout << the. Array[j] << ”tt"; cout << "the. Zoo[" << j << "]: "; cout << the. Zoo[j] << endl; } Operator usage: see template_operator_zoo