Chapter 16 Templates. Learning Objectives Function Templates – Syntax, defining – Compiler complications Class Templates – Syntax – Example: Pair class.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Chapter 8 Operator Overloading, Friends, and References.
Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
Chapter 1 C++ Basics. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-2 Learning Objectives Introduction to C++ Origins, Object-Oriented.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Template. 2 Using templates, it is possible to create generic functions and classes. In a generic function or class, the type of data upon which the function.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
1 Templates Chapter What You Will Learn Using function templates to created a group of overloaded functions Using class templates to create a group.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Stacks and Queues. Sample PMT online… Browse 1120/sumII05/PMT/2004_1/
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Templates CS-240 Dick Steflik & DJ. Foreman. Reuse Templates allow us to get more mileage out of the classes we create by allowing the user to supply.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
Object-Oriented Programming in C++
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
Templates Templates for Algorithm Abstraction. Slide Templates for Algorithm Abstraction Function definitions often use application specific adaptations.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Copyright 2006 Pearson Addison-Wesley, 2008, 2009 Joey Paquet 9-1 Concordia University Department of Computer Science and Software Engineering COMP345.
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.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
 2006 Pearson Education, Inc. All rights reserved Templates.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CPSC 252 Templatization Page 1 Templatization In CPSC152, we saw a class vector in which we could specify the type of values that are stored: vector intData(
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objectives  Function Templates  Recursion Functions.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
C++ Template.
Pointers and Dynamic Arrays
User-Written Functions
Templates.
Inheritance and Overloading
Pointers and Pointer-Based Strings
Templates.
Chapter 17 Linked Lists.
Chapter 4 Inheritance.
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
CMSC 202 Lesson 22 Templates I.
Constructors and Other Tools
Pointers and Pointer-Based Strings
Templates I CMSC 202.
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Introduction: Some Representative Problems
Chapter 4 Constructors Section 4.4
Chapter 2 Reference Types.
Presentation transcript:

Chapter 16 Templates

Learning Objectives Function Templates – Syntax, defining – Compiler complications Class Templates – Syntax – Example: Pair class 16-2 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Introduction C++ templates – Allow very "general" definitions for functions and classes – Type names are "parameters" instead of actual types – Precise definition determined at run-time 16-3 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Function Templates Recall function swapValues: void swapValues(int& var1, int& var2) { int temp; temp = var1; var1 = var2; var2 = temp; } Applies only to variables of type int But code would work for any types! 16-4 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Function Templates vs. Overloading Could overload function for chars: void swapValues(char& var1, char& var2) { char temp; temp = var1; var1 = var2; var2 = temp; } But notice: code is nearly identical! – Only difference is type used in 3 places 16-5 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Function Template Syntax Allow "swap values" of any type variables: template void swapValues(T& var1, T& var2) { T temp; temp = var1; var1 = var2; var2 = temp; } First line called "template prefix" – Tells compiler what’s coming is "template" – And that T is a type parameter 16-6 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Template Prefix Recall: template In this usage, "class" means "type", or "classification" Can be confused with other "known" use of word "class"! – C++ allows keyword "typename" in place of keyword "class" here – But most use "class" anyway 16-7 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Template Prefix 2 Again: template T can be replaced by any type – Predefined or user-defined (like a C++ class type) In function definition body: – T used like any other type Note: can use other than "T", but T is "traditional" usage 16-8 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Function Template Definition swapValues() function template is actually large "collection" of definitions! – A definition for each possible type! Compiler only generates definitions when required – But it’s "as if" you’d defined for all types Write one definition  works for all types that might be needed 16-9 Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Calling a Function Template Consider following call: swapValues(int1, int2); – C++ compiler "generates" function definition for two int parameters using template Likewise for all other types Needn’t do anything "special" in call – Required definition automatically generated Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Another Function Template Declaration/prototype: Template void showStuff(int stuff1, T stuff2, T stuff3); Definition: template void showStuff(int stuff1, T stuff2, T stuff3) { cout << stuff1 << endl << stuff2 << endl << stuff3 << endl; } Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

showStuff Call Consider function call: showStuff(2, 3.3, 4.4); Compiler generates function definition – Replaces T with double Since second parameter is type double Displays: Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Multiple Type Parameters Can have: template Not typical – Usually only need one "replaceable" type – Cannot have "unused" template parameters Each must be "used" in definition Error otherwise! Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Algorithm Abstraction Refers to implementing templates Express algorithms in "general" way: – Algorithm applies to variables of any type – Ignore incidental detail – Concentrate on substantive parts of algorithm Function templates are one way C++ supports algorithm abstraction Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Defining Templates Strategies Develop function normally – Using actual data types Completely debug "ordinary" function Then convert to template – Replace type names with type parameter as needed Advantages: – Easier to solve "concrete" case – Deal with algorithm, not template syntax Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Inappropriate Types in Templates Can use any type in template for which code makes "sense" – Code must behave in appropriate way e.g., swapValues() template function – Cannot use type for which assignment operator isn’t defined – Example: an array: int a[10], b[10]; swapValues(a, b); Arrays cannot be "assigned"! Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Class Templates Can also "generalize" classes template – Can also apply to class definition – All instances of "T" in class definition replaced by type parameter – Just like for function templates! Once template defined, can declare objects of the class Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Class Template Definition template class Pair { public: Pair(); Pair(T firstVal, T secondVal); void setFirst(T newVal); void setSecond(T newVal); T getFirst() const; T getSecond() const; private: T first; T second; }; Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Template Class Pair Members template Pair ::Pair(T firstVal, T secondVal) { first = firstVal; second = secondVal; } template void Pair ::setFirst(T newVal) { first = newVal; } Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Template Class Pair Objects of class have "pair" of values of type T Can then declare objects: Pair score; Pair seats; – Objects then used like any other objects Example uses: score.setFirst(3); score.setSecond(0); Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Pair Member Function Definitions Notice in member function definitions: – Each definition is itself a "template" – Requires template prefix before each definition – Class name before :: is "Pair " Not just "Pair" – But constructor name is just "Pair" – Destructor name is also just "~Pair" Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Class Templates as Parameters Consider: int addUP(const Pair & the Pair); – The type (int) is supplied to be used for T in defining this class type parameter – It "happens" to be call-by-reference here Again: template types can be used anywhere standard types can Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Class Templates Within Function Templates Rather than defining new overload: template T addUp(const Pair & the Pair); //Precondition: Operator + is defined for values of type T //Returns sum of two values in thePair Function now applies to all kinds of numbers Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Restrictions on Type Parameter Only "reasonable" types can be substituted for T Consider: – Assignment operator must be "well-behaved" – Copy constructor must also work – If T involves pointers, then destructor must be suitable! Similar issues as function templates Copyright © 2012 Pearson Addison-Wesley. All rights reserved.

Summary Function templates – Define functions with parameter for a type Class templates – Define class with parameter for subparts of class Copyright © 2012 Pearson Addison-Wesley. All rights reserved.