C++ REVIEW – TEMPLATES. GENERIC PROGRAMMING Programming/developing algorithms with the abstraction of types Algorithms/data is expressed “without type”

Slides:



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

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.
OOP Etgar 2008 – Recitation 51 Object Oriented Programming Etgar 2008 Recitation 5.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
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.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointer Data Type and Pointer Variables
Templates and Polymorphism Generic functions and classes
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
CSE 332: C++ functions Review: What = and & Mean In C++ the = symbol means either initialization or assignment –If it’s used with a type declaration, it.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Templates ~ their instantiation and specialization.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Templates.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
ITEC 320 C++ Examples.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments.
C++ Memory Overview 4 major memory segments Key differences from Java
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
Templates L. Grewe. 2 Goals Often want to do basically the same thing w/diff things –functions work on variables only types specified –  algorithmic.
CSE 332: C++ template examples Concepts and Models Templates impose requirements on type parameters –Types that are plugged in must meet those requirements.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
1 Programming Principles II Lecture Notes 4 Functions (Returning Values) Andreas Savva.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
CSCI 62 Data Structures Dr. Joshua Stough December 2, 2008.
C:\Temp\Templates 4 5 Use This Main Program 6.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
C++ Templates 1. Why Use Templates? C++ requires variables, functions, classes etc with specific data types. However, many algorithms (quicksort for example)
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Templates 3 Templates and type parameters The basic idea templates is simple: we can make code depend on parameters, so that it can be used in different.
Motivation for Generic Programming in C++
Pointers and Dynamic Arrays
Overview 4 major memory segments Key differences from Java stack
How to be generic Lecture 10
Motivation and Overview
Standard Template Library
OOP-4-Templates, ListType
Templates in C++.
Templates ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY.
Pointers and References
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Overview 4 major memory segments Key differences from Java stack
CMSC 202 Templates.
Built-In (a.k.a. Native) Types in C++
CMSC 202 Lesson 22 Templates I.
Overview of C++ Overloading
Parasol Lab, Texas A&M University
Templates I CMSC 202.
Pointers and dynamic objects
Pointers and References
Templates CMSC 202, Version 4/02.
COP 3330 Object-oriented Programming in C++
Presentation transcript:

C++ REVIEW – TEMPLATES

GENERIC PROGRAMMING Programming/developing algorithms with the abstraction of types Algorithms/data is expressed “without type” 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 automatically “replaces” the placeholders with the concrete type Closer to reality – the compiler makes a copy of the template, fills in the placeholders, and compiles the code C++ templates come in two flavors: Functions templates Class templates Similar to Java’s Generics

FUNCTION TEMPLATES used to define generic algorithms While this is useful, it only works for integers. A better solution is to define a function template for max template T max(T x, T y){ return x < y ? y : x; } int max(int x, int y){ return x < y ? y : 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 }

CLASS TEMPLATES Class Templates May contain data member objects of any type. Then instantiate the same container with objects of different types: template class myarray { public: T* v; size_t sz; myarray(size_t s) { v = new T [sz = s]; } ~myarray() { delete[] v; } T& operator[] (size_t i) { return v[i]; } void set(size_t i, T val) { v[i] = val; } int size() { return sz; } }; myarray iarr(10); myarray sarr(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; Can now use decltype and auto in C++11/14 for other use cases

THE SKIES ARE THE LIMIT! Templates have an extreme amount of uses Can many template parameters Specialized templates for specific types Variadics We will only scratch the surface in this class and probably most of what you will use in C++ Etc, etc, etc

PAIR PROGRAMMING EXERCISE 1. Class Templates Create a class Point which has a template parameter of the type of internal data, T. Store an internal array of type T which should have a fixed dimension (2, 3, or n – maybe passed in the constructor or as another template parameter). 2. Function Templates Create a template function to compute the minimum of an array. Recommendation: template the function generic pointer types and pass into the function a beginning and ending pointer 3. Test your class/function!