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.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 16 Templates. Learning Objectives Function Templates – Syntax, defining – Compiler complications Class Templates – Syntax – Example: Pair class.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
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.
Templates in C++. Generic Programming Programming/developing algorithms with the abstraction of types The uses of the abstract type define the necessary.
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 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
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.
Structures Spring 2013Programming and Data Structure1.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
C/c++ 4 Yeting Ge.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 1 Overview – Chapter Section 4 Arrays and Classes (10.4)
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 8 Arrays and Strings
Templates Overload function: define more than one function With same function name Different parameter type Different type Different number of parameter.
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.
Templates and the STL.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
chap8 Chapter 8 Arrays (Hanly) chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a.
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Object Oriented Programming Elhanan Borenstein Lecture #10 copyrights © Elhanan Borenstein.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
Templates Templates for Algorithm Abstraction. Slide Templates for Algorithm Abstraction Function definitions often use application specific adaptations.
SORTING Chapter 8 CS Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Overview of C++ Templates
Templates “Generic Programming” ECE Templates A way to write code once that works for many different types of variables –float, int, char, string,
Pointers *, &, array similarities, functions, sizeof.
Polymorphism CMPS Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
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.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
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.
Week 7 Part I Kyle Dewey. Overview Code from last time Array initialization Pointers vs. arrays Structs typedef Bubble sort (if time)
C:\Temp\Templates 4 5 Use This Main Program 6.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Chapter 5 The MFC Collection Classes. How to store many data? 1. Use an Array 2. Use a Linked List value: 10 Node * p a a value: 20 Node * p value: 30.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
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 –
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
 2006 Pearson Education, Inc. All rights reserved Templates.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ REVIEW – TEMPLATES. GENERIC PROGRAMMING Programming/developing algorithms with the abstraction of types Algorithms/data is expressed “without type”
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.
Templates.
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Introduction to Programming
Introduction to Programming
CS2011 Introduction to Programming I Methods (II)
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Chapter 9: Pointers and String
C++ Templates An Introduction to Generic Programming.
Presentation transcript:

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.

Examples Of Generic Algorithm Swap the values of two variables of the same data types Find the index of the smallest value in an array. Pair up 2 values Linked list

Overloading functions for Generic Algorithms void swap (char& v1, char& v2) { char temp = v1; v1 = v2; v2 = v3; } void swap (int& v1, int& v2) { int temp = v1; v1 = v2; v2 = v3; } void double (double & v1, double & v2) { double temp = v1; v1 = v2; v2 = v3; } void swap (CAny& v1, CAny& v2) { CAny temp = v1; v1 = v2; v2 = v3; }

Template for Functions template void swap (T& v1, T& v2) { T temp = v1; v1 = v2; v2 = v3; } int main () { int i1=1, i2=2; swap (i1, i2); char c1=‘a’, c2=‘A’; swap (c1, c2); }

Template Function Notes The words template tell the compiler this is a template for a generic data type T and that T is the parametized type which can be replaced by a defined data type. T must have all of the operations used in the template function. int a[5], b[5]; … // Some code to fill up arrays swap (a, b); // NO! There is no assignment operator for arrays

Class Template //Class template for a pair of values of type T template class Pair { public: Pair(); Pair(T f, T s); void setElement (int pos, T val); T getElement (int pos); private: T first; T second; };

Class Template (cont.) template Pair ::Pair() {} template Pair ::Pair(T f, T s) { first = f; second = s; } template void Pair ::setElement (int pos, T val) { if (pos == 1) first = val; else second = val; } template T Pair ::getElement (int pos) { if (pos == 1) return first; else return second; } void main () { Pair scores; Pair seats(‘A’, ‘B’); score.setElement(1, 3); score.setElement(2, 5); char mySeat mySeat=seats.getElement(1); }

Class Template Notes The words template tell the compiler this is a template for a generic data type T and that T is the parametized type which can be replaced by a defined data type. T is used as a type in all function definitions or parameters of the class template. You can use typedef to improve readability. typedef Pairs PairsOfInt; then declare PairsOfInt score; Type T is specialized by giving a type argument to the class name in place of the T.