Templates. Class templates – why? Writing programs we often use abstract data types such as stack, queue or tree. Implementations of these types may be.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
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.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
OOP Languages: Java vs C++
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Templates Zhen Jiang West Chester University
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
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.
Data Structures Using C++ 2E
The Rest of the Story.  Constructors  Compiler-generated  The Initializer List  Copy Constructors  Single-arg (conversion ctors)  The Assignment.
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)
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Templates.
Programming Languages and Paradigms Object-Oriented Programming.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Dynamic memory allocation and Pointers Lecture 4.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
 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.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
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.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Principles of programming languages 10: Object oriented languages
Pointers and Dynamic Arrays
Programming with ANSI C ++
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Introduction to Custom Templates
Chapter 5 Classes.
Student Book An Introduction
Stacks Chapter 4.
CS212: Object Oriented Analysis and Design
Templates.
This pointer, Dynamic memory allocation, Constructors and Destructor
Pointers and Linked Lists
Chapter 15 Pointers, Dynamic Data, and Reference Types
CS212: Object Oriented Analysis and Design
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.
7 Arrays.
Pointers Pointers point to memory locations
Lab4 problems More about templates Some STL
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Corresponds with Chapter 5
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Templates

Class templates – why? Writing programs we often use abstract data types such as stack, queue or tree. Implementations of these types may be almost identical, for example a list of chars and a list of numbers. They differ only in the type of elements, other features are the same. Writing programs we often use abstract data types such as stack, queue or tree. Implementations of these types may be almost identical, for example a list of chars and a list of numbers. They differ only in the type of elements, other features are the same. A class template is a more general class pattern in which some type or class used inside is given as a parameter. A general list template may be created first and it may be used to generate classes like a list of char or a list of figures at need. A class template is a more general class pattern in which some type or class used inside is given as a parameter. A general list template may be created first and it may be used to generate classes like a list of char or a list of figures at need. Using templates is better and more comfortable way of creating families of types and functions. It is recommended over using the preprocessor. Using templates is better and more comfortable way of creating families of types and functions. It is recommended over using the preprocessor.

Class templates – declaration template // template whose argument is type T class Stack// template name is Stack { T* v; // bottom of Stack T* v; // bottom of Stack T* p; // top of Stack T* p; // top of Stack int size; // capacity of Stack int size; // capacity of Stackpublic: Stack (int r) {v = p = new T[size=r];} // constructor with argument: Stack (int r) {v = p = new T[size=r];} // constructor with argument: // maximal size of stack // maximal size of stack ~Stack () {delete[]v;}// destructor ~Stack () {delete[]v;}// destructor void push(T a) {*p++ = a;} // push onto Stack void push(T a) {*p++ = a;} // push onto Stack T pop() {return *--p;} // pop from Stack T pop() {return *--p;} // pop from Stack int getSize() const {return p-v;}// how many T elements are there now int getSize() const {return p-v;}// how many T elements are there now};

Class templates – declaration template // in template declaration, // type T is an argument // template is a keyword class Stack// Stack is a template name Type (class) T may be used in the declaration as any other declared type or class. Type (class) T may be used in the declaration as any other declared type or class. Within the scope of declaration of template Stack the full name Stack is not used; Stack is used for template name, constructor and destructor. Within the scope of declaration of template Stack the full name Stack is not used; Stack is used for template name, constructor and destructor. Outside declaration we refer to template as Stack Outside declaration we refer to template as Stack

Class templates – declaring With the template Stack we may declare stacks of various elements, passing the element type as the actual parameter of the template With the template Stack we may declare stacks of various elements, passing the element type as the actual parameter of the template The syntax of declaration of a class derived from template: The syntax of declaration of a class derived from template:template_name<argument> Class – stack of integers: Class – stack of integers:Stack<int> Class – stack of pointers to figure: Class – stack of pointers to figure: Stack Stack

templates Declaration: Declaration: Stack numbers (100); Stack numbers (100); is a declaration of an object named numbers, is a declaration of an object named numbers, of class Stack, of class Stack, constructor Stack (100) is called. constructor Stack (100) is called. The name of the class derived from a template may be used as any other class name; syntax is the only difference. The name of the class derived from a template may be used as any other class name; syntax is the only difference.

Stack spf(200); // Stack of pointers to figures // of the capacity of 200 ptrs // of the capacity of 200 ptrs Stack sp(400); // Stack of points for 400 elements void f(Stack &sc) // function f, whose argument is // a reference to stack of complex numbers // a reference to stack of complex numbers{ sc.push(complex(1,2)); // push a complex onto the stack passed as the argument sc.push(complex(1,2)); // push a complex onto the stack passed as the argument complex z = 2.5 * sc.pop(); // pop a number from a Stack, complex z = 2.5 * sc.pop(); // pop a number from a Stack, // multiply it and store // multiply it and store Stack *p=0;// declaration of a pointer Stack *p=0;// declaration of a pointer // of a Stack of integers // of a Stack of integers p=new Stack (800); // construction of a Stack for 800 ints p=new Stack (800); // construction of a Stack for 800 ints for (int i=0; i<400; i++) // 400 times for (int i=0; i<400; i++) // 400 times { p->push(i); // push a number onto a Stack of ints p->push(i); // push a number onto a Stack of ints sp.push(Point(i,i+400)); // push a Point onto a Stack of those sp.push(Point(i,i+400)); // push a Point onto a Stack of those } delete p; // destruction of the Stack delete p; // destruction of the Stack}

Validation The compiler checks the validity of template only when it is used, so error in the declaration of a template may not be noticed until it is used to generate a class. The compiler checks the validity of template only when it is used, so error in the declaration of a template may not be noticed until it is used to generate a class. Successful compilation of a declaration of a template does not mean that it does not contain errors. Successful compilation of a declaration of a template does not mean that it does not contain errors. It is a good method to create and a specific class first (e.g. Stack_char ) and then to transform it into a general template Stack. It is a good method to create and a specific class first (e.g. Stack_char ) and then to transform it into a general template Stack.

templates In the previous version all methods are inline — they are defined inside the template. They may also be defined outside: In the previous version all methods are inline — they are defined inside the template. They may also be defined outside: template template class Stack { T* v; // bottom of Stack T* v; // bottom of Stack T* p; // top of Stack T* p; // top of Stack int size; // capacity of Stack int size; // capacity of Stackpublic: Stack (int r);// declaration: constructor with argument Stack (int r);// declaration: constructor with argument ~Stack (); ~Stack (); void push(T a); // declaration: push onto Stack void push(T a); // declaration: push onto Stack T pop(); // declaration: pop from Stack T pop(); // declaration: pop from Stack int getSize();// how many T elements are there now int getSize();// how many T elements are there now};

templates If methods are defined outside the declaration of template, the keyword template has to be used for each: If methods are defined outside the declaration of template, the keyword template has to be used for each: „A method of a template is a template of a method” „A method of a template is a template of a method” // definition of e method template template void Stack ::push(T a) { *p++ = a; *p++ = a;}; // constructor template template Stack ::Stack(int r) { v = p = new T[size=r]; v = p = new T[size=r];};

templates Remainder: Within the scope of declaration Remainder: Within the scope of declaration template class Stack the full name Stack is not used; Stack is used for constructor and destructor. the full name Stack is not used; Stack is used for constructor and destructor. The code below is wrong: The code below is wrong: // template // template // Stack ::Stack (int r) // it is an error, // // should read: Stack :: Stack(int r) //{ // v = p = new T[size=r]; //};

Modifying templates A template which is written and used, should not be later modified — the modifications would apply to all classes derived from this template. A template which is written and used, should not be later modified — the modifications would apply to all classes derived from this template. If we add members, size of all derived objects will grow. If we add members, size of all derived objects will grow. If we change definition of methods, the changes would apply to all derived classes. If we change definition of methods, the changes would apply to all derived classes. Therefore, instead of modifying an existing template, we may create a derived (inheriting) template with new features. Therefore, instead of modifying an existing template, we may create a derived (inheriting) template with new features.

Extending of templates Example: We need a stack of strings with saving and loading from a file Example: We need a stack of strings with saving and loading from a file template template class Filed_Stack: public Stack class Filed_Stack: public Stack { char * file_name; char * file_name;public: /* constructor, parameters size and filename */ /* constructor, parameters size and filename */ Stack(int size, char * filename = NULL) Stack(int size, char * filename = NULL) :Stack (size) // parent construction :Stack (size) // parent construction { // here store the filename { // here store the filename } void save_Stack(); void save_Stack(); void load_Stack(); void load_Stack();};

Special case of template If a template would generate invalid code for some specific parameter (type), we may define a specific case of template for this parameter. E.g: class comparing objects of some type: If a template would generate invalid code for some specific parameter (type), we may define a specific case of template for this parameter. E.g: class comparing objects of some type: template template class comparator// general template {public: static less(T &a, T &b) static less(T &a, T &b) { return a<b; return a<b; }}; The above is valid for types such as int or char. For strings (char *) not strings but their addresses would be compared, which is not proper. The above is valid for types such as int or char. For strings (char *) not strings but their addresses would be compared, which is not proper.

Special case of template For strings (char *) not strings but their addresses would be compared, which is not proper. Therefore we create a special case of template, which will be used only for strings, instead of the version generated from the general template: For strings (char *) not strings but their addresses would be compared, which is not proper. Therefore we create a special case of template, which will be used only for strings, instead of the version generated from the general template: class comparator // special case of template for strings {public: static less(const char * a, const char * b) static less(const char * a, const char * b) { return strcmp(a, b)<0; return strcmp(a, b)<0; }}; The compiler will use the special case if it is visible in the point where it may be used (it should be declared above). Otherwise the general template will be expanded. The compiler will use the special case if it is visible in the point where it may be used (it should be declared above). Otherwise the general template will be expanded.

Template arguments A template may have many arguments. They can be classes, but also functions and constant expressions A template may have many arguments. They can be classes, but also functions and constant expressions Example: a buffer template whose size is a parameter: Example: a buffer template whose size is a parameter: template template class buffer { T w[size]; T w[size]; //... //...} How to used such a template: How to used such a template: buffer tbf; // declaration of object f // a buffer for 250 figures // a buffer for 250 figures buffer tbc; // a buffer for 100 characters

Identity of templates Two types derived from one template are identical only when all the arguments are identical. Otherwise they are different types that have nothing in common. Two types derived from one template are identical only when all the arguments are identical. Otherwise they are different types that have nothing in common. In the declarations below only objects tbc0 and tbc1 share the same class ( buffer ), others are of different classes. In the declarations below only objects tbc0 and tbc1 share the same class ( buffer ), others are of different classes. buffer tbc0; buffer tbf0; buffer tbc1; buffer tbf1;

function templates template // outside of class declaration void exchange(T &x, T &y)// not a method, but a function { T t=x; T t=x;x=y;y=t;} int a=7,b=8; exchange(a,b); // the compiler will expand a template // (if it is visible)

function templates – example Create a family of functions to increment the first argument by the second (both are numbers) Create a family of functions to increment the first argument by the second (both are numbers) template template void increm(t &i, double d)// will work for any numerical types {// but calling increm(1, 1) i+=t(d);// will cause 2 automatic conversions i+=t(d);// will cause 2 automatic conversions };// waste of time template template void increm_optimised (t &i, const d delta) // const will do no harm {// and may be useful i+=t(delta); i+=t(delta);};

function templates – example Create a family of functions to increment the first argument by the second (both are numbers) or by 1 if the second arg is not given. Create a family of functions to increment the first argument by the second (both are numbers) or by 1 if the second arg is not given. // template // template // void increm_1 (t &i, const d delta=1) // … Trap!: At the call Trap!: At the call increm_1(3.1415); the compiler will not determine the type of d! the compiler will not determine the type of d!

function templates – example Create a family of functions to increment the first argument by the second (both are numbers) or by 1 if the second arg is not given.. Create a family of functions to increment the first argument by the second (both are numbers) or by 1 if the second arg is not given.. template template void increm_1(t &i, const d delta) { i+=t(delta); i+=t(delta);}; template template void increm_1(t &i) { i+=t(1); i+=t(1);};