Answers to Self Review Exercises


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.


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



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.


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?


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

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism


Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger


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

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