170 likes | 499 Views
Templates. Chapter 12. 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. Introduction.
E N D
Templates Chapter 12
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
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
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
Function Templates Required syntax • Function template definition syntaxtemplate < class Whatever >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
Function Templates Specify as many classes in the template as types will vary in the different implementations to be used Needs appear only once here to appear multiple times here template < class T1, class T2 >void DoSomething ( T1 *tPtr, T1 v1, T2 v2) { . . . }
Function Template template < class T1, class T2 >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
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
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 !
Class Templates Specifies type to be used wherever theT1 appears in theclass template declaration // syntax example of declaration template < class T1 > class Whatever { public: void DoIt (T1 tval); . . . private: T1 tlist[30]; } ; View fig 12.3, listen to text CD audio // Syntax example of instantiationWhatever < int > intWhatever;
Class Templates Important !!!Most compilers require declarations and definitions to be in the same file Not separate .h and .cpp files Specified prior to each member function definition // syntax for definition template <class T1>void Whatever < T1 >::DoIt (T1 tval) { … cout << tval; … } Included in each function heading Used as type in parameter list and function as needed
Non-Type Parameters template < class T1, int size > • When we saw the previous declarationtemplate < class T1 >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
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
Templates & Friends • Consider the following declaration:template < class Q > class P { friend void snarf ( ) ; . . . }; • Function snarf is a friend of every template class instantiated from class P
Templates & Friends • Given this declaration:template < class Q > class P { friend void snarf ( ) ; friend void blat (P < Q > &); . . . }; • For a particular type of Q (say int), blat (P<int>&)is a friend of P<int> only
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)
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