Templates C++ template

Slides:



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

C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
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 
1) More on parameter passing: a) Parameter association b) Default parameters c) Procedures as parameters 2) Modules: Ada packages, C modules, C header.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Templates Zhen Jiang West Chester University
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Learners Support Publications Classes and Objects.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
CS Introduction to Data Structures Spring Term 2004 Franz Hiergeist.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
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?
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
Templates Where the TYPE is generic. Templates for functions Used when the you want to perform the same operation on different data types. The definition.
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.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Motivation for Generic Programming in C++
Chapter 12 Classes and Abstraction
TK1924 Program Design & Problem Solving Session 2011/2012
Regarding assignment 1 Style standards Program organization
Procedural and Object-Oriented Programming
Chapter 22 - C++ Templates
Classes C++ representation of an object
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.
A First C++ Class – a Circle
Chapter 14 Templates C++ How to Program, 8/e
Classes Object-oriented programming: Example: Bank transactions
C++ Object-Oriented Programming
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?
Class: Special Topics Copy Constructors Static members Friends this
Templates.
Introduction to Classes
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Code Organization Classes
Andy Wang Data Structures, Algorithms, and Generic Programming
Introduction to Classes and Objects
Classes and Objects.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
9-10 Classes: A Deeper Look.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Classes C++ representation of an object
Chapter 22 - C++ Templates
Templates C++ template
Chapter 22 - C++ Templates
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
CMSC 341 C++ and OOP.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Code Organization Classes
9-10 Classes: A Deeper Look.
Classes and Objects Systems Programming.
Presentation transcript:

Templates C++ template A function or a class that has type independent elements The unspecified types are assigned at compile time Depending on how the template is used A form of compile-time polymorphism

Function templates Identified with the construct template <class T> Or template <typename T> Placed before the template function “T” is a placeholder for the type It is user defined, can be anything Used in the template as a “type” // swaps the values for two // elements of type “T” template <class T> void swapValues(T &x, T &y) { T temp; temp=y; y=x; x=temp; }

Function templates (usage) Using a template function Specify the type within angel braces “<>” Examples: swapValues<int>(i,j); // swap 2 integers swapValues<float>(x,y); // swap 2 floats swapValues<string>(a,b); // swap 2 strings Compiler will generate the appropriate function(s) // swaps the values for two // elements of type “T” template <class T> void swapValues(T &x, T &y) { T temp; temp=y; y=x; x=temp; }

Template class Similar to creating a template function Use the template keyword construct to identify both the class and any necessary data members as template items template <class T> Or template <typename T> “T” is a placeholder for the type It is user defined (“T” is common, but it can be anything) Used in the template as a “type”

Template Class Example #ifndef MYCLASS_H #define MYCLASS_H template <typename eltType> class MyClass { public: MyClass(); // default constructor! void setValue(int v) ; eltType getValue(); private: eltType value; }; MyClass<eltType>::MyClass() { value=0; } void MyClass <eltType> ::setValue(eltType v) { value= v; eltType MyClass <eltType> ::getValue() { return value; #endif A templated class is identified with the template keyword construct As are all member methods And the class name with the scope resolution operator (::) Template type (eltType) is used wherever the template data member is needed Usage is similar to a template function MyClass<int> I; Creates an instance I to hold an integer MyClass<float> F; Creates an instance F to hold a float F.setValue(3.14); // set the float value

Compilation Simplest approach is to put template functions and classes in a header (.h) file Include the header in any file that uses the template Do not use separate header and implementation files for classes Do not compile the template files separately This approach allows the compiler to create the necessary function/class types upon compilation Otherwise, if the templates are compiled separately, class/function prototypes will need to be defined for all types used in the target program So that all used function/template types are available when linking occurs