1 Chapter 17-1 Templates and Exceptions Dale/Weems.

Slides:



Advertisements
Similar presentations
Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
Advertisements

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
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.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Lists + CS3240, L. Grewe 1. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked.
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.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
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.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Using Templates Object-Oriented Programming Using C++ Second Edition 11.
 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 Lecture 8: Introduction to C++ Templates and Exceptions  C++ Function Templates  C++ Class Templates.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
1 Fall Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Introduction to C++ Templates and Exceptions l C++ Function Templates l C++ Class Templates l Exception and Exception Handler.
Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 5 An Array Class Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
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.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Lecture 17 Templates, Part I. What is a Template? In short, it’s a way to define generic functionality on parameters without needing to declare their.
CS212: Object Oriented Analysis and Design
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
Copyright 2006, The Ohio State University Introduction to C++ Templates l C++ Function Templates l C++ Class Templates.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
1 Chapter 17 Templates and Exceptions Dale/Weems.
Computer Science and Software Engineering University of Wisconsin - Platteville 5. Template Yan Shi CS/SE 2630 Lecture Notes.
1 Chapter 17 Templates and Exceptions Dale/Weems/Headington.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
 2003 Prentice Hall, Inc. All rights reserved. 1 Ders Notu 8 - Template İçerik 11.1 Giriş 11.2 Fonksiyon Template ları 11.3 Overloading Fonksiyon Templates.
Introduction to C++ Templates and Exceptions C++ Function Templates C++ Class Templates Exception and Exception Handler.
 2006 Pearson Education, Inc. All rights reserved Templates.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 13 Applied Arrays: Lists and Strings
Chapter 14 Templates C++ How to Program, 8/e
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Generic programming – Function template
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.
Chapter 16 Linked Structures
Fundamental Programming
Yan Shi CS/SE 2630 Lecture Notes
Chapter 13 Applied Arrays: Lists and Strings
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Class: Special Topics Overloading (methods) Copy Constructors
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Lecture 8: Introduction to C++ Templates and Exceptions
More C++ Concepts Exception Handling Templates.
Presentation transcript:

1 Chapter 17-1 Templates and Exceptions Dale/Weems

2 Chapter 17 Topics l C++ Function Templates l Instantiating a Function Templates l User-defined Specializations l C++ Class Templates l Instantiating Class Templates l Function Definitions for Members of a Template Class l Exception Classes, Throwing an Exception l Exception Handlers

3 Generic Algorithms Generic algorithms are algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not

4 Example of a Generic Algorithm void PrintInt(int n) { cout << "***Debug" << endl; cout << "Value is " << n << endl; } void PrintChar(char ch) { cout << "***Debug" << endl; cout << "Value is " << ch << endl; } void PrintFloat(float x) { } void PrintDouble(double d) { } sum = alpha + beta + gamma; PrintInt(sum); PrintChar(initial); PrintFloat(angle); To output the traced values, we insert:

5 Function Overloading Function overloading is the use of the same name for different functions, distinguished by their parameter lists  Eliminates need to come up with many different names for identical tasks  Reduces the chance of unexpected results caused by using the wrong function name

6 Example of Function Overloading void Print(int n) { cout << "***Debug" << endl; cout << "Value is " << n << endl; } void Print(char ch) { cout << "***Debug" << endl; cout << "Value is " << ch << endl; } void Print(float x) { } Print(someInt); Print(someChar); Print(someFloat); To output the traced values, we insert:

7 Function Template A C++ language construct that allows the compiler to generate multiple versions of a function by allowing parameterized data types Template FunctionDefinition FunctionTemplate TemplateParamDeclaration class Identifier typename

8 Example of a Function Template template void Print(SomeType val) { cout << "***Debug" << endl; cout << "Value is " << val << endl; } Print (sum); Print (initial); Print (angle); To output the traced values, we insert: Template parameter Template argument

9 Instantiating a Function Template When the compiler instantiates a template, it substitutes the template argument for the template parameter throughout the function template Function (FunctionArgList) TemplateFunction Call

10 Implicit template arguments l Print(sum);// Implicit: Print (sum) l Print(initial);// Implicit: Print (initial) l Print(angle);// Implicit: Print (angle)

11 Enhancing the Print Template template void Print( string vName, // Name of the variable SomeType val ) // Value of the variable { cout << "***Debug" << endl; cout << "Value of " << vName << “ = “ << val << endl; }

12 Generic Functions, Function Overloading, Template Functions Generic Function Different Function Definitions Different Function Names Function Overloading Different Function Definitions Same Function Name Template Functions One Function Definition (a function template) Compiler Generates Individual Functions

13 User-Defined Specializations Example that demonstrates use of template template<> void Print(string vName, // Name of the variable StatusType val ) // Value of the variable { cout << "***Debug" << endl; cout << "Value of " << vName << " = "; switch (val)

14 Example, continued. { case OK : cout << "OK"; break; case OUT_OF_STOCK : cout << "OUT_OF_STOCK"; break; case BACK_ORDERED : cout << "BACK_ORDERED"; break; default : cout << "Invalid value"; } cout << endl; }

15 Organization of Program Code Three possibilities: 1.Template definitions at the beginning of program file, prior to main function 2.Function prototypes first, then the main function, then the template definitions 3.Template definition in the header file, use #include to insert that file into the program

16 What is a Generic Data Type? It is a type for which the operations are defined but the data types of the items being manipulated are not

17 What is a Class Template? It is a C++ language construct that allows the compiler to generate multiple versions of a class by allowing parameterized data types

18 Example of a Class Template template class GList { public: bool IsEmpty() const; bool IsFull() const; int Length() const; void Insert(/* in */ ItemType item); void Delete(/* in */ ItemType item); bool IsPresent(/* in */ ItemType item) const; void SelSort(); void Reset() const; ItemType GetNextItem(); GList(); // Constructor private: int length; ItemType data[MAX_LENGTH]; }; Template parameter

19 Instantiating a Class Template // Client code GList list1; GList list2; GList list3; list1.Insert(356); list2.Insert(84.375); list3.Insert("Muffler bolt"); To create lists of different data types GList_int list1; GList_float list2; GList_string list3; template argument Compiler generates 3 distinct class types

20 Instantiating a Class Template Class template arguments must be explicit The compiler generates distinct class types called template classes or generated classes When instantiating a template, a compiler substitutes the template argument for the template parameter throughout the class template

21 Substitution Example class GList_int { public: void Insert(/* in */ ItemType item); void Delete(/* in */ ItemType item); bool IsPresent(/* in */ ItemType item) const; private: int length; ItemType data[MAX_LENGTH]; }; int

22 Writing Function Templates template void GList ::Insert(/* in */ ItemType item) { data[length] = item; length++; } void GList ::Insert(/* in */ float item) { data[length] = item; length++; }

23 Organization of Program Code l A compiler must know the argument to the template in order to generate a function template, and this argument is located in the client code l Solutions n Have specification file include implementation file n Combine specification file and implementation file into one file

24 Warning! Are you using an IDE (integrated development environment) where the editor, compiler, and linker are bundled into one application? Remember The compiler must know the template argument How you organize the code in a project may differ depending on the IDE you are using

25 The End of Chapter 17 Part 1