Chp~19 - C+ PROGRAMMING Chapter 19 Templates What Are...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
C++ PROGRAMMING Chapter 19 Templates What Are Templates? Parameterized Types Template Definition Listing 19.1. A template of an Array class . Using the Name Implementing the Template Listing 19.2. The implementation of the template array . Template Functions Templates and Friends Non-Template Friend Classes and Functions Listing 19.3. Non-template friend function . General Template Friend Class or Function Listing 19.4. Using operator ostream . A Type-Specific Template Friend Class or Function Using Template Items Listing 19.5. Passing template objects to and from functions. Specialized Functions Listing 19.6. Specializing template implementations . Static Members and Templates Listing 19.7. Using static member data and functions with templates. The Standard Template Library Summary Quiz Exercises
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Chapter 19 Templates On Chapter 17, "The Preprocessor," you saw how to use macros to create various lists using the concatenation operator. Macros have a number of problems that are fixed by templates. ToChapter you will learn What templates are and how to use them. Why templates supply a better alternative to macros. How to create class templates. How to create function templates. What Are Templates? At the end of New Chapters, you saw how to build a PartsList object and how to use it to create a PartsCatalog . If you want to build on the PartsList object to make a list of cats, you have a problem: PartsList only knows about parts. To solve this problem, you can create a List base class and derive from it the PartsList and CatsList classes. You could then cut and paste much of the PartsList class into the new CatsList declaration. Next week, when you want to make a list of Car objects, you would then have to make a new class, and again you'd cut and paste. Needless to say, this is not a satisfactory solution. Over time, the List class and its derived classes will have to be extended. Making sure that all the changes are propagated to all the related classes would be a nightmare. On Chapter 17, one approach to parameterizing lists was demonstrated briefly--using macros and name concatenation. Although macros do save much of the cutting and pasting, they have one killer disadvantage: Like everything else in the preprocessor, they are not type-safe. Templates offer the preferred method of creating parameterized lists in C++. They are an integrated part of the language, they are type-safe, and they are very flexible. Parameterized Types Templates allow you to teach the compiler how to make a list of any type of thing, rather than creating a set of type-specific lists--a PartsList is a list of parts, a CatList is a list of cats. The only way in which they differ is the type of the thing on the list. With templates, the type of the thing on the list becomes a parameter to the definition of the class. A common component of virtually all C++ libraries is an array class. As you saw with
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 30

Chp~19 - C+ PROGRAMMING Chapter 19 Templates What Are...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online