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(

Slides:



Advertisements
Similar presentations
Chapter6 LISTS AND STRINGS. Outline 1. List Specifications 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked.
Advertisements

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.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Function and Class Templates.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Generic programming Define software components with type parameters –A sorting algorithm has the same structure, regardless of the types being sorted –Stack.
Classes: A Deeper Look Systems Programming.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Generic programming Define software components with type parameters –A sorting algorithm has the same structure, regardless of the types being sorted –Stack.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
Review of C++ Programming Part II Sheng-Fang Huang.
Intro to Generic Programming Templates and Vectors.
Templates and the STL.
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’
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
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.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Templates An introduction. Simple Template Functions template T max(T x, T y) { if (x > y) { return x; } else { return y; } } int main(void) { int x =
1 Joe Meehean.  Suppose we wanted a class to store a pair of ints  Access the first using first()  and the second using second()  Lets call it LCPair.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
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.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
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.
Templates Templates for Algorithm Abstraction. Slide Templates for Algorithm Abstraction Function definitions often use application specific adaptations.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
Functions, Scope, and The Free Store Functions Functions must be declared by a function prototype before they are invoked, return_type Function_name(type,
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
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.
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 © Curt Hill Generic Functions Separating Data Type from Logic.
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.
Motivation for Generic Programming in C++
Programming with ANSI C ++
How to be generic Lecture 10
Chapter 14 Templates C++ How to Program, 8/e
Templates.
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.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Introduction to Programming
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Queues Jyh-Shing Roger Jang (張智星)
Templates I CMSC 202.
Java Programming Language
Lab4 problems More about templates Some STL
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Templates Generic Programming.
Templates An introduction.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Presentation transcript:

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( 30 ); vector stringData( 10 ); vector is an example of a templatized class. The type of data stored in the vector is specified when we declare an instance of the vector. Templatization allows us to create collection classes that work for any type of data.

CPSC 252 Templatization Page 2 In 252 we have used a typedef to define the Item_type that is stored in a collection. typedef int Item_type; class Queue { bool enqueue(const Item_type &item); Item_type dequeue(); } Issues: Changing the type of data stored in the collection means editing the.h file. We can only have collections of one kind of data at a time. In C++ we can create templatized functions and templatized classes.

CPSC 252 Templatization Page 3 Function templates – swap ints Suppose we want to swap the value of two integers: void swap( int& num1, int& num2 ) { int temp = num1; num1 = num2; num2 = temp; }

CPSC 252 Templatization Page 4 Function templates – swap strings We can overload swap void swap( string& str1, string& str2 ) { string temp = str1; str1 = str2; str2 = temp; } This gets tedious (and error prone) Templatization allows us to define a version of the swap function that will work with any data type…

CPSC 252 Templatization Page 5 Function templates – templatized swap template void swap( Item_type& item1, Item_type& item2 ) { Item_type temp = item1; item1 = item2; item2 = temp; } Note: the first line of the above function definition could be replaced with: template But the typename keyword is perhaps more precise.

CPSC 252 Templatization Page 6 Templatized functions - calling int num1 = 10, num2 = 20; string str1 = “hello”, str2 = “world”; swap( num1, num2 ); // compiler generates a // void swap( int&, int& ); // version of function swap( str1, str2 ); // compiler generates a // void swap( string&, string& ); // version of function swap( num1, str2 ); // syntax error: data types of // parameters do not match Note: the templatized function must be compiled at the same time as the client code. swap.cpp can be #include ’ d at the head of the client code.

CPSC 252 Templatization Page 7 Templatized functions – calling explicitly int num1 = 10, num2 = 20; string str1 = “hello”, str2 = “world”; swap ( num1, num2 ); swap ( str1, str2 ); This points out the problem with swap ( num1, str2 ); What type should you supply? swap ( num1, str2 );

CPSC 252 Templatization Page 8 Class Templates The declaration of a templatized version of our Queue class may look as follows: template class Queue { bool enqueue(const Item_type &item); Item_type dequeue(); } The body of the class declaration will look no different from the version that uses a typedef to define Item_type. However, the syntax used to define each of the member functions is a little more involved…

CPSC 252 Templatization Page 9 The function definition for the constructor will look as follows: template Queue ::Queue() { … } and the enqueue(…) function looks like: template bool Queue ::enqueue(const Item_type& item) { … } C++ syntax at its finest! A templatized version of our Queue class will be found on the web.

CPSC 252 Templatization Page 10 A Style Suggestion: Since the entire templatized class needs to be compiled with the client code that uses it, and since the syntax for separate method bodies is ugly, and since we normally include.h files (and not.cpp files), then one can include the bodies of the templatized functions in the class definition. Many of the examples use the convention of a.template file, which is included in lieu of a.h file.

CPSC 252 Templatization Page 11 In Queue.template (or Queue.h): template class Queue { Queue(){ front = count = 0; } bool enqueue(const Item_type& item) { if (full()) return false; // Only shuffle the elements down if you must if (front + count >= CAPACITY) { for (int i = 0; i < count; i++) data[i] = data[front + i]; front = 0; } data[front + count++] = item; return true; } … }

CPSC 252 Templatization Page 12 Using the templatized Queue class Queue intQ; Queue stringQ; intQ.enqueue(34); stringQ.enqueue(“abc”); Attempts to mix types generate compiler errors: stringQ.enqueue(34); Queue-driver.cpp:78: error: invalid conversion from `int' to `const char*' Queue-driver.cpp:78: error: initializing argument 1 of ` std::basic_string ::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator ]'