 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.

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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
C++ Templates Gordon College CPS212. Overview Templates Definition: a pattern for creating classes or functions as instances of the template at compile.
 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.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
Review of C++ Programming Part II Sheng-Fang Huang.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
 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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
 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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
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.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
© 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.
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.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
LECTURE LECTURE 17 Templates 19 An abstract recipe for producing concrete code.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
 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.
Templates יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום.
 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 15 - C++ As A "Better C"
Chapter 22 - C++ Templates
Chapter 18 Introduction to Custom Templates
Chapter 14 Templates C++ How to Program, 8/e
Introduction to Custom Templates
STACKS AND QUEUES UNIT 2 DS THROUGH C++.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Introduction to Programming
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 15 - C++ As A "Better C"
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:

 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions 12.4Class Templates 12.5Class Templates and Non-type Parameters 12.6Templates and Inheritance 12.7Templates and friends 12.8Templates and static Members

 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction 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

 2000 Deitel & Associates, Inc. All rights reserved. 12.2Function Templates overloaded functions –perform similar operations on different data types function templates –perform identical operations on different data types –provide type checking Format: template –can use class or typename - specifies type parameters template –Function definition follows template statement

 2000 Deitel & Associates, Inc. All rights reserved. 12.2Function Templates (II) 1template 2void printArray( const T *array, const int count ) 3{3{ 4 for ( int i = 0; i < count; i++ ) 5 cout << array[ i ] << " "; 6 7 cout << endl; 8}8} 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; } T is the type parameter. T 's type is detected and substituted inside the function. The newly created function is compiled.

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1. Function template definition 1.1 Initialize variables 2. Call template functions 3. Output 1// Fig 12.2: fig12_02.cpp 2// Using template functions 3#include 4 5using std::cout; 6using std::endl; 7 8template 9void printArray( const T *array, const int count ) 10{ 11 for ( int i = 0; i < count; i++ ) 12 cout << array[ i ] << " "; cout << endl; 15} 16 17int 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 cout << "Array a contains:" << endl; 25 printArray( a, aCount ); // integer template function cout << "Array b contains:" << endl; 28 printArray( b, bCount ); // double template function cout << "Array c contains:" << endl; 31 printArray( c, cCount ); // character template function return 0; 34} Notice how type parameter T is used in place of int, float, etc.. Each type array gets operated on by a different template function.

 2000 Deitel & Associates, Inc. All rights reserved. Outline Program Output Array a contains: Array b contains: Array c contains: H E L L O

 2000 Deitel & Associates, Inc. All rights reserved. 12.3Overloading Template Functions related template functions have same name –compiler uses overloading resolution to call the right one function template can be overloaded –other function templates can have same name but different number of parameters –non-template function can have same name but different arguments compiler tries to match function call with function name and arguments –if no precise match, looks for function templates if found, compiler generates and uses template function –if no matches or multiple matches are found, compiler gives error

 2000 Deitel & Associates, Inc. All rights reserved. 12.4Class Templates class templates –allow type-specific versions of generic classes Format: template class ClassName{ definition } –Need not use "T", any identifier will work –To create an object of the class, type ClassName myObject; Example: Stack doubleStack;

 2000 Deitel & Associates, Inc. All rights reserved. 12.4Class Templates (II) Template class functions –declared normally, but preceded by template generic data in class listed as type T –binary scope resolution operator used –Template class function definition: template MyClass ::MyClass(int size) { myArray = new T[size]; } constructor definition - creates an array of type T

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1. Class template definition 1.1 Function definitions 1.2 Stack constructor 1// Fig. 12.3: tstack1.h 2// Class template Stack 3#ifndef TSTACK1_H 4#define TSTACK1_H 5 6template 7class Stack { 8public: 9 Stack( int = 10 ); // default constructor (stack size 10) 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 13private: 14 int size; // # of elements in the stack 15 int top; // location of the top element 16 T *stackPtr; // pointer to the stack bool isEmpty() const { return top == -1; } // utility 19 bool isFull() const { return top == size - 1; } // functions 20}; 21 22// Constructor with default size 10 23template 24Stack ::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} Notice how a member function of the class template is defined

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

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1. Load header 1.1 Initialize doubleStack 1.2 Initialize variables 2. Function calls 55// Fig. 12.3: fig12_03.cpp 56// Test driver for Stack template 57#include 58 59using std::cout; 60using std::cin; 61using std::endl; 62 63#include "tstack1.h" 64 65int main() 66{ 67 Stack doubleStack( 5 ); 68 double f = 1.1; 69 cout << "Pushing elements onto doubleStack\n"; while ( doubleStack.push( f ) ) { // success true returned 72 cout << f << ' '; 73 f += 1.1; 74 } cout << "\nStack is full. Cannot push " << f 77 << "\n\nPopping elements from doubleStack\n"; while ( doubleStack.pop( f ) ) // success true returned Pushing elements onto doubleStack Stack is full. Cannot push 6.6 Popping elements from doubleStack

 2000 Deitel & Associates, Inc. All rights reserved. Outline 2. Function calls 3. Output 80 cout << f << ' '; cout << "\nStack is empty. Cannot pop\n"; Stack intStack; 85 int i = 1; 86 cout << "\nPushing elements onto intStack\n"; while ( intStack.push( i ) ) { // success true returned 89 cout << i << ' '; 90 ++i; 91 } cout << "\nStack is full. Cannot push " << i 94 << "\n\nPopping elements from intStack\n"; while ( intStack.pop( i ) ) // success true returned 97 cout << i << ' '; cout << "\nStack is empty. Cannot pop\n"; 100 return 0; 101} Stack is empty. Cannot pop Pushing elements onto intStack Popping elements from intStack Stack is full. Cannot push Stack is empty. Cannot pop

 2000 Deitel & Associates, Inc. All rights reserved. Outline Program Output 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

 2000 Deitel & Associates, Inc. All rights reserved. 12.5Class Templates and Non-type Parameters can use non-type parameters in templates –default argument –treated as const Example: template Stack mostRecentSalesFigures; declares object of type Stack –This may appear in the class definition: T stackHolder[ elements ]; //array to hold stack creates array at compile time, rather than dynamic allocation at execution time

 2000 Deitel & Associates, Inc. All rights reserved. 12.5Class Templates and Non-type Parameters (II) Classes can be overridden –for template class Array, define a class named Array –This new class overrides then class template for myCreatedType –The template remains for unoverriden types

 2000 Deitel & Associates, Inc. All rights reserved. 12.6Templates 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

 2000 Deitel & Associates, Inc. All rights reserved. 12.7Templates 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 & ); f2( X & ) is a friend of X 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

 2000 Deitel & Associates, Inc. All rights reserved. 12.7Templates and friends (II) –friend void C ::f4( X & ); C ::f4( X & ) is a friend of class X only friend classes –friend class Y; every member function of Y a friend with every template class made from X –friend class Z ; class Z a friend of class X, etc.

 2000 Deitel & Associates, Inc. All rights reserved. 12.8Templates 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