Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Exercises

  1. Specify the set of values and the set of operations provided by each of the following C++ built-in data types:
    1. char,
    2. int,
    3. double, and
    4. string.
  2. What are the features of C++ that facilitate the creation of user-defined data types.
  3. Explain how each of the following C++ features supports polymorphism:
    1. operator overloading,
    2. templates, and
    3. inheritance.
  4. Suppose we define two concrete classes, A and B, both of which are derived from the Object class declared in Program gif. Furthermore, let a and b be instances of classes A and B (respectively) declared as follows:
    class A : public Object { ... };
    class B : public Object { ... };
    A a;
    B b;
    Give the sequence of functions called in order to evaluate a comparison such as ``a<b''. Is the result of the comparison true or false? Explain.
  5. Consider the Wrapper<T> class defined in Program gif. Explain the operation of the following program fragment:
    int i = 5;
    Wrapper<int> j = 7;
    i = j;
    j = i;
  6. There are three ways to test whether a given object instance, obj, is the null object:
    1. obj.IsNull ()
    2. obj == NullObject::Instance (), and
    3. &obj == &(NullObject::Instance ()).
    Discuss the relative merits of each approach.
  7. Let c be an instance of some concrete class derived from the Container class given in Program gif. Explain how the statement
    cout << c;
    prints the contents of the container on the standard output stream, cout.
  8. Suppose we have a container c (i.e., an instance of some concrete class derived from the Container class defined in Program gif) which among other things happens to contain itself. Is it permissible for c to own the objects it contains? What happens when c's destructor runs if it owns the objects it contains.
  9. Iterators and visitors provide two ways to do the same thing--to visit one-by-one all the objects in a container. Give an implementation for the Accept function of the Container class that uses an iterator.
  10. Is it possible to implement an iterator using a visitor? Explain.
  11. Suppose we have a container which we know contains only instances of the Int class defined in Program gif. Design a Visitor which computes the sum of all the integers in the container.
  12. Explain what the following visitor does and why it is a horribly bad idea:
    class DeletingVisitor : public Visitor
    {
    public:
        void Visit (Object& object)
            { delete &object; }
    };
  13. Consider the following pair of Associations:
    Association a (*new Int (3), *new Int (4));
    Association b (*new Int (3));
    Give the sequence of functions called in order to evaluate a comparison such as ``a==b''. Is the result of the comparison true or false? Explain.

next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.