Chapter 3 Templates Saurav Karmakar Spring 2007. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates.

Slides:



Advertisements
Similar presentations
Introduction to C++ Templates Speaker: Bill Chapman, C++ developer, financial company in Mid-Manhattan. Can be reached via: ' This.
Advertisements

Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
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.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function 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.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
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; }
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Templates CS-341 Dick Steflik. Reuse Templates allow us to get more mileage out of the classes we create by allowing the user to supply certain attributes.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Generic Programming David Rabinowitz. June 14, 2006 Object Oriented Design Course 2 The problem Assume we have a nice Stack implementation. Our stack.
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.
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.
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’
Templates & Generic Programming Junaed Sattar November 12, 2008 Lecture 10.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Templates and Polymorphism Generic functions and classes
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan and A. Ranade.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
1 CSC241: Object Oriented Programming Lecture No 25.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Overview of C++ Templates
1 Chapter 1 C++ Templates Sections 1.6 and Templates Type-independent patterns that can work with multiple data types –Generic programming –Code.
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
Introduction to Programming Lecture 40. Class Class is a user defined data type.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
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.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Chapter 1 C++ Templates (Sections 1.6, 1.7). Templates Type-independent patterns that can work with multiple data types. Function Templates  These define.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
CMSC 341 Lecture 2. Announcements Tutors wanted Office hrs Project 1.
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
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © Curt Hill Generic Functions Separating Data Type from Logic.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
Chapter 2 Objects and Classes
C++ Templates.
Templates.
Review Bernard Chen Spring 2006.
Chapter 2 Objects and Classes
Templaets It is a new concept which enables us to define “generic class “ and “generic function” to provide the “ generic programming” We are familiar.
Introduction to Programming
CMSC 202 Lesson 22 Templates I.
Templates. Templates Example: Swap Template Mechanism.
Introduction to Programming
Templates I CMSC 202.
C++ Templates An Introduction to Generic Programming.
Presentation transcript:

Chapter 3 Templates Saurav Karmakar Spring 2007

Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes

3.1 What is a Template A mechanism for writing routines that work for arbitrary types w/o knowing these types (type independent). Most likely be seen in generic algorithm implementations, i.e. find max, swapping, sorting & searching.

3.2 Function Templates typedef: keyword for defining new type from an existing one.Ex: typedef double Object; void swap( Object &lhs, Object &rhs){ Object temp = lhs; lhs = rhs; rhs = temp;}

3.2 Function Templates A function template is not an actual function, instead it’s a design or a pattern, for what could become an actual function. A function template is a design or pattern for a function which allows processors to generate an actual function from this design.

Function template example: Swap routine typedef double Object; void swap(Object & lhs, Object & rhs) { Object tmp = lhs; lhs = rhs; rhs = tmp; } // figure 3.1 Note: swap is part of the STL template void swap(Object & lhs, Object & rhs) { Object tmp = lhs; lhs = rhs; rhs = tmp; }// figure 3.2

Swap routine used in main function: int main(){ int x =5, y = 7; double a = 2, b = 4; swap (x,y); // swap(int,int) swap(x,y); //reuse previous instantiation swap(a,b); //swap(double, double) //swap(x, b); // illegal: no match return 0; } // figure 3.3

3.3 A Sorting Function Template template void insertionSort(vector &a) { for(int p = 1; p < a.size(); p++) { Comparable tmp = a[p]; int j; for(j = p; j > 0 && tmp < a[j-1]; j--) a[j] = a [j –1]; a[j] = tmp; }

Insertion Sort example The given insertionSort routine work for double, int, float but not char*, (primitive string), because operator “=” and “<” are undefined.

3.4 Class Templates A class can be a template. Example: vector is a class template C++ Class Templates are used where we have multiple copies of code for different data types with the same logic. If a set of functions or classes have the same functionality for different data types, they becomes good candidates for being written as Templates. When possible, constant reference should be used instead of call by value because if object is a large object, making a copy could be inefficient. (or illegal if copy constructor is disable or not defined)

A class template example: template class MemoryCell{ public: explicit MemoryCell(const Object & initVal = Object()) :storedValue(initVal){} const Object & read() const {return storedValue;} void write(const Object & x) {storedValue = x;} private: Object storedValue; }; // figure 3.8

Typical template interface template class ClassName{ public: //public members private: //private member };

Typical member implementation template ReturnType ClassName ::memberName(p arameterList) /*const*/ { // member body }

template class MemoryCell { public: explicit MemoryCell(const Object & initVal = Object()); const Object & read() const; const write(const Object & x); private: Object storedValue; };// figure 3.10 Interface of the template

Summary Discussion of template facilities Template for generic algorithms