Bài giảng Lập trình C++ - Chương 11: Templates

Outline
11.1 Introduction
11.2 Function Templates
11.3 Overloading Function Templates
11.4 Class Templates
11.5 Class Templates and Nontype Parameters
11.6 Templates and Inheritance
11.7 Templates and Friends
11.8 Templates and static Members

 

ppt25 trang | Chuyên mục: C/C++ | Chia sẻ: dkS00TYs | Lượt xem: 1916 | Lượt tải: 1download
Tóm tắt nội dung Bài giảng Lập trình C++ - Chương 11: Templates, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
* Chapter 11 - Templates Outline11.1 	Introduction11.2 	Function Templates11.3 	Overloading Function Templates11.4 	Class Templates11.5 	Class Templates and Nontype Parameters11.6 	Templates and Inheritance11.7 	Templates and Friends11.8 	Templates and static Members * 11.1 	Introduction Templates Function templates Specify entire range of related (overloaded) functions Function-template specializations Class templates Specify entire range of related classes Class-template specializations * 11.2 	Function Templates Overloaded functions Similar operations Different types of data Function templates Identical operations Different types of data Single function template Compiler generates separate object-code functions Type checking * 11.2 	Function Templates Function-template definitions Keyword template List formal type parameters in angle brackets () Each parameter preceded by keyword class or typename class and typename interchangeable template template template Specify types of Arguments to function Return type of function Variables within function * fig11_01.cpp(1 of 2) 1 // Fig. 11.1: fig11_01.cpp 2 // Using template functions. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 // function template printArray definition 9 template 10 void printArray( const T *array, const int count ) 11 { 12 for ( int i = 0; i 7 class Stack { 8 9 public: 10 Stack( int = 10 ); // default constructor (stack size 10) 11 12 // destructor 13 ~Stack() 14 { 15 delete [] stackPtr; 16 17 } // end ~Stack destructor 18 19 bool push( const T& ); // push an element onto the stack 20 bool pop( T& ); // pop an element off the stack 21 * tstack1.h (2 of 4) 22 // determine whether Stack is empty 23 bool isEmpty() const 24 { 25 return top == -1; 26 27 } // end function isEmpty 28 29 // determine whether Stack is full 30 bool isFull() const 31 { 32 return top == size - 1; 33 34 } // end function isFull 35 36 private: 37 int size; // # of elements in the stack 38 int top; // location of the top element 39 T *stackPtr; // pointer to the stack 40 41 }; // end class Stack 42 * tstack1.h (3 of 4) 43 // constructor 44 template 45 Stack::Stack( int s ) 46 { 47 size = s > 0 ? s : 10; 48 top = -1; // Stack initially empty 49 stackPtr = new T[ size ]; // allocate memory for elements 50 51 } // end Stack constructor 52 53 // push element onto stack; 54 // if successful, return true; otherwise, return false 55 template 56 bool Stack::push( const T &pushValue ) 57 { 58 if ( !isFull() ) { 59 stackPtr[ ++top ] = pushValue; // place item on Stack 60 return true; // push successful 61 62 } // end if 63 64 return false; // push unsuccessful 65 66 } // end function push 67 * tstack1.h (4 of 4) 68 // pop element off stack; 69 // if successful, return true; otherwise, return false 70 template 71 bool Stack::pop( T &popValue ) 72 { 73 if ( !isEmpty() ) { 74 popValue = stackPtr[ top-- ]; // remove item from Stack 75 return true; // pop successful 76 77 } // end if 78 79 return false; // pop unsuccessful 80 81 } // end function pop 82 83 #endif * fig11_03.cpp(1 of 3) 1 // Fig. 11.3: fig11_03.cpp 2 // Stack-class-template test program. 3 #include 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include "tstack1.h" // Stack class template definition 10 11 int main() 12 { 13 Stack doubleStack( 5 ); 14 double doubleValue = 1.1; 15 16 cout intStack; 33 int intValue = 1; 34 cout . 4 #include 5 6 using std::cout; 7 using std::cin; 8 using std::endl; 9 10 #include "tstack1.h" // Stack class template definition 11 12 // function template to manipulate Stack 13 template 14 void testStack( 15 Stack &theStack, // reference to Stack 16 T value, // initial value to push 17 T increment, // increment for subsequent values 18 const char *stackName ) // name of the Stack object 19 { 20 cout doubleStack( 5 ); 41 Stack intStack; 42 43 testStack( doubleStack, 1.1, 1.1, "doubleStack" ); 44 testStack( intStack, 1, 1, "intStack" ); 45 46 return 0; 47 48 } // end main * fig11_04.cppoutput (1 of 1) 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 * 11.5 	Class Templates and Nontype Parameters Class templates Nontype parameters Default arguments Treated as consts Example: 	template 	Stack mostRecentSalesFigures; Declares object of type Stack Type parameter Default type Example: template * 11.5 	Class Templates and Nontype Parameters Overriding class templates Class for specific type 	 Does not match common class template Example: template Class Array { 	// body of class definition }; * 11.6 	Templates and Inheritance Several ways of relating templates and inheritance Class template derived from class-template specialization Class template derived from non-template class Class-template specialization derived from class-template specialization Non-template class derived from class-template specialization * 11.7 	Templates and Friends Friendships between class template and Global function Member function of another class Entire class * 11.7 	Templates and Friends friend functions Inside definition of template class X friend void f1(); f1() friend of all class-template specializations friend void f2( X & ); f2( X & ) friend of X only, 	f2( X & ) friend of X only, 	f2( X & ) friend of X only, 	… friend void A::f4(); Member function f4 of class A friend of all class-template specializations * 11.7 	Templates and Friends friend functions Inside definition of template class X friend void C::f5( X & ); Member function C::f5( X & ) friend of class X only friend classes Inside definition of template class X friend class Y; Every member function of Y friend of every class-template specialization friend class Z; class Z friend of class-template specialization X, etc. * 11.8 	Templates and static Members Non-template class static data members shared between all objects Class-template specialization Each has own copy of static data members static variables initialized at file scope Each has own copy of static member functions 

File đính kèm:

  • pptBài giảng Lập trình C++ - Chương 11 Templates.ppt
Tài liệu liên quan