Chapter 1 C++ Templates (Sections 1.6, 1.7). Templates Type-independent patterns that can work with multiple data types. Function Templates  These define.

Slides:



Advertisements
Similar presentations
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Advertisements

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.
CMSC 202, Version 2/02 1 Operator Overloading Strong Suggestion: Go over the Array class example in Section 8.8 of your text. (You may ignore the Array.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 14: Overloading and Templates
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
. Templates. Example… A useful routine to have is void Swap( int& a, int &b ) { int tmp = a; a = b; b = tmp; }
C++ data types. Structs vs. Classes C++ Classes.
More on Operator Overloading CS-2303, C-Term More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
1 L39 Generics (1). 2 OBJECTIVES  To create generic methods that perform identical tasks on arguments of different types.  To create a generic Stack.
1 Matrix Addition, C = A + B Add corresponding elements of each matrix to form elements of result matrix. Given elements of A as a i,j and elements of.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Chapter 15: Operator Overloading
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.
OOP Languages: Java vs C++
Templates and the STL.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
1 COMS 261 Computer Science I Title: Classes Date: November 7, 2005 Lecture Number: 28.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
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.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
CIS 270—Application Development II Chapter 18-Generics.
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:
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Templates Templates for Algorithm Abstraction. Slide Templates for Algorithm Abstraction Function definitions often use application specific adaptations.
1 Chapter 1 C++ Templates Sections 1.6 and Templates Type-independent patterns that can work with multiple data types –Generic programming –Code.
Chapter 3 Templates Saurav Karmakar Spring Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
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.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Programming in C++ Michal Brabec Petr Malý. Standard Template Library Containers - vector, map, set, deque, list Algorithms - copy, replace, sort, find.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
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.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 2 Objects and Classes
Chapter 7: User-Defined Functions II
C++ for Engineers and Scientists Second Edition
C++ STL Vector Container
Constructors and Other Tools
Standard Template Library
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Data Structures and ADTs
C++ data types.
Creating and Using Classes
Presentation transcript:

Chapter 1 C++ Templates (Sections 1.6, 1.7)

Templates Type-independent patterns that can work with multiple data types. Function Templates  These define logic behind the algorithms that work for multiple data types. Class Templates  These define generic class patterns into which specific data types can be plugged in to produce new classes.

Function Templates example Generic function to find a maximum value in a given vector If argument is a vector type, then compiler generates a corresponding function which Comparable is replaced by vector type. Similarly for vector, vector etc. Assumption in this example:  Operator< is defined in the Comparable. Hence assumptions made by the template much be clearly stated.

Function Templates Usage Each call to findMax(Comparable a) on a different data type forces the compiler to generate a different function using the template. Compiler will complain about findMax(v4) because IntCell class does not define operator<

Class Template Example MemoryCell template can be used for any type Object. Assumptions  Object has a zero parameter constructor  Object has a copy constructor  Copy-assignment operator Convention  Class templates declaration and implementation usually combined in a single file.  It is not easy to separate them in independent files due to complex C++ syntax.  This is different from the convention of separating class interface and implementation in different files.

Class Template Usage Example MemoryCell can be used to store both primitive and class types. Remember  MemoryCell is not a class.  It’s a class template.  MemoryCell, MemoryCell are classes.

Another example Operator Overloading  Defines the meaning of operator< for Employee class. Output operator<<  Define a public member function print(ostream &out)  Define a global nonclass function operator<< that calls print(…)

Function Objects Objects whose primary purpose is to define a function Here findMax() accepts a Comparator parameter as a function object.  Comparator assumed to define the isLessThan(Comparator) function. There is a more formal way to define function objects in C++ (next slide).

Function objects in C++ style Define operator() for CaseInsensitiveCompare class. Case-sensitive comparison can also be performed using STL function object less (...)

Matrices C++ library does not provide a matrix class Constructor  Creates rows number of zero- sized vectors  Resizes each vector to col elements Two types of [] operators  One for LHS that returns by reference  Another for RHS that returns by constant reference  So we have two very identical functions What makes their signatures different?

Reading Assignment for next week 2.1, 2.2, 2.3, 2.4.1, 2.4.2, 2.4.3