Andy Wang Data Structures, Algorithms, and Generic Programming

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
CSE 303 Lecture 16 Multi-file (larger) programs
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
C++ for Engineers and Scientists Third Edition
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.
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.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Motivation for Generic Programming in C++
Chapter 12 Classes and Abstraction
TK1924 Program Design & Problem Solving Session 2011/2012
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.
Programming Logic and Design Seventh Edition
Andy Wang Object Oriented Programming in C++ COP 3330
Functions.
Classes C++ representation of an object
Programming with ANSI C ++
User-Written Functions
Chapter 13: Overloading and Templates
C++ Templates.
Templates C++ template
Templates.
A First C++ Class – a Circle
Chapter 14 Templates C++ How to Program, 8/e
Introduction to Custom Templates
Functions Separate Compilation
Introduction to Classes
More about OOP and ADTs Classes
ADT Implementations: Templates and Standard Containers
Introduction to Classes
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science)
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
6 Chapter Functions.
More about OOP and ADTs Classes
Review for Final Exam.
Object-Oriented Programming Using C++ Second Edition
Chapter 9 Objects and Classes
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Andy Wang Object Oriented Programming in C++ COP 3330
Introduction to Classes and Objects
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Miscellaneous C++ Topics
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Review for Final Exam.
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Classes C++ representation of an object
What Is? function predefined, programmer-defined
C++ Templates An Introduction to Generic Programming.
Templates C++ template
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Lecture 8 Object Oriented Programming (OOP)
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Andy Wang Data Structures, Algorithms, and Generic Programming Templates Andy Wang Data Structures, Algorithms, and Generic Programming

Templates Provide generic programming Goal: Generic classes Generic functions Generic algorithms (later lectures) Goal: Program reuse Templates The principal mechanism in C++ for defining generics is the template. The idea behind templates is quite simple: when defining a container, for example, a parameter is used to represent the element_type; when using the container, a specific class may be substituted for the template parameter. Templates are used in defining three distinct kinds of generics: generic classes, generic functions, and generic algorithms. Generic functions and classes are discussed later in this chapter. Generic algorithms, which depend on the concept of iterator, are discussed in subsequent chapters. Any legal C++ identifier may be used to name a template parameter. Just as with ordinary identifiers, it can be useful to use parameter names that are self-documenting. In our examples, we will often use T as a template parameter, where "T" can be thought to represent "template" or "type" (or the keyword "typename"). The template parameter is used in coding the function body or class implementation as though it were a known type. Any methods or operators for T that are used in this coding will eventually be called for a specific substituted type, and the compiler will report an error if these methods or operators are not defined for the substituted type.

Motivating Example Find the largest element in an array int largest(const int *iptr, unsigned int size) { int current_largest = *iptr; for (; size > 0; --size, ++iptr) { if (current_largest < *iptr) { current_largest = *iptr; } return current_largest; Example Functions Shown in this slide are several functions called "largest". Each of these functions accomplishes essentially the same task, namely finding the largest value in an array of values. These functions differ only in the type that is passed through parameters and returned as a value. We could easily make up many more of these functions, one for almost any type or even user-defined class. The only requirement on the type is that the less-than operator <() be defined for the type. This situation is a perfect setup for templates.

Motivating Example Find the largest element in an array float largest(const float *iptr, unsigned int size) { float current_largest = *iptr; for (; size > 0; --size, ++iptr) { if (current_largest < *iptr) { current_largest = *iptr; } return current_largest;

Motivating Example Find the largest element in an array double largest(const double *iptr, unsigned int size) { double current_largest = *iptr; for (; size > 0; --size, ++iptr) { if (current_largest < *iptr) { current_largest = *iptr; } return current_largest;

Motivating Example int int_array[20]; float float_array[30]; double double_array[40]; cout << largest(int_array, 20) << “ “ << largest(float_array, 30) << “ “ << largest(double_array, 40);

Function Template Example Find the largest element in an array template <typename T> T largest(const T *iptr, unsigned int size) { T current_largest = *iptr; for (; size > 0; --size, ++iptr) { if (current_largest < *iptr) { current_largest = *iptr; } return current_largest; Function Template Example In this slide we see how to use a template parameter to define a generic function called "largest" that replaces all three of the type-specific examples of the previous slide. Moreover, whenever we need a function "largest" for a new type, whether built-in or user-defined, we can use this same generic function. All we need do is call the function for the type, and make sure the generic code is available to the compiler. The compiler handles the rest: it substitutes our type into the template and creates code for that specific "largest" function. Example use of largest() for three different types is also shown in this slide.

In-Class Exercise Swap two values of the same type Template Function Example 2 This slide shows code for another generic function that swaps the values of two variables of the same type. Note that the two parameters are passed to the function by reference, so that changes are made to the values in the calling routine.

Function Template Example 2 Swap two values of the same type template <typename T> void swap (T& t1, T& t2) { T temp; temp = t1; t1 = t2; t2 = temp; } Template Function Example 2 This slide shows code for another generic function that swaps the values of two variables of the same type. Note that the two parameters are passed to the function by reference, so that changes are made to the values in the calling routine.

Class Template Example A class that holds a pair of items template <typename T> class Pair { public: T first; T second; Pair(); Pair(T t1, T t2); }; Template Class Example This slide shows the definition and implementation of a template class called "Pair". An object of type Pair<T> is an ordered pair of T objects. For example, an object of type Pair<int> is an ordered pair of ints; an object of type Pair<widget> is an ordered pair of widgets. Declaration of such objects works as follows: Pair<int> intPair;Pair<widget> widgetPair;// yada dada Spacing may be added in template declarations for readability or style, as follows: Pair < int > intPair;Pair < widget > widgetPair;// yada dada When nesting template declarations, some spacing is essential to avoid syntactic ambiguity, as in: Pair < widget < char > > charwidgetPair;Pair < Pair < char > > intQuad;// yada dada(where widget is a template class). If the two right angle brackets are not spaced, the compiler finds the right shift operator >> (or an overload of it) in a peculiar place!

Class Template Example Constructors (in the same header file) template <typename T> Pair<T>::Pair() { } Pair<T>::Pair(T t1, T t2) : first(t1), second(t2) {

Class Template Example Declarations Pair<int> intPair(1, 2); Pair<float> floatPair(1.1, 2.2); Pair< Pair<int> > pair(intPair, intPair); You need the space

Class Template Example 2 A class that holds a pair of items with different types template <typename T1, typename T2> class Pair { public: T1 first; T2 second; Pair(); Pair(T1 t1, T2 t2); }; Template Class Example 2 This slide illustrates the use of more than one template parameter in a class definition. The example is a Pair class with different types for the two items being paired.

Class Template Example 2 Constructors template <typename T1, typename T2> Pair<T1, T2>::Pair() { } Pair<T1, T2>::Pair(T1 t1, T2 t2) : first(t1), second(t2) {

Template Code Files Place template class definitions and implementation in the same header file Protect files from nested inclusions #ifndef _YADDY_H #define _YADDY_H … #endif _YADDY_H Template Code Files Template code files follow a somewhat different set of conventions from regular code files. The fact behind the adoption of this convention is that template code cannot be translated into object code until it has first been converted into actual code by substituting a specific type for the template parameter (or types for parameters, if there are multiple template parameters). The reason is simple: object code needs to have size information about its variables in order to make a sensible memory model for the computation. Without knowledge of the type to be used for the template parameter, this is not possible. Thus template code needs to be pre-processed into real code at the source level before translation to object code can occur. The main reason for going to the trouble of separating regular code into header files and implementation files is so that the implementations can be pre-compiled to object code and stored in libraries. The header file gives the definitions of classes and prototypes of functions for client programs to use, but the implementation code is pre-translated. This saves time in compilation, and it also allows implementation source code to be kept proprietary. Because the possibility of pre-compiling the implementations of template functions and template class methods is not there, there is no reason to maintain separate files, and the custom is not to do so. We follow that custom in our course library. Here are two example file contents, one for a class template and one for a function template. First the funtcion template: /* swap.h Feb 3, 2003 Chris Lacher  Example of a template function header file*/ #ifndef _SWAP_H#define _SWAP_H template <typename T>void swap (T& t1, T& t2){ T temp; temp = t1; t1 = t2; t2 = temp;} #endif Note that there is no template function prototype, we simple write the function header and proceed directly to the implementation. The use of the #ifndef directive as illustrated in these two files serves to protect against multiple reads of the files. The effect is to define the symbol (a mutated file name) the first time the file is read and then to skip reading all subsequent times, because the symbol has been defined. The symbol serves no purpose in the actual trsanslation. /* Pair.h Feb 3, 2003 Chris Lacher  Example of a template class header file*/ #ifndef _PAIR_H#define _PAIR_H template <typename T>class Pair{ public: // no private components T first; T second; Pair(); // default constructor Pair (T t1, T t2); // constructor with parameters} ; template <typename T> // a template functionPair<T>::Pair() // scope resolution operator{}  template <typename T>Pair<T>::Pair(T t1, T t2) : first (t1), second (t2) // initialization list{} #endif Note that the method implementations are function templates and are placed in the header file following the template class definition. An alternative is to use a #include directive that pulls an implementation file into the header file, if there is some reason to have the implementation in a separate file. This makes the code "appear" to be in the same file because the #include directive is performed before translation takes place. Examples of either method of logically including class method implementations in the header file are used in the course library.

Next Two Class Meetings 9/15, 9/17 MCH 201