Really reusable software

Slides:



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

Computer Science 1620 Function Overloading. Review Question: suppose I have the following function: can I call this function with an integer? yes – compiler.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Template Classes. A class that depends on an underlying data type(s) likewise can be implemented as a template class. We can have a ‘NewClass’ of ints,
Chapter 7: User-Defined Functions II
Chapter 14: Overloading and Templates
 2006 Pearson Education, Inc. All rights reserved Templates.
Pointers1 Pointers & Dynamic Arrays Allocating memory at run-time.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
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.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
CSC212 Data Structure - Section FG Lecture 11 Templates, Iterators and STL Instructor: Professor Zhigang Zhu Department of Computer Science City College.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
Classes, Interfaces and Packages
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes.
 Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes.  This presentation shows how.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Procedural and Object-Oriented Programming
Pointers and Dynamic Arrays
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?
C++ Templates.
Template Classes CMPS 2143.
Templates.
Template Classes and Functions
Suppose we want to print out the word MISSISSIPPI in big letters.
Templates, Iterators and STL
Chapter 14 Templates C++ How to Program, 8/e
About the Presentations
Chapter 15: Overloading and Templates
This technique is Called “Divide and Conquer”.
CS212: Object Oriented Analysis and Design
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
User-Defined Functions
Template Functions Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation.
Lecture 4-7 Classes and Objects
User Defined Functions
Classes and Data Abstraction
Template Functions Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation.
Name: Rubaisha Rajpoot
CMSC 202 Templates.
6 Chapter Functions.
Abstraction: Generic Programming, pt. 2
Classes and Objects.
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Template Functions Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
9-10 Classes: A Deeper Look.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Templates CMSC 202, Version 4/02.
COP 3330 Object-oriented Programming in C++
The Three Attributes of an Identifier
Standard Version of Starting Out with C++, 4th Edition
9-10 Classes: A Deeper Look.
Templates Generic Programming.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Really reusable software 4/25/2019 Templates: Really reusable software tmplts

Finding the Maximum of Two Integers Here’s a small function that you might write to find the maximum of two integers int maximum(int a, int b) { if (a > b) return a; else return b; }

Finding the Maximum of Two Doubles Here’s a small function that you might write to find the maximum of two double numbers. double maximum(double a, double b) { if (a > b) return a; else return b; }

Finding the Maximum of Two Knafns Here’s a small function that you might write to find the maximum of two Knafns. Knafn maximum(Knafn a, Knafn b) { if (a > b) return a; else return b; }

One Hundred Million Functions ... Suppose your program uses 100,000,000 different data types, and you need a maximum function for each... int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; }

A Template Function for Maximum This template function can be used with many data types. template <class Item> Item maximum(Item a, Item b) { if (a > b) return a; else return b; }

A Template Function for Maximum When you write a template function, you choose a data type for the function to depend upon... template <class Item> Item maximum(Item a, Item b) { if (a > b) return a; else return b; }

A Template Function for Maximum A template prefix is also needed immediately before the function’s implementation: template <class Item> Item maximum(Item a, Item b) { if (a > b) return a; else return b; }

Using a Template Function Once a template function is defined, it may be used with any adequate data type in your program... template <class Item> Item maximum(Item a, Item b) { if (a > b) return a; else return b; } cout << maximum(1,2); cout << maximum(1.3, 0.9); ...

Finding the Maximum Item in an Array Here’s another function that can be made more general by changing it to a template function: int array_max(int data[ ], size_t n) { size_t i; int answer; assert(n > 0); answer = data[0]; for (i = 1; i < n; i++) if (data[i] > answer) answer = data[i]; return answer; }

Finding the Maximum Item in an Array Here’s the template function: template <class Item> Item array_max(Item data[ ], size_t n) { size_t i; Item answer; assert(n > 0); answer = data[0]; for (i = 1; i < n; i++) if (data[i] > answer) answer = data[i]; return answer; }

Parameter matching for template functions The second template function example required two parameters: an array of Items (the template type) and a numeric type (size_t in this case) representing array size The second parameter presents us with a potential problem: arguments to template functions must exactly match the data type(s) of formal parameters

Parameter matching for template functions When a template function is activated, the compiler selects underlying data type so that the type of the formal parameter exactly matches the actual parameter No automatic casting takes place The requirement for exact matching applies to all parameters of a template function

Solution to matching problem Can generalize both parameters in the array_max function as template parameters: template <class Item, class SizeType> Item array_max (const Item data[], SizeType n) etc. Function can now be instantiated with any data types appropriate to content & size of array

Placing template functions in a toolkit Notable requirement: implementations of template functions must appear in the header file Since this violates principle of information hiding, can employ a sneaky workaround: place include directive for implementation file at end of header instead of placing include directive for header at start of implementation

Template Classes Can implement template classes for reasons similar to template function implementation Precede class definition with template prefix (same as used for template functions) Example: template <class Item> class Bag { public: // can leave out typedef statement now ...

Template class functions Member functions that manipulate a template object are dependent on the template type Within the class definition, the compiler is aware of this dependency -- so function prototypes (which are inside the class definition) look the same as in non-template classes

Template class functions Outside the class definition, follow these rules: place template prefix before each function prototype (for non-member functions) and implementation (for all functions) each use of class name (ex. Bag) is changed to template class name (Bag<Item>) can now use name of underlying type without scope operator (Item instead of Bag::Item)

Template class function examples Function operator + is a non-member function that manipulates Bags Original prototype: Bag operator + (const Bag& b1, const Bag& b2); New prototype: template <class Item> Bag<Item> operator + (const Bag<Item>& b1, const Bag<Item>& b2);

Template class function examples Default constructor implementation Original version: Bag::Bag ( ) New version: template <class Item> Bag<Item>::Bag() Note that the template syntax applies only to the class name, not the function name

Template class function examples Final example: grab function Original implementation: Bag::Item Bag::grab( )const Template version: template <class Item> Item Bag<Item>::grab( ) const

Using template classes Specify underlying data type of the template object at declaration: Bag<char> cbag; Bag<int> ibag; Member functions are used as usual: cbag.insert(‘c’); int num = ibag.grab();

Templates & the linked list toolkit Original version: struct Node { typedef char Item; Item data; Node* link; }; Template version: template <class Item> struct Node { Item data; Node* link; };

Function modifications in linked list toolkit Precede each function definition with template prefix Change each Node reference to Node<Item> Change references to Node::Item to just Item Allow for exact parameter matching