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,

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 © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
1 Templates Chapter What You Will Learn Using function templates to created a group of overloaded functions Using class templates to create a group.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Chapter 14: Overloading and Templates
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Rossella Lau Lecture 11, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 11: Template and Operator overload  Template.
Specification and Implementation Separating the specification from implementation makes it easier to modify programs. Changes in the class’s implementation.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Stacks and Queues. Sample PMT online… Browse 1120/sumII05/PMT/2004_1/
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Basic Elements of C++ Chapter 2.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
Object Oriented Programming … and other things you need to program in java.
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 =
Learners Support Publications Classes and Objects.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Midterm Review CS1220 Spring Disclaimer The following questions are representative of those that will appear on the midterm exam. They do not represent.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
Chapter 8 Operator Overloading, Friends, and References.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
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.
11 Introduction to Object Oriented Programming (Continued) Cats.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Templates Templates for Algorithm Abstraction. Slide Templates for Algorithm Abstraction Function definitions often use application specific adaptations.
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.
CS Introduction to Data Structures Spring Term 2004 Franz Hiergeist.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and 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 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
1 Introduction to Object Oriented Programming Chapter 10.
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
C++ Templates 1. Why Use Templates? C++ requires variables, functions, classes etc with specific data types. However, many algorithms (quicksort for example)
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
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(
Operator Overloading.
Template Classes CMPS 2143.
Templates.
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.
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 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for 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.
COP 3330 Object-oriented Programming in C++
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Submitted By : Veenu Saini Lecturer (IT)
Really reusable software
Class: Special Topics Overloading (methods) Copy Constructors
Class rational part2.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Templates CMSC 202, Version 4/02.
COP 3330 Object-oriented Programming in C++
Presentation transcript:

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, chars, strings, whatever. On page 91 of the text, a template class of BasicVector is declared. ( I’ll do another one here!!!) It both depend on an underlying data type of the items in the data structure.

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

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

Methods for the template class All of the methods that manipulate newClass objects are now dependent on the ItemType. 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 ItemType& newData);

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

Ex2. So now instead of NewClass operator +(const NewClass& newClass1, const NewClass& newClass2) {... } it is template NewClass operator + (const NewClass & newClass1, const NewClass & newClass2) { … }

NOTE: The name NewClass is changed to NewClass only when it is used as a class name. (The name NewClass is also used as the name of the NewClass’ constructor, and that usage stays as NewClass.) int main() { NewClass myFirst; NewClass mySecond (4.5); myFirst.setData (5); cout << mySecond.getData() << endl;

Make the implementation visible Annoying requirement: The implementation of the ST functions 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. For example, we can use size_t parameter for the NewClass’ resize function. Unlike ordinary template non-member function, the compiler is able to match a size_t parameter of a member function with any of the usual integer arguments (such as int or const int). Ex. myTable (42); //42 will convert to equivalent //size_t value by the compiler LOOK AT Table.h and Table.cpp and main skeleton in assignment