Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.

Slides:



Advertisements
Similar presentations
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
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.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
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.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Templates CS-240 Dick Steflik & DJ. Foreman. Reuse Templates allow us to get more mileage out of the classes we create by allowing the user to supply.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
Review of C++ Programming Part II Sheng-Fang Huang.
Intro to Generic Programming Templates and Vectors.
OOP Languages: Java vs C++
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
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.
EEL 3801 Part VIII Fundamentals of C and C++ Programming Template Functions and Classes.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Chapter 10 Introduction to Classes
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
Object Oriented Programming (OOP) Lecture No. 11.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
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:
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
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.
Overview of C++ Templates
Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Chapter 3 Templates Saurav Karmakar Spring Objective In Chapter 3, we will discuss: The concept of a template Function templates Class 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.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Individual Testing, Big-O, C++ Bryce Boe 2013/07/16 CS24, Summer 2013 C.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
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)
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 2006 Pearson Education, Inc. All rights reserved Templates.
C++ Functions A bit of review (things we’ve covered so far)
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Programming with ANSI C ++
How to be generic Lecture 10
C++ Templates.
Templates.
Chapter 14 Templates C++ How to Program, 8/e
CS410 – Software Engineering Lecture #11: C++ Basics IV
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 5 Classes.
Classes.
CS212: Object Oriented Analysis and Design
Templates.
Object-Oriented Programming (OOP) Lecture No. 32
CS212: Object Oriented Analysis and Design
Java Programming Language
CS410 – Software Engineering Lecture #5: C++ Basics III
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Presentation transcript:

Templates CS212 & CS-240

Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified in template definition Resolved by compiler via text substitutions Applies template parameter to template body. Like text “replace” in a wordprocessor

Template Functions Not really a function, It's the design (a pattern) of a function starts off with template –in function body, name will be replaced with actual type of submitted parameter –typename may be substituted for class multiple substitutions can be specified by using

Template Function template T sum3 ( T a, T b, T c) { return (a+b+c); } int main ( ) { int l,m,n; double r,s,t; /* use sum3 to sum sets variables of different types */ cout << sum3 (l,m,n) << “ “ << sum3 (r,s,t); }

Template class Like a function template is not a function a template class is not a class, but it is a pattern for the compiler on how to define the class template parameters are specified the same as for template functions text substitutions are done explicitly not implicitly like for template functions

General Form of a Template Class template // choices are: class or typename class templateClass { public: // constructor templateClass (const T& item); // member functions T f( ); void g (const T& item); private: T dataValue; … }; Required keywords, not related to typename Name of your new class

Sample usage template class mine; // declared, not defined yet class mine *mineptr; // declare pointer class mine newone; // error, unknown size template class mine {// template class defined here }; //"newone wants to be a "class template" // "mine" is the "template class"

MemoryCell Template class template class MemoryCell { public: /* constructor */ explicit MemoryCell( const T & initVal = T( ) ) : storedValue(initValue){ } /* accessor functions */ const T & read ( ) const { return storedValue; } void write (const T & x) { storedValue = x; } private: T storedValue; }

MemoryCell class Notice, previous examples use “inline” code to define the class Notice also that the syntax is slightly different The next example separates into interface and implementation

MemoryCell interface template class MemoryCell { public: explicit MemoryCell (const T & initVal = T( )); const T & read( ) const; void write (const T & x); private: T storedValue; }

MemoryCell implementation #include “MemoryCell.h” template MemoryCell :: MemoryCell (const T & initVal) : storedValue(initVal) {// code for default constructor (if any)} template const T & MemoryCell :: read ( ) const { return storedValue; } template void MemoryCell :: write (const T & x) { storedValue = x ;}

Using the template class Assuming that we had defined a template class named stack –to define a stack of integers called myIntStack you would use: stack myIntStack; –to define a stack of doubles called myDblStack you would use: stack myDblStack; –to define a stack of people called myFolks you would use: stack myFolks;

Muddying the waters a bit // uses items T in container C class Myclass { public: ~Myclass(); void push( const T & ); private: C item; }; Myclass > newone; // now create one