1 Templates Chapter 12. 2 What You Will Learn Using function templates to created a group of overloaded functions Using class templates to create a group.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
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.
TEMPLATES Lecture Presented By SHERY KHAN Object Orienting Programming.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
 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 
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.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
 2006 Pearson Education, Inc. All rights reserved Generics.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
Templates Overload function: define more than one function With same function name Different parameter type Different type Different number of parameter.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 1Winter Quarter Inheritance and Overloading.
Operator overloading Object Oriented Programming.
Intro to Generic Programming Templates and Vectors.
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.
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
CHAPTER 3 Function Overloading. 2 Introduction The polymorphism refers to ‘one name having many forms’ ‘different behaviour of an instance depending upon.
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 =
Computer Programming & Applications Mohamed Iqbal Pallipurath Lecture 02P. 1 Inheritance and Overloading Lecture 28.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
CIS 270—Application Development II Chapter 18-Generics.
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.
CS212: Object Oriented Analysis and Design
1 CSC241: Object Oriented Programming Lecture No 25.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
 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.
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.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
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.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
 2006 Pearson Education, Inc. All rights reserved Templates.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
TK1924 Program Design & Problem Solving Session 2011/2012
Chapter VII: Arrays.
Programming with ANSI C ++
C++ Templates.
Chapter 14 Templates C++ How to Program, 8/e
Generic programming – Function template
CS212: Object Oriented Analysis and Design
Name: Rubaisha Rajpoot
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Exception Handling.
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.
Templates Generic Programming.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Presentation transcript:

1 Templates Chapter 12

2 What You Will Learn Using function templates to created a group of overloaded functions Using class templates to create a group of related types Distinguish between function templates and template functions

3 Introduction Templates enable us to specify an entire range of related functions –takes a single code segment –the related functions are overloaded –called template functions Can also have template classes –might write a single class template for a stack class –then have C++ generate stack-of-int, stack-of- float, etc. classes

4 Function Templates Overloaded functions –normally used to perform similar operations on different types of data overload + operator for complex numbers If operations are identical for each type, this could be done more compactly using function templates –programmer writes single function template definition –compiler generates separate object-code functions for each type of call

5 Function Templates Function template definition syntax template void DoSomething ( Whatever Stuff) {... } The function DoSomething can be called with different types for the parameter Stuff The compiler generates separate code for each call, using the appropriate type See Figure 12.1 on Text CD See Figure 12.1 on Text CD Required syntax

6 Function Templates template void DoSomething ( T1 *tPtr, T1 v1, T2 v2) {... } Needs appear only once here to appear multiple times here Specify as many classes in the template as types will vary in the different implementations to be used

7 Function Template template void DoSomething ( T1 *tPtr, T1 v1, T2 v2) {... } // calls to DoSomething int intval = 5; float floatval, *fptr, char ch, * cptr; Dosomething (cptr, ch, floatval); DoSomething (fptr, floatval, intval); View Figure 12.2 with audio from text CD

8 Overloading Template Functions Provide other function templates that specify the same function name but different function parameters Provide other non-template functions with the same function name but with different arguments Compiler determines which function to call –looks for precise match –then looks for function template which would match

9 Class Templates Consider a class which would implement a matrix, or a list, or a stack –is a set of numbers and the various operations on those numbers Possible to need one of these classes for different types of numbers –integer stack, float stack, character stack, etc. Need means for describing our class generically and instantiating the class for different types ==> CLASS TEMPLATE !

10 Class Templates // syntax example of declaration template class Whatever { public: void DoIt (T1 tval);... private: T1 tlist[30]; } ; // syntax example of declaration template class Whatever { public: void DoIt (T1 tval);... private: T1 tlist[30]; } ; // Syntax example of instantiation Whatever intWhatever; Specifies type to be used wherever the T1 appears in the class template declaration View fig 12.3, listen to text CD audio

11 Class Templates // syntax for definition template void Whatever ::DoIt (T1 tval) { … cout << tval; … } Specified prior to each member function definition Included in each function heading Used as type in parameter list and function as needed Important !!! Most compilers require declarations and definitions to be in the same file Not separate.h and.cpp files

12 Non-Type Parameters When we saw the previous declaration template T1 is called a "type parameter" –we are using the parameter to specify what the type will be Also possible to have parameters which do not specify a type -- non-type parameters template

13 Templates & Inheritance A class template can be derived from a template class A class template can be derived from a non- template class A template class can be derived from a class template A non-template class can be derived from a class template

14 Templates & Friends Consider the following declaration: template class P { friend void snarf ( ) ;... }; Function snarf is a friend of every template class instantiated from class P

15 Templates & Friends Given this declaration: template class P { friend void snarf ( ) ; friend void blat (P &);... }; For a particular type of Q (say int), blat (P &) is a friend of P only

16 Templates & Friends Friendship can be established between a class template and … –a global function –a member function of another class (possibly a template class) –an entire class (possibly a template class)

17 Templates & Static Members With a non-template class –one copy of a static data member is shared among all objects of the class –the static data member must be initialized at file scope With a template class –each instantiantion has its own copy of each static data member of the class template –all objects of that template class share that one static data member –static data members must be initialized at file scope