Templates ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY.

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

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.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Parameter Passing Mechanisms Reference Parameters Read § §
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
Parameter Passing Mechanisms Reference Parameters § §
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Templates L. Grewe. 2 Goals Often want to do basically the same thing w/diff things –functions work on variables only types specified –  algorithmic.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
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.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
C:\Temp\Templates 4 5 Use This Main Program 6.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
C++ Templates 1. Why Use Templates? C++ requires variables, functions, classes etc with specific data types. However, many algorithms (quicksort for example)
 2006 Pearson Education, Inc. All rights reserved Templates.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
C++ REVIEW – TEMPLATES. GENERIC PROGRAMMING Programming/developing algorithms with the abstraction of types Algorithms/data is expressed “without type”
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Pointer to an Object Can define a pointer to an object:
Programming what is C++
Test Driven Development (TDD)
Overview 4 major memory segments Key differences from Java stack
How to be generic Lecture 10
C++ Templates.
Template Classes CMPS 2143.
Templates.
FUNCTIONS In C++.
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
Command Line Arguments
Chapter 14 Templates C++ How to Program, 8/e
Standard Template Library
OOP-4-Templates, ListType
Introduction to Classes
CS212: Object Oriented Analysis and Design
Templates.
Templates in C++.
Object-Oriented Programming (OOP) Lecture No. 32
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
ADT Implementations: Templates and Standard Containers
Introduction to Classes
Overview 4 major memory segments Key differences from Java stack
Name: Rubaisha Rajpoot
CMSC 202 Templates.
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Templaets It is a new concept which enables us to define “generic class “ and “generic function” to provide the “ generic programming” We are familiar.
Overview of C++ Overloading
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Exception Handling.
Parasol Lab, Texas A&M University
C++ Templates L03 - Iterator 10 – Iterator.
Overview of C++ Polymorphism
Really reusable software
Functions Imran Rashid CTO at ManiWeber Technologies.
Class: Special Topics Overloading (methods) Copy Constructors
Functions Reasons Concepts Passing arguments to a function
Templates An introduction.
Function Templates Class Templates
Templates CMSC 202, Version 4/02.
COP 3330 Object-oriented Programming in C++
Templates Generic Programming.
SPL – PS1 Introduction to C++.
Presentation transcript:

Templates ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY

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<typename T> 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 Next few slides are just a refresher for C++ templates: Templates are not types, but rather they are a placeholder for a type C++ templates come in two flavors: Functions templates Class templates

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 int max(int x, int y){ return x < y ? y : x; } Template functions have the following form: template < template-argument-list > function-definition The template-argument-list is one or more type-names within the scope of the template definition. In template functions the first argument is always a type. template<class T> T max(T x, T 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[]) { int a = 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 } SHOW PREVIOUS PAGE AS FOIL Nothing special has to be done to use a function template No special libraries or header files required. The compiler examines the types of the arguments and instantiates the appropriate function for you. For this example, we are only using the built-in types (e.g. int and double), so the C++ language defines the ‘less-than’ operator. SHOW EXAMPLE ( func_template1 )

Class Templates template <class T> 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; } }; Class Templates May contain data member objects of any type. Then instantiate the same container with objects of different types: Class Templates Just as a function template is able to take argument of any type, a class template may contain (as its data members) objects of any type.  class myarray You can instantiate different widget-containers which store objects of different types: myarray<int> iwidget(10); myarray<char> cwidget(10); myarray<shape> swidget(10); Take a look at the notation, the type-name is myarray<specific_type>. myarray<int> iarr(10); myarray<shape> sarr(10);

The skies are the limit! Templates have an extreme amount of uses Can many template parameters Specialized templates for specific types Variadics