 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.

Slides:



Advertisements
Similar presentations
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved Multiple Inheritance Multiple inheritence –Derived class has several base classes –Powerful, but.
14 Templates. OBJECTIVES In this chapter you will learn:  To use function templates to conveniently create a group of related (overloaded) functions.
 2006 Pearson Education, Inc. All rights reserved Templates.
 2006 Pearson Education, Inc. All rights reserved Templates.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Function and Class Templates.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 18 - Operator Overloading Outline 18.1Introduction 18.2Fundamentals of Operator Overloading 18.3Restrictions.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
 2006 Pearson Education, Inc. All rights reserved Generics.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
Templates Overload function: define more than one function With same function name Different parameter type Different type Different number of parameter.
Review of C++ Programming Part II Sheng-Fang Huang.
Introduction to C++ Systems Programming.
 2003 Prentice Hall, Inc. All rights reserved Linked Lists Upcoming program has two class templates –Create two class templates –ListNode data.
Templates Zhen Jiang West Chester University
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction 15.2C A Simple Program: Adding Two Integers.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Polymorphism, Template, Preprocessor Lecture 6 June 28, 2004.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
CS240 Computer Science II Function and Class Templates (Based on Deitel) Dr. Erh-Wen Hu.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
Templates Class Templates Used to specify generic class types where class members data types can be specified as parameters, e.g. here is a generic List.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Templates Lecture 10 March 23, 2004.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Template, Standard Template Library Lecture 9 March 22, 2005.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 22 - C++ Templates Outline 22.1Introduction.
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
LECTURE LECTURE 17 Templates 19 An abstract recipe for producing concrete code.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
Chapter 22 - C++ Templates Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2003 Prentice Hall, Inc. All rights reserved. 1 Ders Notu 8 - Template İçerik 11.1 Giriş 11.2 Fonksiyon Template ları 11.3 Overloading Fonksiyon Templates.
 2006 Pearson Education, Inc. All rights reserved Templates.
Templates 3 Templates and type parameters The basic idea templates is simple: we can make code depend on parameters, so that it can be used in different.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Template, Preprocessing Lecture 9 November 2, 2004.
Session 03 - Templates Outline 03.1Introduction 03.2Function Templates 03.3Overloading Template Functions 03.4Class Templates 03.5Class Templates and Non-type.
Chapter 22 - C++ Templates
Chapter 18 Introduction to Custom Templates
Introduction to C++ Systems Programming.
Chapter 14 Templates C++ How to Program, 8/e
Introduction to Custom Templates
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Introduction to Programming
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
template< class T > class Stack { public:
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Presentation transcript:

 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 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

 2003 Prentice Hall, Inc. All rights reserved 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

 2003 Prentice Hall, Inc. All rights reserved 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

 2003 Prentice Hall, Inc. All rights reserved 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 Specify types of –Arguments to function –Return type of function –Variables within function

 2003 Prentice Hall, Inc. All rights reserved. Outline 5 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 < count; i++ ) 13 cout << array[ i ] << " "; cout << endl; } // end function printArray int main() 20 { 21 const int aCount = 5; 22 const int bCount = 7; 23 const int cCount = 6; 24 Function template definition; declare single formal type parameter T. T is type parameter; use any valid identifier. If T is user-defined type, stream-insertion operator must be overloaded for class T.

 2003 Prentice Hall, Inc. All rights reserved. Outline 6 fig11_01.cpp (2 of 2) 25 int a[ aCount ] = { 1, 2, 3, 4, 5 }; 26 double b[ bCount ] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 }; 27 char c[ cCount ] = "HELLO"; // 6th position for null cout << "Array a contains:" << endl; // call integer function-template specialization 32 printArray( a, aCount ); cout << "Array b contains:" << endl; // call double function-template specialization 37 printArray( b, bCount ); cout << "Array c contains:" << endl; // call character function-template specialization 42 printArray( c, cCount ); return 0; } // end main Creates complete function-template specialization for printing array of int s: void printArray( const int *array, const int count ) { for ( int i = 0; i < count; i++ ) cout << array[ i ] << " " cout << endl; } // end function printArray Compiler infers T is double ; instantiates function-template specialization where T is double. Compiler infers T is char ; instantiates function-template specialization where T is char.

 2003 Prentice Hall, Inc. All rights reserved. Outline 7 fig11_01.cpp output (1 of 1) Array a contains: Array b contains: Array c contains: H E L L O

 2003 Prentice Hall, Inc. All rights reserved Overloading Function Templates Related function-template specializations –Same name Compiler uses overloading resolution Function template overloading –Other function templates with same name Different parameters –Non-template functions with same name Different function arguments –Compiler performs matching process Tries to find precise match of function name and argument types If fails, function template –Generate function-template specialization with precise match

 2003 Prentice Hall, Inc. All rights reserved Class Templates Stack –LIFO (last-in-first-out) structure Class templates –Generic programming –Describe notion of stack generically Instantiate type-specific version –Parameterized types Require one or more type parameters –Customize “generic class” template to form class-template specialization

 2003 Prentice Hall, Inc. All rights reserved. Outline 10 tstack1.h (1 of 4) 1 // Fig. 11.2: tstack1.h 2 // Stack class template. 3 #ifndef TSTACK1_H 4 #define TSTACK1_H 5 6 template 7 class Stack { 8 9 public: 10 Stack( int = 10 ); // default constructor (stack size 10) // destructor 13 ~Stack() 14 { 15 delete [] stackPtr; } // end ~Stack destructor bool push( const T& ); // push an element onto the stack 20 bool pop( T& ); // pop an element off the stack 21 Specify class-template definition; type parameter T indicates type of Stack class to be created. Function parameters of type T.

 2003 Prentice Hall, Inc. All rights reserved. Outline 11 tstack1.h (2 of 4) 22 // determine whether Stack is empty 23 bool isEmpty() const 24 { 25 return top == -1; } // end function isEmpty // determine whether Stack is full 30 bool isFull() const 31 { 32 return top == size - 1; } // end function isFull private: 37 int size; // # of elements in the stack 38 int top; // location of the top element 39 T *stackPtr; // pointer to the stack }; // end class Stack 42 Array of elements of type T.

 2003 Prentice Hall, Inc. All rights reserved. Outline 12 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 } // end Stack constructor // 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 } // end if return false; // push unsuccessful } // end function push 67 Member functions preceded with header template Use binary scope resolution operator ( :: ) with class- template name ( Stack ) to tie definition to class template’s scope. Constructor creates array of type T. For example, compiler generates stackPtr = new T[ size ]; for class-template specialization Stack.

 2003 Prentice Hall, Inc. All rights reserved. Outline 13 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 } // end if return false; // pop unsuccessful } // end function pop #endif Member function preceded with header template Use binary scope resolution operator ( :: ) with class- template name ( Stack ) to tie definition to class template’s scope.

 2003 Prentice Hall, Inc. All rights reserved. Outline 14 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 int main() 12 { 13 Stack doubleStack( 5 ); 14 double doubleValue = 1.1; cout << "Pushing elements onto doubleStack\n"; while ( doubleStack.push( doubleValue ) ) { 19 cout << doubleValue << ' '; 20 doubleValue += 1.1; } // end while cout << "\nStack is full. Cannot push " << doubleValue 25 << "\n\nPopping elements from doubleStack\n"; Link to class template definition. Instantiate object of class Stack. Invoke function push of class-template specialization Stack.

 2003 Prentice Hall, Inc. All rights reserved. Outline 15 fig11_03.cpp (2 of 3) while ( doubleStack.pop( doubleValue ) ) 28 cout << doubleValue << ' '; cout << "\nStack is empty. Cannot pop\n"; Stack intStack; 33 int intValue = 1; 34 cout << "\nPushing elements onto intStack\n"; while ( intStack.push( intValue ) ) { 37 cout << intValue << ' '; 38 ++intValue; } // end while cout << "\nStack is full. Cannot push " << intValue 43 << "\n\nPopping elements from intStack\n"; while ( intStack.pop( intValue ) ) 46 cout << intValue << ' '; cout << "\nStack is empty. Cannot pop\n"; return 0; Invoke function pop of class- template specialization Stack. Note similarity of code for Stack to code for Stack.

 2003 Prentice Hall, Inc. All rights reserved. Outline 16 fig11_03.cpp (3 of 3) fig11_03.cpp output (1 of 1) } // end main Pushing elements onto doubleStack Stack is full. Cannot push 6.6 Popping elements from doubleStack Stack is empty. Cannot pop Pushing elements onto intStack Stack is full. Cannot push 11 Popping elements from intStack Stack is empty. Cannot pop

 2003 Prentice Hall, Inc. All rights reserved. Outline 17 fig11_04.cpp (1 of 2) 1 // Fig. 11.4: fig11_04.cpp 2 // Stack class template test program. Function main uses a 3 // function template to manipulate objects of type Stack. 4 #include 5 6 using std::cout; 7 using std::cin; 8 using std::endl; 9 10 #include "tstack1.h" // Stack class template definition // 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 << "\nPushing elements onto " << stackName << '\n'; while ( theStack.push( value ) ) { 23 cout << value << ' '; 24 value += increment; } // end while Function template to manipulate Stack eliminates similar code from previous file for Stack and Stack.

 2003 Prentice Hall, Inc. All rights reserved. Outline 18 fig11_04.cpp (2 of 2) cout << "\nStack is full. Cannot push " << value 29 << "\n\nPopping elements from " << stackName << '\n'; while ( theStack.pop( value ) ) 32 cout << value << ' '; cout << "\nStack is empty. Cannot pop\n"; } // end function testStack int main() 39 { 40 Stack doubleStack( 5 ); 41 Stack intStack; testStack( doubleStack, 1.1, 1.1, "doubleStack" ); 44 testStack( intStack, 1, 1, "intStack" ); return 0; } // end main

 2003 Prentice Hall, Inc. All rights reserved. Outline 19 fig11_04.cpp output (1 of 1) Pushing elements onto doubleStack Stack is full. Cannot push 6.6 Popping elements from doubleStack Stack is empty. Cannot pop Pushing elements onto intStack Stack is full. Cannot push 11 Popping elements from intStack Stack is empty. Cannot pop Note output identical to that of fig11_03.cpp.

 2003 Prentice Hall, Inc. All rights reserved 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

 2003 Prentice Hall, Inc. All rights reserved 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 };

 2003 Prentice Hall, Inc. All rights reserved 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

 2003 Prentice Hall, Inc. All rights reserved Templates and Friends Friendships between class template and –Global function –Member function of another class –Entire class

 2003 Prentice Hall, Inc. All rights reserved 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, … friend void A::f4(); –Member function f4 of class A friend of all class-template specializations

 2003 Prentice Hall, Inc. All rights reserved 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.

 2003 Prentice Hall, Inc. All rights reserved 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