DOC PREVIEW
LETU COSC 2103 - Templates

This preview shows page 1-2-3-4-5-6 out of 17 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

TemplatesWhat You Will LearnIntroductionFunction TemplatesSlide 5Slide 6Function TemplateOverloading Template FunctionsClass TemplatesSlide 10Slide 11Non-Type ParametersTemplates & InheritanceTemplates & FriendsSlide 15Slide 16Templates & Static Members1TemplatesChapter 122What You Will LearnUsing function templates to created a group of overloaded functionsUsing class templates to create a group of related typesDistinguish between function templates and template functions3Introduction•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. classes4Function 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 call5Function Templates•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.1on Text CDSee Figure 12.1on Text CDRequired syntaxRequired syntax6Function Templatestemplate < class T1, class T2 >void DoSomething ( T1 *tPtr, T1 v1, T2 v2) { . . . }template < class T1, class T2 >void DoSomething ( T1 *tPtr, T1 v1, T2 v2) { . . . }Needs appear only once here to appear multiple times hereSpecify as many classes in the template as types will vary in the different implementations to be used7Function Templatetemplate < class T1, class T2 >void DoSomething ( T1 *tPtr, T1 v1, T2 v2) { . . . }template < class T1, class T2 >void DoSomething ( T1 *tPtr, T1 v1, T2 v2) { . . . }// calls to DoSomethingint intval = 5; float floatval, *fptr, char ch, * cptr;Dosomething (cptr, ch, floatval);DoSomething (fptr, floatval, intval);View Figure 12.2 with audio from text CDView Figure 12.2 with audio from text CD8Overloading 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 match9Class 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 !10Class Templates// syntax example of declarationtemplate < class T1 >class Whatever { public: void DoIt (T1 tval); . . . private: T1 tlist[30]; } ;// syntax example of declarationtemplate < class T1 >class Whatever { public: void DoIt (T1 tval); . . . private: T1 tlist[30]; } ;// Syntax example of instantiationWhatever < int > intWhatever;// Syntax example of instantiationWhatever < int > intWhatever;Specifies type to be used wherever theT1 appears in theclass template declarationView fig 12.3, listen to text CD audioView fig 12.3, listen to text CD audio11Class Templates// syntax for definitiontemplate <class T1>void Whatever < T1 >::DoIt (T1 tval) { … cout << tval; … }Specified prior to each member function definitionIncluded in each function headingUsed as type in parameter list and function as neededImportant !!!Most compilers require declarations and definitions to be in the same file Not separate .h and .cpp filesImportant !!!Most compilers require declarations and definitions to be in the same file Not separate .h and .cpp files12Non-Type Parameters•When we saw the previous declaration template < 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 parameterstemplate < class T1, int size >template < class T1, int size >13Templates & 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 template14Templates & 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 P15Templates & 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> only16Templates & 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)17Templates & 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


View Full Document

LETU COSC 2103 - Templates

Documents in this Course
Arrays

Arrays

16 pages

Methods

Methods

22 pages

Methods

Methods

22 pages

Arrays

Arrays

11 pages

Load more
Download Templates
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Templates and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Templates 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?