- 1 - 29 March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part.

Slides:



Advertisements
Similar presentations
Chapter 7: User-Defined Functions II
Advertisements

C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 14: Overloading and Templates
14 Templates. OBJECTIVES In this chapter you will learn:  To use function templates to conveniently create a group of related (overloaded) functions.
 2006 Pearson Education, Inc. All rights reserved Templates.
 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.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Operator Overloading and Type Conversions
Chapter 12: Adding Functionality to Your Classes.
 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.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
ECE122 Feb. 22, Any question on Vehicle sample code?
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 in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Copyright 2008 Oxford Consulting, Ltd 1 October C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to.
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.
Object Oriented Programming Elhanan Borenstein Lecture #10 copyrights © Elhanan Borenstein.
 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.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Templates Class Templates Used to specify generic class types where class members data types can be specified as parameters, e.g. here is a generic List.
CS212: Object Oriented Analysis and Design
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Chapter -6 Polymorphism
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 22 - C++ Templates Outline 22.1Introduction.
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
 Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 2006 Pearson Education, Inc. All rights reserved Templates.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
TK1924 Program Design & Problem Solving Session 2011/2012
Functions + Overloading + Scope
2 Chapter Classes & Objects.
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.
FUNCTIONS In C++.
Function Overloading.
Review: Two Programming Paradigms
Constructor & Destructor
CS212: Object Oriented Analysis and Design
Polymorphism Polymorphism
Classes and Objects.
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Templates Generic Programming.
C++ Class Members Class Definition class Name { public: constructor(s)
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Templates Generic Programming.
Presentation transcript:

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Templates F Part of the ongoing development of the C++ language F Integral part of the larger C++ Standard Library The Standard Template Library

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Based upon the notion of a type variable A variable that takes a type as its value T i, j, k; Declares 3 variables The type of the variables depends upon the value of T If T is  intvariables are of type int  floatvariables are of type float  user-definedvariables are of type user-defined

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Kinds of Templates o Function Permit the development of generic algorithms o Class Permit the development of generic objects

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates General Syntax template normal declaration or definition template C++ keyword T i One or more formal parameters A formal parameter Must be written as:class T i Type parameter cannot have default value Non-type parameter can have default values

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates General Syntax - Parameters Type Parameters Built-in - Non-type  int  char  float* User Defined Functions In older compilers function templates cannot use non-type parameters. The function definition can. Classes The class template can use non-type parameters.

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Function Templates template T min (T var1, T var2); The function min Has two arguments:var1, var2 of type T Returns a variable:of type T template T min (T* ptVar1, T* ptVar2); The function min Has two arguments ptVar1, ptVar2 of type pointer to T Returns a variableof type T

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Function Templates A templated function can be declared  extern  inline  static template inline T min (T var1, T var2);

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Function Templates Writing Function Templates 1.Write a specific version for each fixed type. 2.Compile and test each version. 3.Replace the fixed type with the type parameter. 4.Add the template header.

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Function Templates Template Instantiation 1.The formal parameters in the function signature are examined in turn. A return type cast does not matter. template T f(T); int b; double aValue = (double) f(b); T has the value int. 2.Each formal parameter involving a type parameter is matched with the corresponding argument in the function call. The match binds the type parameter.

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Function Templates Template Instantiation 3.All matches must be consistent. Only trivial promotions are allowed. int& to const int& 4. Non-Type formal parameters must match without non-trivial conversion or promotion

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Functions Function Call Resolution 1.Examine all non-template functions if an exact match call that version if multiple matches compile error 2.Examine all template functions if an exact match instantiate then call that version if multiple matches compile error 3.Re-examine all non-template functions Apply call resolution rules for overloaded functions

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Class Templates oParticularly useful for making classes generic oImportant application is container classes

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Class Templates Class Template  Template header  Normal Class Definition template class ClassName { class body }

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Class Templates Function Members  Use the same template header as the class  Are instantiated when the compiler first sees a call to it Syntax template returnType ClassName :: functionName (args) { function body }

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Class Templates Static Members A templated class can have static members Each type has an associated set of static members Declarations static T my Function (args) { myFunction body} static int myValue// must be defined outside of the class Definition template int MyClass ::myValue = aValue;

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Class Templates Forward Declarations….. template class MyClass // forward reference …. ….and Friends template class MyClass { friend class YourClass ; public: …… }

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Class Templates Declaring an Instance template class MyClass { public: …… } MyClass myInstance; Observe the use of as part of the class name

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Class Templates oDefinition and Declaration  If using header files, put the declaration and definition both in the header file. oTemplates as a type parameter  The value of a type parameter can be a built-in or user defined type.  The user defined type may be non-typed or typed.

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Class Templates Templates and Inheritance  Derivation works the same as with ordinary classes  One can create a new template object from an existing template

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Class Templates Default Parameters  Relatively new feature to templates  A default value can be specified in a template definition  Supported by most newer compilers. Borland 5.0 Microsoft 4.0 and beyond template class MyClass { ….. }

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Function and Class Templates o Specialization May need to a special version of a template to handle types not easily included in an existing template. Function returnType functionName (args) { function body } Member Function className functionMember (args) { function body } Class class className { class body };

March 2006 Copyright, 2006 Oxford Consulting, Ltd C++ Templates Function and Class Templates o Explicit Instantiation May wish to create a specific version of a function or class Function returnType functionName (args); Class class className