Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,

Slides:



Advertisements
Similar presentations
TEMPLATES Lecture Presented By SHERY KHAN Object Orienting Programming.
Advertisements

C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
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.
 2006 Pearson Education, Inc. All rights reserved Templates.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Classes: A Deeper Look Systems Programming.
A Deeper Look at Classes CS-2303, C-Term A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming.
 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.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Review of C++ Programming Part II Sheng-Fang Huang.
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.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
 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.
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.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 22 - C++ Templates Outline 22.1Introduction.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
LECTURE LECTURE 17 Templates 19 An abstract recipe for producing concrete code.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
 2006 Pearson Education, Inc. All rights reserved Templates.
Chapter 22 - C++ Templates
Functions.
Programming with ANSI C ++
How to be generic Lecture 10
C++ Templates.
Chapter 18 Introduction to Custom Templates
Templates.
Introduction to C++ Systems Programming.
Chapter 14 Templates C++ How to Program, 8/e
Introduction to Custom Templates
CS212: Object Oriented Analysis and Design
Templates.
6 Functions.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Templates Class Templates
Abstraction: Generic Programming, pt. 2
“Under the Hood” of Polymorphism
Classes, Constructors, etc., in C++
Containers and the Standard Template Library (STL)
Miscellaneous C++ Topics
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Symbolic Constants in C
Scope Rules and Storage Types
Programming Assignment #5
Your first C and C++ programs
Iterators Professor Hugh C. Lauer CS-2303, System Programming Concepts
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Digression on Loop Invariants
Chapter 8: Class Relationships
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
A Deeper Look at Classes
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Introduction to Classes and Objects
Presentation transcript:

Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CS-2303, A-Term 2012 Templates (again)

Outline Function templates and related (overloaded) functions Distinguishing between function templates and function-template specializations Class templates to create a group of related types Distinguishing between class templates and class-template specializations Overloading function templates Relationships among templates, inheritance, friends, and static members CS-2303, A-Term 2012 Templates (again)

Review — Function Templates Written by programmer once Defines a whole family of overloaded functions Begins with the template keyword Contains template parameter list of formal types Formal type parameters Enclosed in angle brackets (<>) Preceded by keyword typename or keyword class Placeholders for fundamental or user-defined types Similar to macros in C, but with full type checking Week 5, Miscellaneous C++ topics §16.1 of Absolute C++ CS-2303, A-Term 2012 Templates (again)

Software Engineering Note Most C++ compilers require complete definition of template in client source-code using the template  Templates often defined in header files Included via #include into the appropriate client files For class templates, member functions must also be defined in header file CS-2303, A-Term 2012 Templates (again)

Function Template — Example Forms Common Programming error:– Forgetting to say class or typename template<typename T> f() { } template<class ElementType> g(){ } template<typename BorderType, typename Filltype> h() { } Another common Programming error:– Forgetting to say class or typename for every parameter CS-2303, A-Term 2012 Templates (again)

Common Programming Error If template is invoked with user-defined type, and … … if template uses functions or operators (e.g., ==, +, <=) with objects of class type, then … … those functions and operators must be overloaded for the user-defined type Forgetting to overload such operators causes compilation errors CS-2303, A-Term 2012 Templates (again)

Function Template Specialization Definition:– Function Template Specialization Specialization:– the automatic generation of a new “overloaded function” from the template as needed I.e., whenever a function template is called with a particular type Example for function template max with type parameter T called with int arguments Compiler detects a max invocation in the program code. int is substituted for T throughout the template definition. This produces function-template specialization max<int> CS-2303, A-Term 2012 Templates (again)

Overloading Function Templates Other function templates that specify the same name but different parameters Non-template functions that specify the same name but different parameters The compiler chooses best function or specialization to match the function call A non-template function is chosen over a template specialization in case of a tie Otherwise, multiple matches results in a compilation error (the compiler considers the function call to be an ambiguous function call ) CS-2303, A-Term 2012 Templates (again)

Questions about Function Templates? CS-2303, A-Term 2012 Templates (again)

Class Templates Class-template definitions are preceded by a header Also known as Parameterized Types Class Templates Class-template definitions are preceded by a header Such as template< typename T > Type parameter T can be used as data type in members functions and data Multiple type parameters can be specified using comma-separated list – e.g., template< typename T1, typename T2 > CS-2303, A-Term 2012 Templates (again)

Class Templates (continued) Class templates encourage software reusability … … by enabling type-specific versions of generic classes to be instantiated CS-2303, A-Term 2012 Templates (again)

Example Stack.h (1 of 3) Create class template Stack with type parameter T Member functions with type parameter T in parameter lists CS-2303, A-Term 2012 Templates (again)

Example (continued) Stack.h (2 of 3) Data member stackPtr is pointer to T Member-function template definitions appearing outside class-template definition begin with template header CS-2303, A-Term 2012 Templates (again)

Note: function bodies are spelled out in class header file! Stack.h (3 of 3) Note: function bodies are spelled out in class header file! CS-2303, A-Term 2012 Templates (again)

T might have to overload operator = Stack.h (3ʹ of 3) T might have to overload operator = Why? Digression: What if T is an abstract class with subclasses? CS-2303, A-Term 2012 Templates (again)

Example.cpp (1 of 2) Create specialization Stack< double > CS-2303, A-Term 2012 Templates (again)

Example.cpp (2 of 2) Create specialization Stack< int > CS-2303, A-Term 2012 Templates (again)

Test.cpp (1 of 2) Use a function template to process Stack class-template specializations CS-2303, A-Term 2012 Templates (again)

Test.cpp (2 of 2) CS-2303, A-Term 2012 Templates (again)

Questions? CS-2303, A-Term 2012 Templates (again)

Non-type Parameters and Default Types for Class Templates Nontype template parameters Can have default arguments Are treated as const Example Template header: template<typename T, int elements>... Declaration: Stack< double, 100 > salesFigures; Type parameters can have default arguments – example:– Template header: template< typename T = string >... Declaration: Stack<> jobDescriptions; CS-2303, A-Term 2012 Templates (again)

Templates and Inheritance A class template can be derived from a class-template specialization A class template can be derived from a non-template class A class-template specialization can be derived from a class-template specialization A non-template class can be derived from a class-template specialization CS-2303, A-Term 2012 Templates (again)

Templates and Friends Assume:– template<typename T> class X A function can be a friend of every class-template specialization instantiated from a class template friend void f1(); f1 is a friend of X<double>, X<string>, etc. A function can be the friend of only a class-template specialization with the same type argument friend void f2( X<T> & ); f2( X<float> & ) is a friend of X<float> but not a friend of X<string> CS-2303, A-Term 2012 Templates (again)

Templates and Friends (continued) A member function of another class can be a friend of every class-template specialization instantiated from a class template friend void A::f3(); f3 of class A is a friend of X< double >, X< string >, etc. A member function of another class can be a friend of only a class-template specialization with the same type argument friend void C<T>::f4( X<T> & ); C<float>::f4( X<float> & ) is a friend of X<float> but not a friend of X<string> CS-2303, A-Term 2012 Templates (again)

Templates and Friends (continued) Another class can be a friend of every class-template specialization instantiated from a class template friend class Y; Every member function of class Y is a friend of X<double>, X<string>, etc. A class-template specialization can be the friend of only a class-template specialization with the same type parameter friend class Z< T >; Class-template specialization Z<float> is a friend of X<float>, Z<string> is a friend of X<string>, etc. CS-2303, A-Term 2012 Templates (again)

Templates and static members Each class-template specialization has own copy of each static data member All objects of that specialization share that one static data member static data members must be defined and, if necessary, initialized at file scope Each class-template specialization gets its own copy of the class template’s static member functions CS-2303, A-Term 2012 Templates (again)

Questions about Class Templates? CS-2303, A-Term 2012 Templates (again)

Templates in the C++ Standard Library Strings <basic_string> is the template class string is typedef basic_string<char> There are lots of other kinds of strings Japanese, Chinese, w_char, etc. Vector Generalization of one-dimensional array Includes (stack operations, etc.) vector<bool> is a specialization for one bit per bool item List Ditto CS-2303, A-Term 2012 Templates (again)

Templates in the C++ Standard Library (continued) ValArray bitset Bitwise operations on bits Iterators For sequencing through collections of objects … CS-2303, A-Term 2012 Templates (again)

Questions? CS-2303, A-Term 2012 Templates (again)

CS-2303, A-Term 2012 Templates (again)