Exceptions, Templates, and the Standard Template Library (STL)

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function 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.
Generics and the ArrayList Class
C++ is Fun – Part 11 at Turbine/Warner Bros.! Russell Hanson.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Templates CS-341 Dick Steflik. Reuse Templates allow us to get more mileage out of the classes we create by allowing the user to supply certain attributes.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Exceptions, Templates, and the Standard Template Library (STL)
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 16 Exceptions,
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 28P. 1Winter Quarter Inheritance and Overloading.
Intro to Generic Programming Templates and Vectors.
Templates & Generic Programming Junaed Sattar November 12, 2008 Lecture 10.
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 =
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 16: Exceptions,
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
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.
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.
1 Using Templates COSC 1567 C++ Programming Lecture 10.
C++ Lecture 9 Tuesday, 26 Aug Templates l Template can be a class or function that has data types as parameters. The types are realized when the.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Pointer to an Object Can define a pointer to an object:
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
How to be generic Lecture 10
C++ Templates.
Templates.
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Introduction to C++ Systems Programming.
Exceptions, Templates, and the Standard Template Library (STL)
Chapter 14 Templates C++ How to Program, 8/e
Inheritance and Overloading
Generic programming – Function template
Starting Out with C++ Early Objects Eighth Edition
group work #hifiTeam
Name: Rubaisha Rajpoot
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Exceptions and Templates
Templates. Templates Example: Swap Template Mechanism.
Abstraction: Generic Programming, pt. 2
Introduction to Programming
Exception Handling.
Standard Version of Starting Out with C++, 4th Edition
Exceptions, Templates, and the Standard Template Library (STL)
Java Programming Language
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Functions Imran Rashid CTO at ManiWeber Technologies.
Templates Generic Programming.
Templates An introduction.
Function Templates Class Templates
Final Exam Review Inheritance Template Functions and Classes
COP 3330 Object-oriented Programming in C++
Templates Generic Programming.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Computer Science II for Majors
Presentation transcript:

Exceptions, Templates, and the Standard Template Library (STL) Lecture 17 : Exceptions, Templates, and the Standard Template Library (STL) ( cont. )

Function Templates Function template: a pattern for a function that can work with many data types When written, parameters are left for the data types When called, compiler generates code for specific data types in function call

Function Template Example prefix template <class T> T times10(T num) { return 10 * num; } generic data type type parameter What gets generated when times10 is called with an int: What gets generated when times10 is called with a double: int times10(int num) { return 10 * num; } double times10(double num)

Function Template Example template <class T> T times10(T num) { return 10 * num; } Call a template function in the usual manner: int ival = 3; double dval = 2.55; cout << times10(ival); // displays 30 cout << times10(dval); // displays 25.5

Function Template Notes Can define a template to use multiple data types: template<class T1, class T2> Example: template<class T1, class T2> // T1 and T2 will be double mpg(T1 miles, T2 gallons) // replaced in the { // called function return miles / gallons // with the data } // types of the // arguments

Function Template Notes Function templates can be overloaded Each template must have a unique parameter list template <class T> T sumAll(T num) ... template <class T1, class T2> T1 sumall(T1 num1, T2 num2) ...

Function Template Notes All data types specified in template prefix must be used in template definition Function calls must pass parameters for all data types specified in the template prefix Like regular functions, function templates must be defined before being called

Function Template Notes A function template is a pattern No actual code is generated until the function named in the template is called A function template uses no memory When passing a class object to a function template, ensure that all operators in the template are defined or overloaded in the class definition

Where to Start When Defining Templates Templates are often appropriate for multiple functions that perform the same task with different parameter data types Develop function using usual data types first, then convert to a template: add template prefix convert data type names in the function to a type parameter (i.e., a T type) in the template

Class Templates Classes can also be represented by templates. When a class object is created, type information is supplied to define the type of data members of the class. Unlike functions, classes are instantiated by supplying the type name (int, double, string, etc.) at object definition

Class Template Example template <class T> class grade { private: T score; public: grade(T); void setGrade(T); T getGrade() };

Class Template Example Pass type information to class template when defining objects: grade<int> testList[20]; grade<double> quizList[20]; Use as ordinary objects once defined

Class Templates and Inheritance Class templates can inherit from other class templates: template <class T> class Rectangle { ... }; class Square : public Rectangle<T> Must use type parameter T everywhere base class name is used in derived class