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.

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

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.
Lecture 18: 4/11/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Specification and Implementation Separating the specification from implementation makes it easier to modify programs. Changes in the class’s implementation.
CS-212 Classes (cont) Dick Steflik. Member functions Member functions of a class include – getters used to retrieve state data – setters used to set state.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
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.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
Intro to Generic Programming Templates and Vectors.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
Programming Languages and Paradigms Object-Oriented Programming.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
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.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Templates ~ their instantiation and specialization.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
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 =
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.
Computer Engineering Rabie A. Ramadan Lecture 5.
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. 4.
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
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
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.
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?
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
Individual Testing, Big-O, C++ Bryce Boe 2013/07/16 CS24, Summer 2013 C.
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.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
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.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
Chapter 2 Objects and Classes
TK1924 Program Design & Problem Solving Session 2011/2012
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.
Templates.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Exceptions, Templates, and the Standard Template Library (STL)
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.
Templates.
CS212: Object Oriented Analysis and Design
Name: Rubaisha Rajpoot
Classes.
COP 3330 Object-oriented Programming in C++
Templates Generic Programming.
Presentation transcript:

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 at compile time. Attributes are specified in the template definition and are resolved by doing text substitutions by the compiler preprocessor Think of it like doing text “replaces” in a work processor

Template Functions Isn’t really a function, design of a function starts off with Template –in the function occurrences of text will be replaced with actual type of submitted parameter 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 the instructions 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 template functions

General Form of a Template class

General Form of a Template Class template class templateClass { public: // constructor templateClass (const T& item); // member functions T f( ); void g (const T& item); private: T dataValue; … };

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 example 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) { } templat const T & MemoryCell :: read ( ) const { return storedValue; } template void MemoryCell :: write (const T & x) { storedValue = x ;}