Parasol Lab, Texas A&M University

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

Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Parameter Passing Mechanisms Reference Parameters.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
 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.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Templates An introduction. Simple Template Functions template T max(T x, T y) { if (x > y) { return x; } else { return y; } } int main(void) { int x =
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
Parameter Passing Mechanisms Reference Parameters § §
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++ 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.
LECTURE LECTURE 17 Templates 19 An abstract recipe for producing concrete code.
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)
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
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++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
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++
“Generic Programming” ECE 297
Pointers and Dynamic Arrays
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
Programming with ANSI C ++
How to be generic Lecture 10
C++ Templates.
Templates.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
Chapter 14 Templates C++ How to Program, 8/e
Standard Template Library
OOP-4-Templates, ListType
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.
C++ Templates L03 - Iterator 10 – Iterator.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
ADT Implementations: Templates and Standard Containers
Introduction to Classes
CMSC 202 Templates.
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Built-In (a.k.a. Native) Types in C++
Templaets It is a new concept which enables us to define “generic class “ and “generic function” to provide the “ generic programming” We are familiar.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Overview of C++ Overloading
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Exception Handling.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
C++ Templates L03 - Iterator 10 – Iterator.
C++ Templates L03 - Iterator 10 – Iterator.
Overview of C++ Polymorphism
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Lab4 problems More about templates Some STL
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Class: Special Topics Overloading (methods) Copy Constructors
Templates An introduction.
Function Templates Class Templates
Pointers and References
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Templates CMSC 202, Version 4/02.
COP 3330 Object-oriented Programming in C++
Lecture 8: Introduction to C++ Templates and Exceptions
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
An Introduction to Programming though C++
More C++ Concepts Exception Handling Templates.
Presentation transcript:

Parasol Lab, Texas A&M University C++ Review Templates Parasol Lab, Texas A&M University 1 1

Outline Generic Programming C++ Templates Function Templates Class Templates Typedef Summary 2 2

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 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 Class Templates May contain data member objects of any type. template <class T> class myarray { public: T* v; int sz; myarray(int s) { v = new T [sz = s]; } // Constructor ~myarray() { delete[] v; } // Destructor T& operator[] (int i) { return v[i]; } void set(int i, T val) { v[i] = val; } int size() { return sz; } }; 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>. Then instantiate the same container with objects of different types: myarray<int> iarr(10); myarray<shape> sarr(10);

Typedef “alias” of types into a short hand Very common when using templates as syntax can be verbose Ex. typedef vector<int> VecInt; typedef map<string, tuple<double, int, float, MyClass> > MyTuple; Sometimes you need to use typename before the type Typedef in a templated function for type located within a templated class typedef typename vector<T>::iterator IT;

The skies are the limit! Templates have an extreme amount of uses Can have templated classes and functions with many template parameters Specialized templates for specific types Etc, etc, etc. template<typename T1, typename T2> void myFunc(T1& t1, T2& t2); template<class T> void myFunc(T& t); template<> void myFunc<string>(string& t); //specialization for strings

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 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). Create a template function to compute the minimum of an array. Recommendation: template the function on a generic array type Instantiate a Point<double> and a Point<int> and print their values. Create an array of 10 random integers and an array of 10 random doubles. Use your minimum function to print the minimum of the arrays.