Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Templates Chapter 12. 2 What You Will Learn Using function templates to created a group of overloaded functions Using class templates to create a group.

Similar presentations


Presentation on theme: "1 Templates Chapter 12. 2 What You Will Learn Using function templates to created a group of overloaded functions Using class templates to create a group."— Presentation transcript:

1 1 Templates Chapter 12

2 2 What You Will Learn Using function templates to created a group of overloaded functions Using class templates to create a group of related types Distinguish between function templates and template functions

3 3 Introduction Templates enable us to specify an entire range of related functions –takes a single code segment –the related functions are overloaded –called template functions Can also have template classes –might write a single class template for a stack class –then have C++ generate stack-of-int, stack-of- float, etc. classes

4 4 Function Templates Overloaded functions –normally used to perform similar operations on different types of data overload + operator for complex numbers If operations are identical for each type, this could be done more compactly using function templates –programmer writes single function template definition –compiler generates separate object-code functions for each type of call

5 5 Function Templates Function template definition syntax template void DoSomething ( Whatever Stuff) {... } The function DoSomething can be called with different types for the parameter Stuff The compiler generates separate code for each call, using the appropriate type See Figure 12.1 on Text CD See Figure 12.1 on Text CD Required syntax

6 6 Function Templates template void DoSomething ( T1 *tPtr, T1 v1, T2 v2) {... } Needs appear only once here to appear multiple times here Specify as many classes in the template as types will vary in the different implementations to be used

7 7 Function Template template void DoSomething ( T1 *tPtr, T1 v1, T2 v2) {... } // calls to DoSomething int intval = 5; float floatval, *fptr, char ch, * cptr; Dosomething (cptr, ch, floatval); DoSomething (fptr, floatval, intval); View Figure 12.2 with audio from text CD

8 8 Overloading Template Functions Provide other function templates that specify the same function name but different function parameters Provide other non-template functions with the same function name but with different arguments Compiler determines which function to call –looks for precise match –then looks for function template which would match

9 9 Class Templates Consider a class which would implement a matrix, or a list, or a stack –is a set of numbers and the various operations on those numbers Possible to need one of these classes for different types of numbers –integer stack, float stack, character stack, etc. Need means for describing our class generically and instantiating the class for different types ==> CLASS TEMPLATE !

10 10 Class Templates // syntax example of declaration template class Whatever { public: void DoIt (T1 tval);... private: T1 tlist[30]; } ; // syntax example of declaration template class Whatever { public: void DoIt (T1 tval);... private: T1 tlist[30]; } ; // Syntax example of instantiation Whatever intWhatever; Specifies type to be used wherever the T1 appears in the class template declaration View fig 12.3, listen to text CD audio

11 11 Class Templates // syntax for definition template void Whatever ::DoIt (T1 tval) { … cout << tval; … } Specified prior to each member function definition Included in each function heading Used as type in parameter list and function as needed Important !!! Most compilers require declarations and definitions to be in the same file Not separate.h and.cpp files

12 12 Non-Type Parameters When we saw the previous declaration template T1 is called a "type parameter" –we are using the parameter to specify what the type will be Also possible to have parameters which do not specify a type -- non-type parameters template

13 13 Templates & Inheritance A class template can be derived from a template class A class template can be derived from a non- template class A template class can be derived from a class template A non-template class can be derived from a class template

14 14 Templates & Friends Consider the following declaration: template class P { friend void snarf ( ) ;... }; Function snarf is a friend of every template class instantiated from class P

15 15 Templates & Friends Given this declaration: template class P { friend void snarf ( ) ; friend void blat (P &);... }; For a particular type of Q (say int), blat (P &) is a friend of P only

16 16 Templates & Friends Friendship can be established between a class template and … –a global function –a member function of another class (possibly a template class) –an entire class (possibly a template class)

17 17 Templates & Static Members With a non-template class –one copy of a static data member is shared among all objects of the class –the static data member must be initialized at file scope With a template class –each instantiantion has its own copy of each static data member of the class template –all objects of that template class share that one static data member –static data members must be initialized at file scope


Download ppt "1 Templates Chapter 12. 2 What You Will Learn Using function templates to created a group of overloaded functions Using class templates to create a group."

Similar presentations


Ads by Google