Template Functions Lecture 9 Fri, Feb 9, 2007.

Slides:



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

For(int i = 1; i
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.
Chapter 7 Sorting Part II. 7.3 QUICK SORT Example left right pivot i j 5 > pivot and should go to the other side. 2 < pivot and should go to.
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.
TEMPLATES Lecture Presented By SHERY KHAN Object Orienting Programming.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
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.
SORTING AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Spring 2014 File searchSortAlgorithms.zip on course website (lecture notes for lectures 12, 13) contains.
EC-241 Object-Oriented Programming
C++ Classes & Data Abstraction
C/c++ 4 Yeting Ge.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 23 : Generics King Fahd University of Petroleum & Minerals College of Computer Science.
Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
Rossella Lau Lecture 11, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 11: Template and Operator overload  Template.
1 CS 105 Lecture 12 Pass- By-Reference, 2-D Arrays Version: Wed, Apr 20, 2011, 6:25 pm.
Lecture 2: Fundamental Concepts
1 Module 2: Fundamental Concepts Problems Programs –Programming languages.
Specification and Implementation Separating the specification from implementation makes it easier to modify programs. Changes in the class’s implementation.
CS2420: Lecture 10 Vladimir Kulyukin Computer Science Department Utah State University.
Insertion Sorting Lecture 21. Insertion Sort Start from element 2 of list location 1 –In first iteration: Compare element 1 with all of its elements to.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Selection Sort
Data Structures/ Algorithms and Generic Programming Sorting Algorithms.
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.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Templates & Generic Programming Junaed Sattar November 12, 2008 Lecture 10.
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Templates L. Grewe. 2 Goals Often want to do basically the same thing w/diff things –functions work on variables only types specified –  algorithmic.
CS212: Object Oriented Analysis and Design
Selection Sort
Overview of C++ Templates
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Interfaces and Inner Classes
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
CS 162 Intro to Programming II Insertion Sort 1. Assume the initial sequence a[0] a[1] … a[k] is already sorted k = 0 when the algorithm starts Insert.
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.
Lecture 7.  There are 2 types of libraries used by standard C++ The C standard library (math.h) and C++ The C++ standard template library  Allows us.
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.
QuickSort Algorithm 1. If first < last then begin 2. Partition the elements in the subarray first..last so that the pivot value is in place (in position.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
1 ENERGY 211 / CME 211 Lecture 18 November 2, 2007.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
C++ REVIEW – TEMPLATES. GENERIC PROGRAMMING Programming/developing algorithms with the abstraction of types Algorithms/data is expressed “without type”
Motivation for Generic Programming in C++
TK1924 Program Design & Problem Solving Session 2011/2012
Exceptions, Templates, and the Standard Template Library (STL)
Introduction to Analysis of Algorithms
Introduction to Analysis of Algorithms
CS212: Object Oriented Analysis and Design
Function and class templates
Templates ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY.
Animation of Bubble Sort
Generic Programming CSCE 121 J. Michael Moore.
Name: Rubaisha Rajpoot
Medications Utilities – Mass Void Medications
Object-Oriented Programming (OOP) Lecture No. 37
Object-Oriented Programming (OOP) Lecture No. 38
CS148 Introduction to Programming II
Templates Generic Programming.
Object-Oriented Programming (OOP) Lecture No. 34
COP 3330 Object-oriented Programming in C++
Presentation transcript:

Template Functions Lecture 9 Fri, Feb 9, 2007

Algorithms and Functions A function expresses an algorithm. Often an algorithm can be applied to many different data types without change. A selection sort will sort a list of objects, provided The operator < is defined. The operator = is defined. 2/24/2019 Template Functions

Example: SelectionSort.cpp 2/24/2019 Template Functions

Template Functions A template function contains the code necessary for an algorithm, but does not specify the data types involved. The data types will be specified later, at which point All necessary operators and functions must be defined for those types. 2/24/2019 Template Functions

C++ Template Functions Write at the beginning of the function definition. Use T in place of the type in the function. template <class T> template <class T> void Sort(T list[], int size); 2/24/2019 Template Functions

Example: TemplateSortC++.cpp 2/24/2019 Template Functions

C++ Template Functions A C++ template function is automatically and separately instantiated for as many types as necessary in a program. 2/24/2019 Template Functions

C++ Template Functions More than one template type is possible in a single function. 2/24/2019 Template Functions