Chapter 22 - C++ Templates Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.

Slides:



Advertisements
Similar presentations
Chapter 20- Virtual Functions and Polymorphism Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng.
Advertisements

C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 14: Overloading and Templates
14 Templates. OBJECTIVES In this chapter you will learn:  To use function templates to conveniently create a group of related (overloaded) functions.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
 2006 Pearson Education, Inc. All rights reserved 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.
 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.
Lecture 9 Concepts of Programming Languages
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Abstract Data Types and Encapsulation Concepts
 2006 Pearson Education, Inc. All rights reserved Generics.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
Helpful C++ Transitions
Review of C++ Programming Part II Sheng-Fang Huang.
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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.
CIS 270—Application Development II Chapter 18-Generics.
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.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.
Chapter 17 - C++ Classes: Part II Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Stack.
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.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
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 7 Pointers 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.
 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.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
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++.
CS212: Object Oriented Analysis and Design
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Helpful C++ Transitions
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.
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.
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Chapter 22 - C++ Templates Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University

22.1 Introduction 22.2 Class Templates 22.3 Class Templates and Non-type Parameters 22.4 Templates and Inheritance 22.5 Templates and friends 22.6 Templates and static Members Outline

22.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

22.2Class 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;

Example of template

22.2Class 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

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

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

22.3Class 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

22.3Class 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