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.

Slides:



Advertisements
Similar presentations
Template. 2 Using templates, it is possible to create generic functions and classes. In a generic function or class, the type of data upon which the function.
Advertisements

C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
14 Templates. OBJECTIVES In this chapter you will learn:  To use function templates to conveniently create a group of related (overloaded) functions.
 2006 Pearson Education, Inc. All rights reserved Templates.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Function and Class Templates.
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.
 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.
 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.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
Review of C++ Programming Part II Sheng-Fang Huang.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
OOP Languages: Java vs C++
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Templates & Generic Programming Junaed Sattar November 12, 2008 Lecture 10.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Templates Zhen Jiang West Chester University
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
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.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
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 =
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.
1 CSC241: Object Oriented Programming Lecture No 25.
Overview of C++ Templates
Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Computer Science and Software Engineering University of Wisconsin - Platteville 5. Template Yan Shi CS/SE 2630 Lecture Notes.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 22 - C++ Templates Outline 22.1Introduction.
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.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
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 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)
Templates יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום.
 2006 Pearson Education, Inc. All rights reserved Templates.
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.
Chapter 6 Modularity Using Functions
MAITRAYEE MUKERJI Object Oriented Programming in C++
Pointer to an Object Can define a pointer to an object:
Programming with ANSI C ++
CS212: Object Oriented Analysis and Design
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Templates Class Templates
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Lab4 problems More about templates Some STL
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Presentation transcript:

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 class template, template // t is a generic data type parameter class List { int size; t* list; // a pointer to an object of the generic type t public: // example member functions List(int sz = 10); t& operator[](int); }; // member functions bodies must be preceded by the template keyword template List ::List(int sz){ list = new t [size = sz];} template t& List ::operator [](int j){return list[j];} main(){ List ch_arr(80); List dbl_flt(15); List x; // instantiate 3 classes and three objects

Templates Class Templates (cont.) Template class instantiation List Object_name(parameters); // instantiates a List // class at compile time, the generic type t is replaced by type, then the // object is instantiated as shown in the above main() function Non-type parameters in class templates template List { t list[size]; // statically allocated list of type t elements // the size of the list and the type of elements are determined at the class // instantiation. In the above, the size of the list is specified at compile // time no need for dynamic memory allocation, this avoids run time //errors due to new and eliminates execution overhead for memory //allocation List b;// instantiates the class List with float type // members and a size of 500 and instantiates an object b of this type

Templates Templates and inheritance An explicit Base class instantiated from a class template, e.g., class Integer_Stack: public List {//………}; // the base class is // instantiated from a the template List class A template derived class with a non-template base class template class D: public B {// data members, member function // arguments, return types in D can take the generic type t, inherited members // of B all have explicit types Both, a template derived class with a template base class template class Stack: public List { public: Stack(int sz): List (sz){} //references with formal parameters }; // the following statement instantiates class Stack and class List Stack a(100); // object a is instantiated from these classes

Templates Templates and friend functions and classes friend functions of a template class template class X{ friend void f2(X &); // declares f2() as friend to X type objects }; X a; // assumes the existance of the function f2(X &), this //function can have access to all members of X type objects Friend classes template class ListNode{ type Item; ListNode * Next; template friend class List; // all classes of List are // friends of class ListNode, this is dangerous since type might be // different from T, it is better to do the following instead, friend class List // only the List class where T is replaced by type will be friend of all classes of the template class ListNode

Templates Templates, Container Classes, and Class Libraries A container class is one that operates on a collection of one or more objects of a particular type, e.g., Lists, Stacks, Linked Lists, Queues, etc., Container classes are supported in the compiler’s class library as class templates, e.g. in Borland C++ the directory \classlib\Include have several header files which defines container class templates, these can be used as follows, #include void main(){ TStackAsVector x(100); x.push(50); x.push(30); while(!x.IsEmpty()) cout << x.pop() << endl;}

Templates Function templates A function template is used to specify generic functions in the same way as class templates is used to specify generic classes, for example, template T abs(T n){ // a generic function which returns the absolute value of object n // of type T return (n < 0) ? -n : n; } void main(){ int x = -500; long y = , double z = -1.87; cout << abs(x) << “\t” << abs(y) << “\t” << abs(z); // three different overloaded functions named abs are generated at compile // time, these are abs(int), abs(long), and abs(double)