Function Templates Class Templates

Slides:



Advertisements
Similar presentations
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Advertisements

Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Function and Class Templates.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)
Templates Zhen Jiang West Chester University
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Templates.
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 =
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
CS240 Computer Science II Function and Class Templates (Based on Deitel) Dr. Erh-Wen Hu.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
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.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
 Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
LECTURE LECTURE 17 Templates 19 An abstract recipe for producing concrete code.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
C++ Templates 1. Why Use Templates? C++ requires variables, functions, classes etc with specific data types. However, many algorithms (quicksort for example)
 2003 Prentice Hall, Inc. All rights reserved. 1 Ders Notu 8 - Template İçerik 11.1 Giriş 11.2 Fonksiyon Template ları 11.3 Overloading Fonksiyon Templates.
Templates יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום.
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.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Template, Preprocessing Lecture 9 November 2, 2004.
Java Generics.
Chapter 22 - C++ Templates
Programming with ANSI C ++
Jim Fawcett Copyright ©
How to be generic Lecture 10
C++ Templates.
Templates.
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
Chapter 14 Templates C++ How to Program, 8/e
Introduction to Custom Templates
Generic programming – Function template
Pointers Revisited What is variable address, name, value?
CS212: Object Oriented Analysis and Design
Templates.
Advanced Program Design with C++
CMSC 202 Lesson 22 Templates I.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Exceptions and Templates
Templates Class Templates
Introduction to Programming
Exception Handling.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Parasol Lab, Texas A&M University
Templates I CMSC 202.
Yan Shi CS/SE 2630 Lecture Notes
Lab4 problems More about templates Some STL
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Templates Generic Programming.
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
Templates An introduction.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
COP 3330 Object-oriented Programming in C++
Lecture 8: Introduction to C++ Templates and Exceptions
Templates Generic Programming.
More C++ Concepts Exception Handling Templates.
Presentation transcript:

Function Templates Class Templates C++ Templates Function Templates Class Templates

Templates overview Templates define families of classes and functions. C++ templates enable generic programming. C++ supports both function and class templates. Templates may be parameterized by types, compile-time constants, and other templates. C++ templates are implemented by instantiation at compile-time.

Consider this situation Suppose you wanted to define 2 functions called min that returns the minimum of 2 arguments passed into it for both integers and doubles. You can provide both functions as: int min( int a, int b) { return a < b ? a : b } double min( double a, double b) …and so on if you wanted to do this for more types

Using of a “function” Template The above (plus a lot more) could also be accomplished using a template function like:   template <class T> T min( T a, T b ) { return a < b ? a : b; } The symbol T is called a type parameter. It is simply a place holder that is replaced by an actual type or class when the function is invoked. Note: This function is now defined for all type where “<“ makes sense, not just the intended int and double.

Template header The symbol T is called a type parameter. It is simply a place holder that is replaced by an actual type or class when the function is invoked. A function template is declared in the same way as an ordinary function, except that it is preceded by the specification template <class T> The type parameter T may be used in place of ordinary types within the function definition. The word class is used to mean a class or primitive type. A template may have several type parameters, specified like this: template <class T, class U, class V>

Calling a templated function Templated functions are called the same way ordinary functions are called. int m = 22; int n = 66; int min_value; min_value = min(m,n);

Interesting question Happens if we do this: long m = 2; // an int but with twice the bits double n = 1.8; // a float but with twice the bits. float min_value; min_value = min(m,n); Would this compile? What is min_value? Which templated function ran? int, long or float version

Another Interesting question Suppose you created a class ratio which has a protected/private int num and int den field ratio r(2,3); // a ratio initialized to 2/3 float n = 0.8; int min_value = min(r,n); Would this compile? What is min_value? Why?

BubbleSort anything in C++ template <class T> void swap(T& x, T& y) { T temp = x; x = y; y = temp; } template<class T> void sort(T* v, int n) { for (int i = 1; i < n; i++) for (int j = 0; j=0, j < n-i; j++) if (v[j] > v[j+1]) swap(v[j], v[j+1] );

Class Templates Class templates work the same way as a function template except that it generates classes instead of functions. The general syntax is template<class T, …> class X { … } As with function templates, a class template may have several template parameters. Moreover, some of them can be primitive types parameters: template<class T, int n, class U> class X { … }

Primitive types must be constant Of course, since templates are instantiated at compile time, values passed to primitive types must be constant: template<class T, int n> class X {}; int main() { X<float, 22> x1; // OK const int n = 44; X<char, n> x2; //OK int m = 66; X<short, m> x3 //ERROR: m must be constant } Note: Class templates are sometimes called parameterized types.

Member functions of Class Templates The member functions of a class template are themselves function templates with the same template header as their class. For example: template<class T> class mathFunctions { static T square(T t) {return t*t ; } // more static math functions }; is handled in the same way that the following template function would be handled: T square(T t) {return t*t ; }

Compiler generated class names It is instantiated by the compiler, replacing the template parameter T with the type passed to it. Thus, the declaration; mathFunctions<short> x; Generated the class and object class mathFunctions_short { static short square(short t) { return t*t; } } mathFunctions_short x; Note: your compiler may use some name other than mathFunctions_short for the class.

Example) Stack class template template<class T> class Stack { public: Stack (int s = 100) : size(s), top(-1), {data = new T[size] ; } ~Stack() {delete [] data; } void push(const T& x) { data[++top] = x; } T pop() {return data[top--]; } int isEmpty() const { return top == -1; } int isFull() const {return top == size –1; } private: int size; int top; T* data; }

Use of the Stack Template class int main() { Stack<int> intStack1(5) ; Stack<int> intStack2(10); Stack<char> charStack(8); intStack1.push(11) ; intStack2.push(22); charStack.push(‘A’); intStack2.push(intStack1.pop()); cout << intStack2.pop() << endl; if (intStack2.isEmpty() ) cout << “intStack2 is empty. \n” ; } Output: 11 22 intStack2 is empty.

Java (mid 90’s) change to C++ (mid 80’s) Java calls Templates = Generics Java doesn’t use the Template header line template<class T> // C++ template declaration class Stack {…} // Templated Stack definited In Java it becomes: class Stack<T> {…} // Java generic Stack