Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Data Structures Using C++ 2E
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Generics and the ArrayList Class
C++ is Fun – Part 11 at Turbine/Warner Bros.! Russell Hanson.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Starting Out with C++, 3 rd Edition 1 Chapter 16 – Exceptions, Templates, and the Standard Template Library (STL) Exceptions are used to signal errors.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Templates and Polymorphism Generic functions and classes.
. Templates. Example… A useful routine to have is void Swap( int& a, int &b ) { int tmp = a; a = b; b = tmp; }
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Exceptions, Templates, and the 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.
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,
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Intro to Generic Programming Templates and Vectors.
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
Templates and the STL.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
Data Structures Using C++ 2E
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Software Design 1.1 Tapestry classes -> STL l What’s the difference between tvector and vector  Safety and the kitchen sink What happens with t[21] on.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 16: Exceptions,
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.
Templates Templates for Algorithm Abstraction. Slide Templates for Algorithm Abstraction Function definitions often use application specific adaptations.
Templates “Generic Programming” ECE Templates A way to write code once that works for many different types of variables –float, int, char, string,
1 Chapter 1 C++ Templates Sections 1.6 and Templates Type-independent patterns that can work with multiple data types –Generic programming –Code.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objective  Standard Template Library.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 ENERGY 211 / CME 211 Lecture 7 October 6, 2008.
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objectives  Function Templates  Recursion Functions.
C++ Templates.
Templates.
Exceptions, Templates, and the Standard Template Library (STL)
Exceptions, Templates, and the Standard Template Library (STL)
Standard Template Library (STL)
Starting Out with C++ Early Objects Eighth Edition
Name: Rubaisha Rajpoot
Exceptions and Templates
Exception Handling.
Standard Version of Starting Out with C++, 4th Edition
Exceptions, Templates, and the Standard Template Library (STL)
Standard Template Library
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Presentation transcript:

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2

16-2 Function Templates Function template: a pattern for a function that can work with many data types When written, parameters are left for the data types When called, compiler generates code for specific data types in function call

16-3 Function Template Example template T times10(T num) { return 10 * num; } template prefix generic data type type parameter What gets generated when times10 is called with an int: What gets generated when times10 is called with a double: int times10(int num) { return 10 * num; } double times10(double num) { return 10 * num; }

16-4 Function Template Example template T times10(T num) { return 10 * num; } Call a template function in the usual manner: int ival = 3; double dval = 2.55; cout << times10(ival); // displays 30 cout << times10(dval); // displays 25.5

16-5 Function Template Notes Can define a template to use multiple data types: template Example: template // T1 and T2 will be double mpg(T1 miles, T2 gallons) // replaced in the { // called function return miles / gallons // with the data } // types of the // arguments

16-6 Function Template Notes All data types specified in template prefix must be used in template definition Like regular functions, function templates must be defined before being called

16-7 Function Template Notes A function template is a pattern No actual code is generated until the function named in the template is called A function template uses no memory When passing a class object to a function template, ensure that all operators in the template are defined or overloaded in the class definition

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Where to Start When Defining Templates 16.3

16-9 Where to Start When Defining Templates Templates are often appropriate for multiple functions that perform the same task with different parameter data types Develop function using usual data types first, then convert to a template: – add template prefix – convert data type names in the function to a type parameter (i.e., a T type) in the template

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Class Templates 16.4

16-11 Class Templates Classes can also be represented by templates. When a class object is created, type information is supplied to define the type of data members of the class. Unlike functions, classes are instantiated by supplying the type name ( int, double, string, etc.) at object definition

16-12 Class Template Example template class grade { private: T score; public: grade(T); void setGrade(T); T getGrade() };

16-13 Class Template Example Pass type information to class template when defining objects: grade testList[20]; grade quizList[20]; Use as ordinary objects once defined

16-14 Class Templates and Inheritance Class templates can inherit from other class templates: template class Rectangle {... }; template class Square : public Rectangle {... }; Must use type parameter T everywhere base class name is used in derived class

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to the Standard Template Library 16.5

16-16 Introduction to the Standard Template Library Standard Template Library (STL): a library containing templates for frequently used data structures and algorithms Not supported by many older compilers

16-17 Standard Template Library Two important types of data structures in the STL: – containers: classes that stores data and imposes some organization on it – iterators: like pointers; mechanisms for accessing elements in a container

16-18 Containers Two types of container classes in STL: – sequence containers: organize and access data sequentially, as in an array. These include vector, dequeue, and list – associative containers: use keys to allow data elements to be quickly accessed. These include set, multiset, map, and multimap

16-19 Iterators Generalization of pointers, used to access information in containers Four types: – forward (uses ++ ) – bidirectional (uses ++ and -- ) – random-access – input (can be used with cin and istream objects) – output (can be used with cout and ostream objects)

16-20 Algorithms STL contains algorithms implemented as function templates to perform operations on containers. Requires algorithm header file algorithm includes binary_searchcount for_eachfind find_ifmax_element min_elementrandom_shuffle sort and others