1 / 17

Template Functions and Class Templates

Learn how to create and use function templates and class templates in C++. Understand how to create type-specific versions of generic classes using class templates.

jwilkie
Download Presentation

Template Functions and Class 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 Outline 12.1 Introduction 12.2 Function Templates 12.3 Overloading Template Functions 12.4 Class Templates 12.5 Class Templates and Non-type Parameters 12.6 Templates and Inheritance 12.7 Templates and friends 12.8 Templates and static Members

  2. 12.1 Introduction • Templates - easily create a large range of related functions or classes • function template - the blueprint of the related functions • template function - a specific function made from a function template

  3. 12.2 Function Templates • overloaded functions • perform similar operations on different data types • function templates • perform identical operations on different data types • provide type checking • Format: template<class type, class type...> • can use class or typename - specifies type parameters template< class T > template< typename ElementType > template< class BorderType, class FillType > • Function definition follows template statement

  4. 1 template< class T > 2 void printArray( const T *array, constint count ) 3 { T is the type parameter. T's type is detected and substituted inside the function. The newly created function is compiled. 4 for ( int i = 0; i < count; i++ ) 5 cout << array[ i ] << " "; 6 7 cout << endl; 8 } 12.2 Function Templates (II) The int version of printArray is void printArray( const int *array, const int count ){ for ( int i = 0; i < count; i++ ) cout << array[ i ] << " "; cout << endl;}

  5. 1 // Fig 12.2: fig12_02.cpp 2 // Using template functions 3 #include <iostream> 4 5 using std::cout; Notice how type parameter T is used in place of int, float, etc. . 6 using std::endl; 7 8 template< class T > 9 void printArray( const T *array, const int count ) Each type array gets operated on by a different template function. 10 { 11 for ( int i = 0; i < count; i++ ) 12 cout << array[ i ] << " "; 13 14 cout << endl; 15 } 16 17 int main() 18 { 19 const int aCount = 5, bCount = 7, cCount = 6; 20 int a[ aCount ] = { 1, 2, 3, 4, 5 }; 21 double b[ bCount ] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 }; 22 char c[ cCount ] = "HELLO"; // 6th position for null 23 24 cout << "Array a contains:" << endl; 25 printArray( a, aCount ); // integer template function 26 27 cout << "Array b contains:" << endl; 28 printArray( b, bCount ); // double template function 29 30 cout << "Array c contains:" << endl; 31 printArray( c, cCount ); // character template function 32 33 return 0; 34 }

  6. Array a contains: 1 2 3 4 5 Array b contains: 1.1 2.2 3.3 4.4 5.5 6.6 7.7 Array c contains: H E L L O Program Output

  7. 12.4 Class Templates • class templates • allow type-specific versions of generic classes • Format: template <class T> class ClassName{ definition } • Need not use "T", any identifier will work • To create an object of the class, type ClassName < type > myObject; Example: Stack< double > doubleStack;

  8. 12.4 Class Templates (II) • Template class functions • declared normally, but preceded by template<class T> • generic data in class listed as type T • binary scope resolution operator used • Template class function definition: template<class T> MyClass< T >::MyClass(int size) { myArray = new T[size]; } • constructor definition - creates an array of type T

  9. 1 // Fig. 12.3: tstack1.h 2 // Class template Stack 3 #ifndef TSTACK1_H 4 #define TSTACK1_H 5 6 template< class T > 7 class Stack { 8 public: 9 Stack( int = 10 ); // default constructor (stack size 10) Notice how a member function of the class template is defined 10 ~Stack() { delete [] stackPtr; } // destructor 11 bool push( const T& ); // push an element onto the stack 12 bool pop( T& ); // pop an element off the stack 13 private: 14 int size; // # of elements in the stack 15 int top; // location of the top element 16 T *stackPtr; // pointer to the stack 17 18 boolisEmpty() const { return top == -1; } // utility 19 boolisFull() const { return top == size - 1; } // functions 20 }; 21 22 // Constructor with default size 10 23 template< class T > 24 Stack< T >::Stack( int s ) 25 { 26 size = s > 0 ? s : 10; 27 top = -1; // Stack is initially empty 28 stackPtr = new T[ size ]; // allocate space for elements 29 }

  10. 30 31 // Push an element onto the stack 32 // return 1 if successful, 0 otherwise 33 template< class T > Test if the stack is full. If not, push element. 34 bool Stack< T >::push( const T &pushValue ) 35 { 36 if ( !isFull() ) { Test if the stack is empty. If not, pop an element. 37 stackPtr[ ++top ] = pushValue; // place item in Stack 38 returntrue; // push successful 39 } 40 returnfalse; // push unsuccessful 41 } 42 43 // Pop an element off the stack 44 template< class T > 45 bool Stack< T >::pop( T &popValue ) 46 { 47 if ( !isEmpty() ) { 48 popValue = stackPtr[ top-- ]; // remove item from Stack 49 returntrue; // pop successful 50 } 51 returnfalse; // pop unsuccessful 52 } 53 54 #endif

  11. 55 // Fig. 12.3: fig12_03.cpp 56 // Test driver for Stack template 57 #include <iostream> 58 59 using std::cout; 60 using std::cin; 61 using std::endl; 62 63 #include "tstack1.h" 64 65 int main() 66 { 67 Stack< double > doubleStack( 5 ); 68 double f = 1.1; 69 cout << "Pushing elements onto doubleStack\n"; 70 71 while ( doubleStack.push( f ) ) { // success true returned 72 cout << f << ' '; 73 f += 1.1; 74 } 75 76 cout << "\nStack is full. Cannot push " << f 77 << "\n\nPopping elements from doubleStack\n"; 78 79 while ( doubleStack.pop( f ) ) // success true returned Pushing elements onto doubleStack 1.1 2.2 3.3 4.4 5.5 Stack is full. Cannot push 6.6 Popping elements from doubleStack

  12. 80 cout << f << ' '; 81 82 cout << "\nStack is empty. Cannot pop\n"; 83 84 Stack< int > intStack; 85 int i = 1; 86 cout << "\nPushing elements onto intStack\n"; 87 88 while ( intStack.push( i ) ) { // success true returned 89 cout << i << ' '; 90 ++i; 91 } 92 93 cout << "\nStack is full. Cannot push " << i 94 << "\n\nPopping elements from intStack\n"; 95 96 while ( intStack.pop( i ) ) // success true returned 97 cout << i << ' '; 98 99 cout << "\nStack is empty. Cannot pop\n"; 100 return 0; 101 } 5.5 4.4 3.3 2.2 1.1 Stack is empty. Cannot pop Pushing elements onto intStack 1 2 3 4 5 6 7 8 9 10 Stack is full. Cannot push 11 Popping elements from intStack 10 9 8 7 6 5 4 3 2 1 Stack is empty. Cannot pop

  13. Pushing elements onto doubleStack 1.1 2.2 3.3 4.4 5.5 Stack is full. Cannot push 6.6 Popping elements from doubleStack 5.5 4.4 3.3 2.2 1.1 Stack is empty. Cannot pop Pushing elements onto intStack 1 2 3 4 5 6 7 8 9 10 Stack is full. Cannot push 11 Popping elements from intStack 10 9 8 7 6 5 4 3 2 1 Stack is empty. Cannot pop Program Output

  14. 12.6 Templates and Inheritance • A class template can be derived from a template class • A class template can be derived from a non-template class • A template class can be derived from a class template • A non-template class can be derived from a class template

  15. 12.7 Templates and friends • friendships allowed between a class template and • global function • member function of another class • entire class • friend functions • inside definition of class template X: • friend void f1(); • f1() a friend of all template classes • friend void f2( X< T > & ); • f2( X< int > & ) is a friend of X< int > only. The same applies for float, double, etc. • friend void A::f3(); • member function f3 of class A is a friend of all template classes

  16. 12.7 Templates and friends (II) • friend void C< T >::f4( X< T > & ); • C<float>::f4( X< float> & ) is a friend of class X<float> only • friendclasses • friend class Y; • every member function of Y a friend with every template class made from X • friend class Z<T>; • class Z<float> a friend of class X<float>, etc.

  17. 12.8 Templates and static Members • non-template class • static data members shared between all objects • template classes • each class (int, float, etc.) has its own copy of static data members • static variables initialized at file scope • each template class gets its own copy of static member functions

More Related