Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Data Structures Using C++ 2E
TEMPLATES Lecture Presented By SHERY KHAN Object Orienting Programming.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 14: Overloading and Templates
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
 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++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 16 Exceptions,
Basic C++ Sequential Container Features
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Data Structures Using C++ 2E
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
Data Structures Using C++ 2E Chapter 13 Standard Template Library (STL) II.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
Data Structures Using C++1 Chapter 4 Standard Template Library (STL)
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
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.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
1 Chapter 1 C++ Templates Sections 1.6 and Templates Type-independent patterns that can work with multiple data types –Generic programming –Code.
Lecture 7 : Intro. to STL (Standard Template Library)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Standard Template Library (STL) - Use Vector and Deque
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
C++ Review STL CONTAINERS.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Programming in C++ Michal Brabec Petr Malý. Standard Template Library Containers - vector, map, set, deque, list Algorithms - copy, replace, sort, find.
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 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
 2006 Pearson Education, Inc. All rights reserved Templates.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
CS212: Object Oriented Analysis and Design
Standard Template Library
Exceptions, Templates, and the Standard Template Library (STL)
Standard Template Library (STL)
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Standard Version of Starting Out with C++, 4th Edition
Exceptions, Templates, and the Standard Template Library (STL)
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Presentation transcript:

Chapter 17 – Templates

Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type sum (Type a, Type b) { return (a + b); }

Function Templates u Called in same manner as ordinary function u Permissible to have both generic data types and ordinary data types u Treated similar to overloaded function u Need to be careful that function call data types compatible with function bodies u Useful for operations that apply to many data types Lesson 17.1

Overloaded Function Templates u Cannot replace overloaded functions u Perform same operations for each different data type u Can vary number of arguments u Specify more than one type of argument –Distinguished by number or distribution of types of arguments Lesson 17.1

Class Templates: Example Lesson 17.2 template class Class1 { private: Type value; public: Class1 ( ); void set_value (Type); Type get_value ( ); }; Name of class Data member Constructor Member functions

Class Template u Allows creation of object of class and use the data type of choice u Syntax to declare object –Class1 ob; –Indicates the ob.value is type double Lesson 17.2

Mechanics of Class Templates u Declaration for object using class template –Causes memory reserved for all data members –Causes instructions to be generated and stored for all function members u If another object with same bracketed data type declared –New memory reserved, but no new function instructions Lesson 17.2

Friends of Class Templates u Four cases (assuming single type parameter) –Ordinary function friend of each template class instantiated from class template –Ordinary class friend of each template class instantiated from class template –Template function – only if type parameter for function and class same –Template class – only matching type class is friend Lesson 17.2

Sequence Containers u Designed to directly control position of element within container u Three containers –vector –deque –list u Dynamic memory allocation used to reserve memory Lesson 17.3 Standard Template Library

Vectors u Need to include header vector vector1; –Declares vector1 to be vector container of int u Elements in contiguous memory locations –First element has subscript 0 u Can be accessed using array-like notation –“push” family of functions reserve memory and initialize single element u Random access Lesson 17.3

Deques u Need to include header deque deque1; –Declares deque1 to be deque container of char u Can be created using push_front( ) and push_back ( ) u Elements in contiguous memory locations u Can modify values with array notation –First element, subscript 0 u Random Access Lesson 17.3

Lists u Need to include the header list list1; –Declares list1 to be list container of doubles u Called doubly linked list –Two pointer values: one to next element and another to previous element u Not stored in contiguous memory Lesson 17.3

Iterators u Designed to be user-friendly pointers –Know type of container u Can go through list with ++ operator u General form for declaring container :: iterator name; u ordinary iterator needs no special header file Lesson 17.4 Standard Template Library

Using an Iterator u Need to initialize to point to location first then manipulate u begin ( ) member function returns object that points to memory location of first element u Can access element pointed to by iterator using unary * operator Lesson 17.4

Constant Iterators u General form or declaring container :: const_iterator name; Lesson 17.4 Type of container such as list, vector or deque Data type of container Valid identifier for iterator

List Iterators and Operators u Called bidirectional iterators u Cannot advance more than one element at a time u Use both ++ and - - to more forward and backward in list u Useable operators –unary * operator, ++, --, =, ==, and != Lesson 17.4

Algorithms u Different definition than dictionary u Global template functions designed to work with containers using iterators u Not member functions u called with function name and argument list name (iterator1, iterator2, iterator3); –name is name of algorithm –iterator1, iterator2, iterator3 names of iterators or return values from member functions Lesson 17.5 Standard Template Library

Summary u Function templates u Class templates u Three types of sequences containers are vector, deque and list u Basic components of STL are iterators, algorithms and containers u STL has both sequence and associative containers