Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 20101 Templates (again) CS-2303 System Programming Concepts (Slides.

Similar presentations


Presentation on theme: " 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 20101 Templates (again) CS-2303 System Programming Concepts (Slides."— Presentation transcript:

1  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 20101 Templates (again) CS-2303 System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel, and from The C++ Programming Language, 3 rd edition, by Bjarne Stroustrup)

2  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 20102 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

3  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 20103 Review — Function Templates Written by programmer once Defines a whole family of overloaded functions Begins with the template keyword Contains a template parameter list of formal type and the parameters for the function template are enclosed in angle brackets ( <> ) Formal type parameters –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 4, Miscellaneous C++ topics

4  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 20104 Software Engineering Note Most C++ compilers require the complete definition of a template in the client source-code file using the template For this reason & for reusability, templates are often defined in header files –Included via #include into the appropriate client files For class templates, member functions are also defined in the header file

5  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 20105 Function Template – Example Forms template f() { } template g(){ } template h() { } Common Programming error:– Forgetting to say class or typename Another common Programming error:– Forgetting to say class or typename every parameter Example template functions:– D&D, Fig 26.1

6  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 20106 Common Programming Error If a template is invoked with a user-defined type, and … if that template uses functions or operators (e.g., ==, +, <=) with objects of that class type, then … those functions and operators must be overloaded for the user-defined type –Forgetting to overload such operators causes compilation errors

7  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 20107 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 Definition:–

8  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 20108 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 the 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 )

9  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 20109 Questions about Function Templates?

10  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 201010 Class Templates Class-template definitions are preceded by a header –Such as template Type parameter T can be used as a data type in member functions and data members Additional type parameters can be specified using a comma-separated list – e.g., –template Also known as Parameterized Types

11  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 201011 Class Templates (continued) Class templates encourage software reusability by enabling type-specific versions of generic classes to be instantiated.

12  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 201012 Example Stack.h –(1 of 3) Create class template Stack with type parameter T Member functions that use type parameter T in specifying function parameters

13  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 201013 Data member stackPtr is a pointer to a T Member-function template definitions that appear outside the class-template definition begin with the template header Example Stack.h –(2 of 3)

14  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 201014 Example Stack.h –(3 of 3)

15  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 201015 Create class-template specialization Stack where type double is associated with type parameter T Example Fig25-3.cpp –(1 of 2)

16  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 201016 Create class-template specialization Stack where type int is associated with type parameter T Example Fig25-3.cpp –(2 of 2)

17  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 201017 Use a function template to process Stack class-template specializations Example Fig25-4.cpp –(1 of 2)

18  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 201018 Example Fig25-4.cpp –(2 of 2)

19  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 201019 Questions?

20  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 201020 Non-type Parameters and Default Types for Class Templates Nontype template parameters –Can have default arguments –Are treated as const –Example Template header: template... Declaration: Stack salesFigures; Type parameters can have default arguments too –Example Template header: template... Declaration: Stack<> jobDescriptions;

21  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 201021 Explicit Specializations Used when a particular type will not work with the general template or requires customized processing Example for an explicit Stack specialization –template<> class Stack { … }; Complete replacements for the general template –Might not use anything from the original class template –MIght even have different members

22  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 201022 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

23  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 201023 Templates and Friends Assume:– template class X A function can be the friend of every class- template specialization instantiated from a class template –friend void f1(); f1 is a friend of X, X, etc. A function can be the friend of only a class- template specialization with the same type argument –friend void f2( X & ); f2( X & ) is a friend of X but not a friend of X

24  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 201024 Templates and Friends (continued) A member function of another class can be the friend of every class-template specialization instantiated from a class template –friend void A::f3(); f3 of class A is a friend of X, X, etc. A member function of another class can be the friend of only a class-template specialization with the same type argument –friend void C ::f4( X & ); C ::f4( X & ) is a friend of X but not a friend of X

25  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 201025 Templates and Friends (continued) Another class can be the 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, X, etc. A class-template specialization can be the friend of only a class-template specialization with the same type parameter –friend class Z ; Class-template specialization Z is a friend of X, Z is a friend of X, etc.

26  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 201026 Templates and static Members Each class-template specialization has its 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

27  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 201027 Questions about Class Templates?

28  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 201028 Templates in the C++ Standard Library Strings is the template class string is typedef basic_string There are lots of other kinds of strings –Japanese, Chinese, w_char, etc. Vector Generalization of one-dimensional array Includes (stack operations, etc.) vector is a specialization for one bit per bool item List Ditto

29  2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 201029 Templates in the C++ Standard Library (continued) ValArray bitset Bitwise operations on bits Iterators For sequencing through collections of objects …


Download ppt " 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term 20101 Templates (again) CS-2303 System Programming Concepts (Slides."

Similar presentations


Ads by Google