How to be generic Lecture 10

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

1 L39 Generics (1). 2 OBJECTIVES  To create generic methods that perform identical tasks on arguments of different types.  To create a generic Stack.
 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.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
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 CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Templates Class Templates Used to specify generic class types where class members data types can be specified as parameters, e.g. here is a generic List.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Overview of C++ Templates
Concepts in C++. Templates in current C++ C++ template is typeless No language support for constrained generics Accidental errors found in instantiation.
Functions, Scope, and The Free Store Functions Functions must be declared by a function prototype before they are invoked, return_type Function_name(type,
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
LECTURE LECTURE 17 Templates 19 An abstract recipe for producing concrete code.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
C++ Templates 1. Why Use Templates? C++ requires variables, functions, classes etc with specific data types. However, many algorithms (quicksort for example)
Learners Support Publications Constructors and Destructors.
 2006 Pearson Education, Inc. All rights reserved Templates.
C++ Functions A bit of review (things we’ve covered so far)
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Defining Your Own Classes II
Motivation for Generic Programming in C++
Constructors and Destructors
C++ Lesson 1.
CS 2304: Templates And Compilation
Programming with ANSI C ++
Jim Fawcett CSE687 – Object Oriented Design Spring 2010
C++ Templates.
Chapter 18 Introduction to Custom Templates
Templates.
Object-Oriented Programming (OOP) Lecture No. 45
FUNCTIONS In C++.
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
Chapter 14 Templates C++ How to Program, 8/e
Introduction to Custom Templates
CS212: Object Oriented Analysis and Design
Templates.
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.
ADT Implementations: Templates and Standard Containers
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Constructors and Destructors
CISC/CMPE320 - Prof. McLeod
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Object-Oriented Programming (OOP) Lecture No. 37
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Lists - I The List ADT.
9-10 Classes: A Deeper Look.
Parasol Lab, Texas A&M University
Lab4 problems More about templates Some STL
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Templates CMSC 202, Version 4/02.
Constructors & Destructors
9-10 Classes: A Deeper Look.
Introduction to Classes and Objects
Presentation transcript:

How to be generic Lecture 10 Templates How to be generic Lecture 10

In java you may have used the Object class as it is a common base type In C++ the STL is all built using template specification, even most of the C++ library is built upon templates template <class CharType, class Traits = char_traits<CharType>, class Allocator = allocator<CharType>> class basic _string; typedef basic_string<char> string;

Templates Templates let you pass the type as an argument to a function or class Must have definition in the same file as declaration Function templates Generic functions Class templates Generic class

Function templates template <typename T> inline T const& max (T const& a, T const& b) { // if a < b then use b else use a return a<b?b:a; } typename – Represents a type we want to use The keyword class and typename are interchangeable.

Arguments need to be dedicated Replacing template parameters with concrete types is called instantiation Using a type that doesn’t overload the operators used in the function will result in a compile error Arguments need to be dedicated Max(4, 7) // ok both arguments evaluate as type integer Max(4, 7.2f) //not ok different types

Max(static_cast<float>(4), 7.2f); //OK! Max<float> (4, 7.2f); You can request as many template parameters as necessary template <typename T1, typename T2> inline T1 max (T1 const& a, T2 const& b) { return a < b ? b : a; } First argument defines a return type!?

Better now we are returning a specified type. template <typename T1, typename T2, typename RT> inline RT max (T1 const& a, T2 const& b) { return a < b ? b : a; } Better now we are returning a specified type. max<int,double,double>(4,4.2) max<double>(4,4.2) //return type is double? The arguments however would be deduced to be int and double from the input.

Template functions can be overloaded inline T const& max (T const& a, T const& b, T const& c) { return max (max(a,b), c); } //3 arguments template <typename T> inline T const& max (T const& a, T const& b) { return a<b?b:a; } //2 arguments

Templates can take pointers to types: template <typename T> inline T* const& max (T* const& a, T* const& b) { return *a < *b ? b : a; }

When functions are declared, they are specified to accept and return specific data types Function templates allow functions to have generic data types Templates can include multiple data types e.g template<typename A, typename B> Templates consist of a definition and a call. They are only compiled when a call is encountered The definition of templates must be in the same file as its declaration

Template classes template <class T> class Stack { … }; template <typename T> class Stack { Stack (Stack<T> const&); // copy constructor Stack<T>& operator= (Stack<T> const&); // assignment operator };

A memeber function for stack: template<typename T> T Stack<T>::pop () { if (elems.empty()) { throw std::out_of_range("Stack<>::pop(): empty stack"); } T elem = elems.back(); // save copy of last element elems.pop_back(); // remove last element return elem; // return copy of saved element }

Template class specification template<> class Stack<std::string> { … }; With this any implementation must be replaced with the specified type void Stack<std::string>::push (std::string const& elem) { elems.push_back(elem); // append copy of passed elem }

It is possible to have a partial specialization // partial specialization: second type is int template <typename T> class MyClass<T,int> { … }; MyClass<int,float> mif; // uses MyClass<T1,T2> MyClass<float,float> mff; // uses MyClass<T,T> MyClass<float,int> mfi; // uses MyClass<T,int> MyClass<int*,float*> mp; // uses MyClass<T1*,T2*>

Default template values template <typename T, typename CONT = std::vector<T> > class Stack { …} Stack<double,std::deque<double> > Replaces default parameter with a deque of type double.

template <typename T> class Base {…}; template <typename T> class Derived : Base<T> { …};

template <class entity_type> class IState : { //virtual void Enter(entity_type*) = 0; }; class SimulationState : public IState<Game> { //void SimulationState::Enter(Game*_game); };