Exercises

  1. 18.3 (Operator Overloads in Templates) 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 otherwise. Use this function template in a program that calls isEqualTo only with a variety of fundamental 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?

  2. 18.4 (Array Class Template) Reimplement class Array from Figs. 10.1010.11 as a class template. Exercise each of the new Array class template’s capabilities in a program.

  3. 18.5 Distinguish between the terms “function template” and “function-template specialization.”

  4. 18.6 Explain which is more like a stencil—a class template or a class-template specialization?

  5. 18.7 What’s the relationship between function templates and overloading?

  6. 18.8 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?

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

  8. 18.10 Explain why a C++ program would use the statement

    Array<Employee> workerList{100};
  9. 18.11 Review your answer to Exercise 18.10. Explain why a C++ program might use the statement

    Array<Employee> workerList;
  10. 18.12 Explain the use of the following notation in a C++ program:

    template <typename T> Array<T>::Array(int s)
  11. 18.13 Why might you use a nontype parameter with a class template for a container such as an array or stack?

  12. 18.14 (GradeBook Class Template) Reimplement class GradeBook from Section 7.9 as a class template with two nontype template parameters that represent the number of students and the number of tests (as described in Section 18.4). Test multiple instances of the GradeBook class template with different numbers of students and exams.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset