T EMPLATES Chapter 11 (11.1 and 11.2 only) Department of CSE, BUET 1.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 16 Templates. Learning Objectives Function Templates – Syntax, defining – Compiler complications Class Templates – Syntax – Example: Pair class.
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.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
I NHERITANCE Chapter 7 Department of CSE, BUET 1.
I NTRODUCING O PERATOR O VERLOADING Chapter 6 Department of CSE, BUET 1.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
1 Templates Chapter What You Will Learn Using function templates to created a group of overloaded functions Using class templates to create a group.
Operator Overloading Fundamentals
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Templates CS-240 Dick Steflik & DJ. Foreman. Reuse Templates allow us to get more mileage out of the classes we create by allowing the user to supply.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
CMSC 202 Lesson 23 Templates II. Warmup Write the templated Swap function _______________________________ void Swap( ________ a, ________ b ) { _______________.
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.
1 Joe Meehean.  Conceptual Picture access only to top item last-in-first-out (LIFO) item 1 item 2 item 3 Values in Values out 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
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 =
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
CIS 270—Application Development II Chapter 18-Generics.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
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.
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
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
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.
Templates. C++ 2 Outline Function templates  Function template definition  Function template overloading Class templates  Class template definition.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
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.
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
TEMPLATE. Introduction  Template can be assumed as one that can be used to develop a function or class.  C++ use template to achieve polymorphism that.
Copyright © Curt Hill Generic Functions Separating Data Type from Logic.
Templates יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום רביעי 08 יוני 2016 יום.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
MAITRAYEE MUKERJI Object Oriented Programming in C++
CPSC 252 Templatization Page 1 Templatization In CPSC152, we saw a class vector in which we could specify the type of values that are stored: vector intData(
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Programming with ANSI C ++
Introducing Templates and Generics in C++
Chapter 14 Templates C++ How to Program, 8/e
CS212: Object Oriented Analysis and Design
ADT Implementations: Templates and Standard Containers
Name: Rubaisha Rajpoot
Virtual Functions Department of CSE, BUET Chapter 10.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Templates Class Templates
Introduction to Programming
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.
COP 3330 Object-oriented Programming in C++
Corresponds with Chapter 5
Presentation transcript:

T EMPLATES Chapter 11 (11.1 and 11.2 only) Department of CSE, BUET 1

O BJECTIVES Generic functions Generic classes 2 Department of CSE, BUET

G ENERIC F UNCTIONS A generic function defines a general set of operations that will be applied to various types of data Allows to create a function that that can automatically overload itself !!! Allows to make the data type, on which to work, a parameter to the function General form template ret-type func-name(param list) { // body of function } Here, template is a keyword We can use keyword “typename” in place of keyword “class” “TtypeN” is the placeholder for data types used by the function 3 Department of CSE, BUET

G ENERIC F UNCTIONS (E XAMPLE -1) Department of CSE, BUET 4 template void swapargs( X &a, X &b) { X temp; temp = a; a = b; b = temp; } template void print( X1 x, X2 y) { cout << x << “, ” << y << endl; } void main() { int i = 10, j = 20; double x = 11.11, y = 22.22; print(i, j); // 10, 20 swapargs (i, j); // (int, int) print(i, j); // 20, 10 print(x, y); // 11.11, swapargs (x, y); (double, double) print(x, y); // 22.22, print(i, y); // 20, // (int, double) }

G ENERIC F UNCTIONS ( CONTD.) The compiler generates as many different versions of a template function as required Generic functions are more restricted than overloaded functions Overloaded functions can alter their processing logic But, a generic function has only a single processing logic for all data types We can also write an explicit overload of a template function 5 Department of CSE, BUET

G ENERIC F UNCTIONS (E XAMPLE -2) template void swapargs(X &a, X &b) { cout << “template version\n”; } void swapargs(int &a, int &b) { cout << “int version\n”; } void main() { int i = 10, j = 20; double x = 11.11, y = 22.22; swapargs(i, j); // “int version” swapargs(x, y); // “template version” } 6 Department of CSE, BUET

G ENERIC C LASSES Makes a class data-type independent Useful when a class contains generalizable logic A generic stack A generic queue A generic linked list etc. etc. etc. The actual data type is specified while declaring an object of the class General form template class class-name { // body of class }; 7 Department of CSE, BUET

G ENERIC C LASSES (E XAMPLE ) Department of CSE, BUET 8 template class stack { X stck[10]; int tos; public: void init( ) { tos = 0; } void push( X item); X pop( ); }; template void stack ::push( X item) { … } template X stack ::pop( ) { … }

G ENERIC C LASSES (E XAMPLE ) ( CONTD.) Department of CSE, BUET 9 void main( ) { stack s1, s2; s1.init( ); s2.init( ); s1.push(‘a’); s1.push(‘b’); s2.push(‘x’); s2.push(‘y’); cout << s1.pop( ); // b cout << s2.pop( ); // y stack ds1, ds2; ds1.init( ); ds2.init( ); ds1.push(1.1); ds1.push(2.2); ds2.push(3.3); ds2.push(4.4); cout << ds1.pop( ); // 2.2 cout << ds2.pop( ); // 4.4 }

L ECTURE C ONTENTS Teach Yourself C++ Chapter 11 (11.1 and 11.2) Study the examples from the book carefully 10 Department of CSE, BUET