Wrap-Up

Answers to Self Review Exercises

14.1

a) True. b) False. Keywords typename and class in this context also allow for a type parameter of a built-in type. c) True. d) False. Template parameter names among function templates need not be unique. e) True. f) False. It could be a nontemplate function. g) False. Each class-template specialization will have its own copy of the static data member.

14.2

a) function-template specializations, class-template specializations. b) template, angle brackets (< and >). c) overloading. d) parameterized. e) binary scope resolution. f) file.

Exercises

14.3

Write a function template selectionSort based on the sort program of Fig. 8.15. Write a driver program that inputs, sorts and outputs an int array and a float array.

14.4

Overload function template printArray of Fig. 14.1 so that it takes two additional integer arguments, namely int lowSubscript and int highSubscript. A call to this function will print only the designated portion of the array. Validate lowSubscript and highSubscript; if either is out of range or if highSubscript is less than or equal to lowSubscript, the overloaded printArray function should return 0; otherwise, printArray should return the number of elements printed. Then modify main to exercise both versions of printArray on arrays a, b and c (lines 2325 of Fig. 14.1). Be sure to test all capabilities of both versions of printArray.


14.5

Overload function template printArray of Fig. 14.1 with a nontemplate version that specifically prints an array of character strings in neat, tabular, column format.

14.6

Write a simple function template for predicate function isEqualTo that compares its two arguments of the same type with the equality operator (==) and returns true if they are equal and false if they are not equal. Use this function template in a program that calls isEqualTo only with a variety of built-in types. Now write a separate version of the program that calls isEqualTo with a user-defined class type, but does not overload the equality operator. What happens when you attempt to run this program? Now overload the equality operator (with the operator function) operator==. Now what happens when you attempt to run this program?

14.7

Use an int template nontype parameter numberOfElements and a type parameter elementType to help create a template for the Array class (Figs. 11.611.7) we developed in Chapter 11. This template will enable Array objects to be instantiated with a specified number of elements of a specified element type at compile time.

14.8

Write a program with class template Array. The template can instantiate an Array of any element type. Override the template with a specific definition for an Array of float elements (class Array< float >). The driver should demonstrate the instantiation of an Array of int tHRough the template and should show that an attempt to instantiate an Array of float uses the definition provided in class Array< float >.

14.9

Distinguish between the terms "function template" and "function-template specialization."

14.10

Which is more like a stencila class template or a class-template specialization? Explain your answer.

14.11

What is the relationship between function templates and overloading?

14.12

Why might you choose to use a function template instead of a macro?

14.13

What performance problem can result from using function templates and class templates?

14.14

The compiler performs a matching process to determine which function-template specialization to call when a function is invoked. Under what circumstances does an attempt to make a match result in a compile error?

14.15

Why is it appropriate to refer to a class template as a parameterized type?

14.16

Explain why a C++ program would use the statement

Array< Employee > workerList( 100 );
14.17

Review your answer to Exercise 14.16. Why might a C++ program use the statement

Array< Employee > workerList;
14.18

Explain the use of the following notation in a C++ program:

template< typename T > Array< T >::Array( int s )
14.19

Why might you use a nontype parameter with a class template for a container such as an array or stack?

 
14.20

Describe how to provide an explicit specialization of a class template.

14.21

Describe the relationship between class templates and inheritance.

14.22

Suppose that a class template has the header

template< typename T > class Ct1
 

Describe the friendship relationships established by placing each of the following friend declarations inside this class template. Identifiers beginning with "f" are functions, identifiers beginning with "C" are classes, identifiers beginning with "Ct" are class templates and T is a template type parameter (i.e., T can represent any fundamental or class type).

  1. friend void f1();
  2. friend void f2( Ct1< T > & );
  3. friend void C2::f3();
  4. friend void Ct3< T >::f4( Ct1< T > & );
  5. friend class C4;
  6. friend class Ct5< T >;
14.23

Suppose that class template Employee has a static data member count. Suppose that three class-template specializations are instantiated from the class template. How many copies of the static data member will exist? How will the use of each be constrained (if at all)?





C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627
Simiral book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net