Templates Class Templates

Slides:



Advertisements
Similar presentations
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
Advertisements

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.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
 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.
OOP Languages: Java vs C++
 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
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)
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
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.
 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.
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.
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.
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.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
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.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
 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
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
The C++ Data Types Fundamental Data Types
Chapter 22 - C++ Templates
Programming with ANSI C ++
How to be generic Lecture 10
C++ Templates.
Templates.
Lecture 6 : Template Acknowledgement : courtesy of Prof. Dekai Wu lecture slides.
Chapter 5 Function Basics
Chapter 15: Overloading and Templates
CS212: Object Oriented Analysis and Design
Templates.
Name: Rubaisha Rajpoot
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Overview of C++ Overloading
Introduction to Programming
Constructors and Destructors
Functions Pass By Value Pass by Reference
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.
9-10 Classes: A Deeper Look.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Chapter 8: Class Relationships
Submitted By : Veenu Saini Lecturer (IT)
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Lab4 problems More about templates Some STL
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
DYNAMIC MEMORY MANAGEMENT
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
Function Templates Class Templates
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
9-10 Classes: A Deeper Look.
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 <class t> // 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 <class t> List<t>::List(int sz){ list = new t [size = sz];} template <class t> t& List<t>::operator [](int j){return list[j];} main(){ List<char> ch_arr(80); List<double> dbl_flt(15); List<Complex> x;

Templates Class Templates (cont.) Template class instantiation List<type> Object_name(parameters); // instantiates a List<char> class at // compile time the generic type t is replaced by type, then the object is // instantiated as shown in the main() function of the previous slide Non-type parameters in class templates template <class t, int size> 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<float, 500> 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<int> {//………}; // the base class is // instantiated from a the template List <class t> class A template derived class with a non-template base class template <class t> 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 t> class Stack: public List<t> { public: Stack(int sz): List<t>(sz){} //references with formal parameters }; // the following statement instantiates class Stack<float> and class List<float> Stack<float> a(100); // object a is instantiated from these classes

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

Templates Templates, Container Classes, and Class Libraries A container class id 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 <classlib\stacks.h> #include <iostream.h> void main(){ TStackAsVector<int> 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 <class T> 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 = -100000, 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)