Copyright © 1999-2014 Curt Hill Generic Functions Separating Data Type from Logic.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
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.
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.
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.
1 Templates Chapter What You Will Learn Using function templates to created a group of overloaded functions Using class templates to create a group.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
5th MaCS Debrecen On the Turing-Completeness of Generative Metaprograms Zoltán Porkoláb, István Zólyomi Dept. of Programming.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Fungsi Risanuri Hidayat, Ir., M.Sc.. Functions C usually consist of two things: instance variables and functions. All C programs consist of one or more.
14 Templates. OBJECTIVES In this chapter you will learn:  To use function templates to conveniently create a group of related (overloaded) functions.
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; }
 2006 Pearson Education, Inc. All rights reserved Templates.
OOP Etgar 2008 – Recitation 51 Object Oriented Programming Etgar 2008 Recitation 5.
ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
1 L39 Generics (1). 2 OBJECTIVES  To create generic methods that perform identical tasks on arguments of different types.  To create a generic Stack.
OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Function Part II: Some ‘advanced’ concepts on functions.
 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.
Generic Subroutines and Exceptions CS351 – Programming Paradigms.
 2006 Pearson Education, Inc. All rights reserved Generics.
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.
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 © Curt Hill Sounds, Resource Packs, JSON What more would you want?
CNS  Executive Overview  Template Parameters  Function Template Issues 2 CNS Templates.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
Copyright © Curt Hill Functions Additional Topics.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
Object Oriented Programming Elhanan Borenstein Lecture #10 copyrights © Elhanan Borenstein.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
Lecture 17 Templates, Part I. What is a Template? In short, it’s a way to define generic functionality on parameters without needing to declare their.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
Copyright Curt Hill Variables What are they? Why do we need them?
March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.
Templates Templates for Algorithm Abstraction. Slide Templates for Algorithm Abstraction Function definitions often use application specific adaptations.
Copyright © – Curt Hill Pointers A Light Introduction.
Chapter 3 Templates Saurav Karmakar Spring Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates.
CSI 3125, Preliminaries, page 1 Generic Class & Generic methods.
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 enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
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.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
TEMPLATE. Introduction  Template can be assumed as one that can be used to develop a function or class.  C++ use template to achieve polymorphism that.
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(
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objectives  Function Templates  Recursion Functions.
Motivation for Generic Programming in C++
Distinguishing logic from data type
Chapter 14 Templates C++ How to Program, 8/e
Secure Coding Rules for C++ Copyright © Curt Hill
Templates.
Methods Additional Topics
More ‘concepts’ on Function
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Exception Handling.
Operator Overloading CSCE 121 Based on Slides created by Carlos Soto.
More ‘concepts’ on Function
Presentation transcript:

Copyright © Curt Hill Generic Functions Separating Data Type from Logic

Copyright © Curt Hill Consider The swap function is often used in sort functions: void swap(float &a,float &b){ float temp; temp = a; a = b; b = temp; } How is this different than a double swap function?

Copyright © Curt Hill Double Swap The double swap is exactly the same except that three instances of float would have to be replaced by double If the C preprocessor were a bit stronger, we could do this with it As it is C++ was enhanced with the template feature to allow this

Copyright © Curt Hill The template swap The swap function as a generic: template void swap(T & a, T & b){ T temp; temp = a; a = b; b = temp; } T is the generic type –It may be any type suitable for assignment

Copyright © Curt Hill Using the generic swap The client code would look like this: int i,j; double x,y;... swap(i,j); swap(x,y); The signature matching process will make the correct function

Copyright © Curt Hill Discussion Client code can not distinguish from overloaded function names and template functions Figuring out which function will actually receive the call: Given function call: x(y) –Where x is the function name –Where z is the argument type –where is some declaration: z y; though it does not have to be explicit

Copyright © Curt Hill Process First look for an exact match among non- template functions: –type x(z y); –If found call it Next look for a function template that if generated with type z could produce an exact match: –template type x(T y) {...} –The generated function must match exactly –Not even trivial conversions will be applied (ie a value parameter must match in type exactly, widening etc. is not applied) Next look for conversions that allow a non template function to work

Copyright © Curt Hill Discussion The runtime effect is exactly the same as if several versions of the function existed Conceptually easy once function name overloading is in place The reserved word in the brackets may be typename or class The type used in the function must be suitable for the kind of operations that are performed in the function

Copyright © Curt Hill Discussion of example In this example assignment only is needed –Arrays and strings may not be assigned so could not be the type –Several objects disallow assignment as well, such as ifstreams and ofstreams In the context of a sort a comparison would be required –Structs and classes are not usually suitable for greater or less comparison –However, overloading these operators works the same as if the type naturally had the operation