Main Page

Previous Page
Next Page

[Page 766 (continued)]



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.


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.

[Page 767]

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.


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?


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.


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 >.


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


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


What is the relationship between function templates and overloading?


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


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


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?


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


Explain why a C++ program would use the statement

Array< Employee > workerList( 100 );


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

Array< Employee > workerList;


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

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


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

[Page 768]

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


Describe the relationship between class templates and inheritance.


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 >;


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)?

Previous Page
Next Page