Template Classes CMPS 2143.

Slides:



Advertisements
Similar presentations
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Advertisements

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.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
CMSC 202, Version 2/02 1 Operator Overloading Strong Suggestion: Go over the Array class example in Section 8.8 of your text. (You may ignore the Array.
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,
ReviewSlides CMPS Advantages/Disadvantages Statically typed languages ▫ Type checking can be done at compile time ▫ Memory layout can be determined.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
 2006 Pearson Education, Inc. All rights reserved Templates.
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(
Motivation for Generic Programming in C++
TK1924 Program Design & Problem Solving Session 2011/2012
Regarding assignment 1 Style standards Program organization
Classes C++ representation of an object
User-Written Functions
How to be generic Lecture 10
C++ Templates.
Templates.
Object-Oriented Design (OOD) and C++
Object-Oriented Programming (OOP) Lecture No. 21
Chapter 14 Templates C++ How to Program, 8/e
Object Oriented Systems Lecture 03 Method
Pointers and Pointer-Based Strings
Operator Overloading BCA Sem III K.I.R.A.S.
Templates.
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
Templates in C++.
Template Functions Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation.
Templates ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY.
Methods Additional Topics
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.
Name: Rubaisha Rajpoot
Function Overloading C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define similar.
CMSC 202 Templates.
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Introduction to Programming
CMSC 202 Lesson 22 Templates I.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
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.
COP 3330 Object-oriented Programming in C++
Pointers and Pointer-Based Strings
Templates I CMSC 202.
Really reusable software
Classes C++ representation of an object
C++ Programming CLASS This pointer Static Class Friend Class
CMSC 202 Lesson 6 Functions II.
Templates Generic Programming.
Templates An introduction.
Function Templates Class Templates
Class rational part2.
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++
9-10 Classes: A Deeper Look.
Templates Generic Programming.
Presentation transcript:

Template Classes CMPS 2143

Another form of polymorphism Generics (or templates) in C++ provide a way to parameterize a function or a class by use of a type A name is defined as a type parameter in the case of a class, eg. List <int> list; Then that type is used within the class definition in place of the parameter Generics in C++, Eiffel, ML, Haskell, Ada, and Java 7

Start with Template functions int max (int x, int y) { if (x < y) return y; else return x; } double max (double x, double y) { template <class T> T max (T x, T y) { if (x < y) return y; else return x; } int a = max (3, 27); double d = max(3.14, 2.75); Fraction f1 (1, 2); Fraction f2 (3, 4); Fraction f3 = max (f1, f2);

Notes on previous example: name T is a parameter (could use ItemType, Ttype..) T must be a type (can be primitive type, despite keyword class) Limits to the type – must have < defined for that type. so in Fraction < must be overloaded.

Template Classes Template functions useful, but more common to see templates applied to classes (especially container 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, chars, strings, whatever.

Syntax Declaring and defining a template class requires three things. The template class declaration Methods (member functions) for the template class Make the implementation visible

The template class declaration template <class T> class NewClass { public: NewClass (); NewClass (T initialData); NewClass (NewClass other); void setData (const T & newValue); T getData () const;   void resize (int newSize); NewClass operator + (const NewClass & newClass1, const NewClass & newClass2)   private: T theData; }; #include “NewClass.cpp”

Methods for the template class All of the methods that manipulate newClass objects are now dependent on the type T. Within the template class declaration, the compiler already knows about this dependency. So declaration of methods written like they have always been. Ex1. void setData (const T & newData);

Outside some rules must be followed to tell the compiler about the dependency. the template prefix template <class T> is placed immediately before each function heading that uses the class. Each use of the class name (such as NewClass) is changed to the template class name such as NewClass <T> Each use of complete underlying type name (such as NewClass::T) may be shortened to just the type name (such as T).

Methods template <class T> T NewClass::setData(const T & newValue ) { theData = T; //assumes = is defined for T } NewClass <T> NewClass <T>::operator + (const NewClass<T> & newClass1, const NewClass<T> & newClass2) NewClass <T> result; result = ??? return result;

Ex2. Instead non-member function looks like NewClass nmFunction(const NewClass & newClass1, const NewClass & newClass2) { . . . }   template <class T> NewClass <T> nmFunction (const NewClass<T> & newClass1, const NewClass<T> & newClass2) … Ex2. Instead non-member function looks like

NOTE: The name NewClass is changed to NewClass <T> only when it is used as a class name  void main() { NewClass <int> myFirst; NewClass <int> mySecond (2); NewClass <int> myThird (33); NewClass <double> myFourth (4.5);   myFirst.setData (5); cout << myFourth.getData() << endl; myFirst = nmFunction (mySecond, myThird); . :

Make the implementation visible Annoying requirement: The implementation of the methods must be present in the header file. Trick: Keep the implementations in a separate implementation file, but place an “include” directive at the bottom of the header file to pick up those implementations. So we have files NewClass.h and NewClass.cpp

Parameter Matching for Methods of Template Classes In template non-member functions, we are careful to help the compiler by providing a template parameter for each of the function’s parameters. HOWEVER, this help is not needed for member functions of a template class.

Homework Write a template function called swap() that interchanges the values of the two arguments sent to it. Then write a main() function to test the function with several types. Due: Tuesday 10 points Extra credit +5 point if you turn in coded program with output.