Templates in C++. Generic Programming Programming/developing algorithms with the abstraction of types The uses of the abstract type define the necessary.

Slides:



Advertisements
Similar presentations
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
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.
Solving IPs – Cutting Plane Algorithm General Idea: Begin by solving the LP relaxation of the IP problem. If the LP relaxation results in an integer solution,
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.
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.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Templates and Polymorphism Generic functions and classes.
. Templates. Example… A useful routine to have is void Swap( int& a, int &b ) { int tmp = a; a = b; b = tmp; }
Rossella Lau Lecture 11, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 11: Template and Operator overload  Template.
OOP Etgar 2008 – Recitation 51 Object Oriented Programming Etgar 2008 Recitation 5.
OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8.
Generic Programming David Rabinowitz. June 14, 2006 Object Oriented Design Course 2 The problem Assume we have a nice Stack implementation. Our stack.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
1 Chapter 21 Generics. 2 Objectives F To know the benefits of generics (§21.1). F To use generic classes and interfaces (§21.2). F To declare generic.
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.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
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.
Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined.
Templates and Polymorphism Generic functions and classes
Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Inheritance. Recall the plant that we defined earlier… class Plant { public: Plant( double theHeight ) : hasLeaves( true ), height (theHeight) { } Plant(
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Templates.
Generics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 5 An Array Class Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Recap Visual Perception and Data Visualization Types of Information Display Examples of Diagrams used for Data Display Planning Requirement for Data Visualization.
Learners Support Publications Functions in C++
Computer Engineering Rabie A. Ramadan Lecture 5.
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.
Templates L. Grewe. 2 Goals Often want to do basically the same thing w/diff things –functions work on variables only types specified –  algorithmic.
CS-2851 Dr. Mark L. Hornick 1 Generic Java Classes Implementing your own generic classes.
Templates Templates for Algorithm Abstraction. Slide Templates for Algorithm Abstraction Function definitions often use application specific adaptations.
CS212: Object Oriented Analysis and Design
Overview of C++ Templates
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
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.
CSCI 62 Data Structures Dr. Joshua Stough December 2, 2008.
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.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
 2006 Pearson Education, Inc. All rights reserved Templates.
C++ REVIEW – TEMPLATES. GENERIC PROGRAMMING Programming/developing algorithms with the abstraction of types Algorithms/data is expressed “without type”
Sections Inheritance and Abstract Classes
Programming with ANSI C ++
What is a compiler? Compiler Source code (e.g. C++) Target code
How to be generic Lecture 10
Templates.
CS212: Object Oriented Analysis and Design
Templates in C++.
Object-Oriented Programming (OOP) Lecture No. 32
Templates ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY.
Array Lists Chapter 6 Section 6.1 to 6.3
Name: Rubaisha Rajpoot
CMSC 202 Templates.
Reference Variables The symbol “&” has a few different purposes depending on where it occurs in code. When it appears in front of a variable name, it is.
This template is 4 square pictures on the left with no heading
Template Functions Lecture 9 Fri, Feb 9, 2007.
Parasol Lab, Texas A&M University
Templates CMSC 202, Version 4/02.
COP 3330 Object-oriented Programming in C++
Templates Generic Programming.
Presentation transcript:

Templates in C++

Generic Programming Programming/developing algorithms with the abstraction of types The uses of the abstract type define the necessary operations needed when instantiation of the algorithm/data occurs template T Add(const T &t1, const T &t2) { return t1 + t2; }

C++ Templates Templates are not types, but rather they are a placeholder for a type At compile time, the compiler makes a copy of the templated code and automatically “replaces” the placeholders with the concrete type C++ templates come in two flavors: – Functions templates – Class templates

Function Templates Used to define generic algorithms While useful, the function only works for integers. A better solution is to define a function template template T Max(T x, T y) { if ( x < y ) return y; return x; } int Max(int x, int y) { if ( x < y ) return y; return x; }

Function Templates Nothing special has to be done to use a function template Note: all that is required of the type passed to Max is the comparison operator, operator<. int main(int argc, char* argv[]) { inta = 3, b = 7; double x = 3.14, y = 2.71; cout << Max(a, b) << endl;// Instantiated with type int cout << Max(x, y) << endl; // Instantiated with type double cout << Max(a, x) << endl; // ERROR: types do not match }

You can instantiate the same container with different types template class myarray { private: T* v; int sz; public: myarray(int s) { v = new T [sz = s]; } // Constructor ~myarray() { delete[] v; } // Destructor T& operator[] (int i) { return v[i]; } int size() { return sz; } }; Class Templates myarray intArray(10); myarray shapeArray(10);

Typedef “alias” of types into a short hand Very common when using templates as syntax can be verbose Ex: – typedef vector VecInt; – typedef map > MyTuple;

The skies are the limit! Can have templated classes and functions with many template parameters Specialized templates for specific types Specialized functions for potential optimization template void myFunc(T1& t1, T2& t2); template void myFunc(T& t); template<> void myFunc (string& t); //specialization for strings template float dotProduct(float *v1, float *v2) { float rval = 0; for ( int i = 0; i < n; i++ ) { rval += v1 [ i ] * v2 [ i ]; } return rval; } // must call with template argument... dotProduct ( v1, v2 );

Summary Generic programming allows for the abstraction of types C++ templates are an instantiation of generic programming C++ has function templates and class templates Templates have many uses and allow for very interesting code design

Exercise 1.Create a class Point which has a template parameter of the type of internal data, T, and a template parameter for the dimension of the vector, n. Store a statically allocated, internal array of type T with dimension n. 2.Create a template function which computes the Euclidean distance between 2 points. 3.Instantiate two Point and compute their distance. Instantiate two Point and compute their distance.