 2006 Pearson Education, Inc. All rights reserved. 1 14 Templates.

Slides:



Advertisements
Similar presentations
TEMPLATES Lecture Presented By SHERY KHAN Object Orienting Programming.
Advertisements

1 Templates Chapter What You Will Learn Using function templates to created a group of overloaded functions Using class templates to create a group.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Functions.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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 Generics Many slides modified by Prof. L. Lilien (even many without an explicit message). Slides.
 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.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
1 L39 Generics (1). 2 OBJECTIVES  To create generic methods that perform identical tasks on arguments of different types.  To create a generic Stack.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
 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.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
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.
 2008 Pearson Education, Inc. All rights reserved Function Call Stack and Activation Records Data structure: collection of related data items.
 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.
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.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
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.
March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
© 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.
 Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
Templates Where the TYPE is generic. Templates for functions Used when the you want to perform the same operation on different data types. The definition.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
 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.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Template, Preprocessing Lecture 9 November 2, 2004.
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
Chapter 22 - C++ Templates
Functions.
How to be generic Lecture 10
Chapter 18 Introduction to Custom Templates
Chapter 14 Templates C++ How to Program, 8/e
Introduction to Custom Templates
CS212: Object Oriented Analysis and Design
Templates.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Introduction to Classes and Objects
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.
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:

 2006 Pearson Education, Inc. All rights reserved Templates

 2006 Pearson Education, Inc. All rights reserved Introduction 14.2 Function Templates 14.3 Overloading Function Templates 14.4 Class Templates 14.5 Nontype Parameters and Default Types for Class Templates 14.6 Notes on Templates and Inheritance 14.7 Notes on Templates and Friends 14.8 Notes on Templates and static Members 14.9 Wrap-Up

 2006 Pearson Education, Inc. All rights reserved Introduction Function templates and class templates – Enable programmers to specify an entire range of related functions and related classes Called function-template specializations and class-template specializations, respectively – Generic programming – Analogy: templates are like stencils, template specializations are like separate tracings All tracings have the same shape, but they could have different colors

 2006 Pearson Education, Inc. All rights reserved Function Templates Function Templates – Used to produce overloaded functions that perform identical operations on different types of data Programmer writes a single function-template definition Compiler generates separate object-code functions (function- template specializations) based on argument types in calls to the function template – Similar to macros in C, but with full type checking

 2006 Pearson Education, Inc. All rights reserved Function Templates (Cont.) Function-template definitions – Preceded by a template header Keyword template List of template parameters – Enclosed in angle brackets ( ) – Each template parameter is preceded by keyword class or keyword typename (both are interchangeable) – Used to specify types of arguments to, local variables in and return type of the function template Examples – template

 2006 Pearson Education, Inc. All rights reserved. 6 Outline fig14_01.cpp (1 of 2) Type template parameter T specified in template header

 2006 Pearson Education, Inc. All rights reserved. 7 Outline fig14_01.cpp (2 of 2) Creates a function-template specialization of printArray where int replaces T Creates a function-template specialization of printArray where double replaces T Creates a function-template specialization of printArray where char replaces T

 2006 Pearson Education, Inc. All rights reserved Overloading Function Templates A function template may be overloaded – Other function templates that specify the same name but different parameters – Nontemplate functions that specify the same name but different parameters – The compiler chooses the best function or specialization to match the function call A nontemplate function is chosen over a template specialization in case of a tie Otherwise, multiple matches results in a compilation error (the compiler considers the function call to be an ambiguous function call )

 2006 Pearson Education, Inc. All rights reserved Class Templates Class templates (or parameterized types) – Class-template definitions are preceded by a header Such as template – Type parameter T can be used as a data type in member functions and data members – Additional type parameters can be specified using a comma-separated list As in template

 2006 Pearson Education, Inc. All rights reserved. 10 Outline Stack.h (1 of 3) Create class template Stack with type parameter T Member functions that use type parameter T in specifying function parameters

 2006 Pearson Education, Inc. All rights reserved. 11 Outline Stack.h (2 of 3) Data member stackPtr is a pointer to a T Member-function template definitions that appear outside the class-template definition begin with the template header

 2006 Pearson Education, Inc. All rights reserved. 12 Outline Stack.h (3 of 3)

 2006 Pearson Education, Inc. All rights reserved. 13 Outline fig14_03.cpp (1 of 3) Create class-template specialization Stack where type double is associated with type parameter T

 2006 Pearson Education, Inc. All rights reserved. 14 Outline fig14_03.cpp (2 of 3) Create class-template specialization Stack where type int is associated with type parameter T

 2006 Pearson Education, Inc. All rights reserved. 15 Outline fig14_03.cpp (3 of 3)

 2006 Pearson Education, Inc. All rights reserved. 16 Outline fig14_04.cpp (1 of 3) Use a function template to process Stack class-template specializations

 2006 Pearson Education, Inc. All rights reserved. 17 Outline fig14_04.cpp (2 of 3)

 2006 Pearson Education, Inc. All rights reserved. 18 Outline fig14_04.cpp (3 of 3)

 2006 Pearson Education, Inc. All rights reserved Nontype Parameters and Default Types for Class Templates Nontype template parameters – Can have default arguments – Are treated as const s – Example Template header: template Declaration: Stack salesFigures; Type parameters can have default arguments too – Example Template header: template Declaration: Stack<> jobDescriptions;

 2006 Pearson Education, Inc. All rights reserved Nontype Parameters and Default Types for Class Templates (Cont.) Explicit specializations – Used when a particular type will not work with the general template or requires customized processing – Example for an explicit Stack specialization template<> class Stack { … }; – Are complete replacements for the general template Do not use anything from the original class template and can even have different members

 2006 Pearson Education, Inc. All rights reserved Notes on Templates and Inheritance Templates and inheritance – A class template can be derived from a class-template specialization – A class template can be derived from a nontemplate class – A class-template specialization can be derived from a class- template specialization – A nontemplate class can be derived from a class-template specialization

 2006 Pearson Education, Inc. All rights reserved Notes on Templates and Friends Templates and friends – Assume class template X with type parameter T as in: template class X A function can be the friend of every class-template specialization instantiated from a class template – friend void f1(); f1 is a friend of X, X, etc. A function can be the friend of only a class-template specialization with the same type argument – friend void f2( X & ); f2( X & ) is a friend of X but not a friend of X

 2006 Pearson Education, Inc. All rights reserved Notes on Templates and Friends (Cont.) A member function of another class can be the friend of every class-template specialization instantiated from a class template – friend void A::f3(); f3 of class A is a friend of X, X, etc. A member function of another class can be the friend of only a class-template specialization with the same type argument – friend void C ::f4( X & ); C ::f4( X & ) is a friend of X but not a friend of X

 2006 Pearson Education, Inc. All rights reserved Notes on Templates and Friends (Cont.) Another class can be the friend of every class-template specialization instantiated from a class template – friend class Y; Every member function of class Y is a friend of X, X, etc. A class-template specialization can be the friend of only a class-template specialization with the same type parameter – friend class Z ; Class-template specialization Z is a friend of X, Z is a friend of X, etc.

 2006 Pearson Education, Inc. All rights reserved Notes on Templates and static Members static data members of a class template – Each class-template specialization has its own copy of each static data member All objects of that specialization share that one static data member static data members must be defined and, if necessary, initialized at file scope – Each class-template specialization gets its own copy of the class template ’ s static member functions