Template Functions Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation.

Slides:



Advertisements
Similar presentations
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Advertisements

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.
. 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 
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Templates. Example… A useful routine to have is void swap(int &a, int &b){ int tmp = a; a = b; b = tmp; }
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Templates & Generic Programming Junaed Sattar November 12, 2008 Lecture 10.
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
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.
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.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
This lecture introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation shows how.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
CSC212 Data Structure - Section FG Lecture 11 Templates, Iterators and STL Instructor: Professor Zhigang Zhu Department of Computer Science City College.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes.
 Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes.  This presentation shows how.
Chapter 8 Writing Generic Functions. Objectives Understand the use of generic functions. Learn about the use of templates, their advantages and pitfalls.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Motivation for Generic Programming in C++
“Generic Programming” ECE 297
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Andy Wang Object Oriented Programming in C++ COP 3330
Programming with ANSI C ++
User-Written Functions
C++ Templates.
Templates.
Object Oriented Programming
Exceptions, Templates, and the Standard Template Library (STL)
References as Function Arguments
Templates, Iterators and STL
Chapter 14 Templates C++ How to Program, 8/e
Chapter 5 Function Basics
Starting Out with C++ Early Objects Eighth Edition
Templates.
Template Functions Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation.
ADT Implementations: Templates and Standard Containers
Object Oriented Programming COP3330 / CGS5409
Chapter 5 Function Basics
Template Functions Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation.
CMSC 202 Templates.
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Multiset Class and its Iterator
Andy Wang Object Oriented Programming in C++ COP 3330
Andy Wang Data Structures, Algorithms, and Generic Programming
Chapter 5 – 9 CSC212 RS April 08, CS Dept, CCNY
Constructors and Other Tools
Abstraction: Generic Programming, pt. 2
Standard Version of Starting Out with C++, 4th Edition
Exceptions, Templates, and the Standard Template Library (STL)
Functions Divide and Conquer
Really reusable software
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
ENERGY 211 / CME 211 Lecture 30 December 5, 2008.
Templates CMSC 202, Version 4/02.
COP 3330 Object-oriented Programming in C++
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Template Functions Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation shows how to implement and use the simplest kinds of templates: template functions. Templates are an important part of C++ that allows a programmer to reuse existing code for new purposes. In some sense, templates ensure that you don’t have to continually “reinvent the wheel.” This lecture introduces how to implement and use template functions. The best time for the lecture is just before the students read Chapter 6. CHAPTER 6 Data Structures and Other Objects

Finding the Maximum of Two Integers Here’s a small function that you might write to find the maximum of two integers. int maximum(int a, int b) { if (a > b) return a; else return b; } To illustrate template functions, let’s look at this simple function. It would appear as part of a program that needs to find the larger of two integers. The function returns a copy of the larger of its two arguments.

Finding the Maximum of Two Doubles Here’s a small function that you might write to find the maximum of two double numbers. int maximum(double a, double b) { if (a > b) return a; else return b; } Suppose your program also needs to find the larger of two double numbers. Then you could add a second function for double numbers. By the way, you may use the same name, maximum, for the two functions. The compiler is smart enough to determine which function your program is using at any given point, by looking at the types of the parameters.

Finding the Maximum of Two Knafns Here’s a small function that you might write to find the maximum of two knafns. int maximum(knafn a, knafn b) { if (a > b) return a; else return b; } Suppose your program also has another data type, called knafn. This could be a class that you wrote yourself for some purpose. Anyway, perhaps knafn objects can be compared with the > operation (because the knafn implementation includes an overloaded > operator). In this case, your program might have a third function to compare two knafn objects.

One Hundred Million Functions... Suppose your program uses 100,000,000 different data types, and you need a maximum function for each... int maximum(Hoo a, Hoo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Hoo a, Hoo b) { if (a > b) return a; else return b; } int maximum(Moo a, Moo b) { if (a > b) return a; else return b; } int maximum(Hoo a, Hoo b) { if (a > b) return a; else return b; } int maximum(Noo a, Noo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Noo a, Noo b) { if (a > b) return a; else return b; } int maximum(Moo a, Moo b) { if (a > b) return a; else return b; } int maximum(Noo a, Noo b) { if (a > b) return a; else return b; } int maximum(Moo a, Moo b) { if (a > b) return a; else return b; } int maximum(Foo a, Foo b) { if (a > b) return a; else return b; } int maximum(Foo a, Foo b) { if (a > b) return a; else return b; } int maximum(Foo a, Foo b) { if (a > b) return a; else return b; } int maximum(Poo a, Poo b) { if (a > b) return a; else return b; } int maximum(Poo a, Poo b) { if (a > b) return a; else return b; } int maximum(Boo a, Boo b) { if (a > b) return a; else return b; } int maximum(Poo a, Poo b) { if (a > b) return a; else return b; } int maximum(Koo a, Koo b) { if (a > b) return a; else return b; } int maximum(Boo a, Boo b) { if (a > b) return a; else return b; } In fact, for your next programming assignment, I’m going to give you a printed list of 100,000,000 different data types, each of which has the > operator defined. Your job will be to implement a maximum function for each of the types. How long do you think this job will take? How many of you think it will take more than one day’s work? Raise your hands. Okay, now how many of you think you’ll need less than a day--in fact less than five minutes? Raise your hands. These are the people that have been reading ahead in Chapter 6 about a feature called template functions. int maximum(Boo a, Boo b) { if (a > b) return a; else return b; } int maximum(Joo a, Joo b) { if (a > b) return a; else return b; } int maximum(Koo a, Koo b) { if (a > b) return a; else return b; } int maximum(Koo a, Koo b) { if (a > b) return a; else return b; } int maximum(Ioo a, Ioo b) { if (a > b) return a; else return b; } int maximum(Joo a, Joo b) { if (a > b) return a; else return b; } int maximum(Knafn a, Knafn b) { if (a > b) return a; else return b; } int maximum(Joo a, Joo b) { if (a > b) return a; else return b; } int maximum(Knafn a, Knafn b) { if (a > b) return a; else return b; } int maximum(Ioo a, Ioo b) { if (a > b) return a; else return b; } int maximum(Knafn a, Knafn b) { if (a > b) return a; else return b; } int maximum(Ioo a, Ioo b) { if (a > b) return a; else return b; } int maximum(Coo a, Coo b) { if (a > b) return a; else return b; } int maximum(Coo a, Coo b) { if (a > b) return a; else return b; } int maximum(Coo a, Coo b) { if (a > b) return a; else return b; } int maximum(Loo a, Loo b) { if (a > b) return a; else return b; } int maximum(Goo a, Goo b) { if (a > b) return a; else return b; } int maximum(Loo a, Loo b) { if (a > b) return a; else return b; } int maximum(Goo a, Goo b) { if (a > b) return a; else return b; } int maximum(Loo a, Loo b) { if (a > b) return a; else return b; } int maximum(Goo a, Goo b) { if (a > b) return a; else return b; }

A Template Function for Maximum This template function can be used with many data types. template <class Item> Item maximum(Item a, Item b) { if (a > b) return a; else return b; } This is our first example of a template function. It is a single function that depends on an underlying data type that I’ve called Item. The actual Item type could be any of many different types: int, double, char-- in fact any type that has two features: (1) the data values can be compared with the > operator, and (2) the data type has a copy constructor. (By the way, where is the copy constructor needed? It is needed to initialized the two parameters from their actual arguments, and also needed when the function returns a value.) Anyway, let’s look at the pattern for converting an ordinary function to a template function...

A Template Function for Maximum When you write a template function, you choose a data type for the function to depend upon... template <class Item> Item maximum(Item a, Item b) { if (a > b) return a; else return b; } Our maximum function depends on the type of the item that we are comparing. This is the type of the two parameters, and also the return type. So, in the template function, we choose a name for this type--we use the name Item--and you use that name instead of the actual data type name.

A Template Function for Maximum A template prefix is also needed immediately before the function’s implementation: template <class Item> Item maximum(Item a, Item b) { if (a > b) return a; else return b; } Before an implementation, a special template prefix is also require. The prefix consists of the keyword template followed by angle brackets. Inside the angle brackets is the keyword class and the name that you choose to describe the underlying data type.

Using a Template Function Once a template function is defined, it may be used with any adequate data type in your program... template <class Item> Item maximum(Item a, Item b) { if (a > b) return a; else return b; } cout << maximum(1,2); cout << maximum(1.3, 0.9); ... Here are two examples of how the single template function can be used in two different ways in a program. In the first example, the compiler sees two integer arguments, so the compiler will automatically create a version of the function where the Item is an int. In the second example, the compiler sees two double arguments, so the compiler will automatically create a version of the function where the Item is a double. We could have a third example where the two arguments were Knafn objects, and the compiler would automatically create a version of the function where the Item is a Knafn.

Finding the Maximum Item in an Array Here’s another function that can be made more general by changing it to a template function: int array_max(int data[ ], size_t n) { size_t i; int answer; assert(n > 0); answer = data[0]; for (i = 1; i < n; i++) if (data[i] > answer) answer = data[i]; return answer; } Here’s another function that can be improved by making it into a template function. In this form, the function can find the largest integer in an array of integers. But it can’t be used for an array of double numbers, or an array of some other kind of objects. In this case, the function depends on the underlying data type of the components of the array. We can call this component type Item, and rewrite the function as a template function...

Finding the Maximum Item in an Array Here’s the template function: template <class Item> Item array_max(Item data[ ], size_t n) { size_t i; Item answer; assert(n > 0); answer = data[0]; for (i = 1; i < n; i++) if (data[i] > answer) answer = data[i]; return answer; } Here’s the template version. Notice these things: 1. the template prefix 2. The change of the data type of at least one parameter (the array) 3. The change of the return type (this might not occur for every template function) 4. In the implementation, we can use the Item type for local variables

C++ Standard Library <algorithm> contains some template function swap max min

Parameter Matching for Templates Template parameter must appear in parameter list of template function Template instantiation – compiler selects data type so that have an exact match with the type of the formal arguments

Parameter Matching for Templates template<class Item> size_t index_of_maximal(const Item data[], size_t n); const size_t SIZE = 5; double data[SIZE]; …… cout << index_of_maximal(data, SIZE); //won’t work with many compilers cout << index_of_maximal(data, 5); //won’t work with many compilers template<class Item, class size_type> size_t index_of_maximal(const Item data[], size_type n); size type is const size_t size type is int

Template classes Change the template class definition, the template header Implement functions for the template class Inside the class definition, template parameters can be used freely Outside of the class definition, a template header is needed for each function Make the implementation visible: the header file must include the implementation file As a result, the implementation file is not linked in as would a regular implementation file Do Not Place Using Directives in a Template Implementation

Template Classes Example bag operator + (const bag& b1, const bag& b2) template<class Item> bag<Item> operator + (const bag<Item>& b1, const bag<Item> & b2)

Review: Iterators An iterator is an object that permits a programmer to easily step through all the items in a container, examining the items changing them. Member functions: begin end * ++

begin member function Its return value is an iterator that provides access to the first item in the container multiset<string> actors; multiset<string>::iterator role; role = actors.begin();

end member function Its return value is an iterator that provides access to the last item in the container multiset<string> actors; multiset<string>::iterator role; role = actors.end();

* operator The * operator can be used to access (cannot change) the current element of the iterator multiset<string> actors; multiset<string>::iterator role; role = actors.begin(); cout<< *role <<endl;

++ operator ++ operator can be used to move an iterator forward to the item in its collection multiset<string> actors; multiset<string>::iterator role; role = actors.begin(); ++role;

The [ … )Pattern

Pitfall Do not access an iterator’s item after reaching end() Remember end() is one location past the last item of the container

Other multiset operations != == It is an error to compare two iterators from different containers find erase multiset<int> m; multiset<int>::iterator position; position = m.find(42); if(position != m.end()) m.erase(position);

STL algorithms STL contains a group of functions in the <algorithms> to manipulate container classes copy set_union: create a union of two sets iterator set_union (iterator1 first1, iterator1 last1, iterator2 first2, iterator2 last2, iterator3 result); result is an output iterator: it can be used to change values in the collection, not just fetch them

Node Template Class Node Iterator The node_iterator is derived from std::iterator Constructor node_iterator<int> start(head_ptr); node_iterator<int> finish; node_iterator<int> position; for(position = start; position != finish; ++position) { if ((*position % 2) == 1) *postion = 0; } Operations: * ++ (prefix ++ is more efficient than postfix ++) == !=

Bag Template class with an Iterator Bag iterator begin end Declare an iterator for a bag Bag<int>::iterator position;

Summary A template function depends on an underlying data type. More complex template functions and template classes are discussed in Chapter 6. A quick summary . . .