Sams Teach Yourself C++ in 21 Days

(singke) #1
Answers 867

D



  1. The following is one possible answer:
    friend int operator==( const Type& lhs, const Type& rhs );

  2. The following is one possible answer:
    0: Exercise 19.7
    1: template
    2: int List::operator==( const Type& lhs, const Type& rhs )
    3: {
    4: // compare lengths first
    5: if ( lhs.theCount != rhs.theCount )
    6: return 0; // lengths differ
    7:
    8: ListCell lh = lhs.head;
    9: ListCell
    rh = rhs.head;
    10:
    11: for(; lh != 0; lh = lh.next, rh = rh.next )
    12: if ( lh.value != rh.value )
    13: return 0;
    14:
    15: return 1; // if they don’t differ, they must match
    16: }

  3. Yes, because comparing the array involves comparing the elements,operator!=
    must be defined for the elements as well.

  4. The following is one possible answer:
    0: // Exercise 19.9
    1: // template swap:
    2: // must have assignment and the copy constructor defined for the Type.
    3: template
    4: void swap( Type& lhs, Type& rhs)
    5: {
    6: Type temp( lhs );
    7: lhs = rhs;
    8: rhs = temp;
    9: }


Day 20


Quiz


  1. An exception is an object that is created as a result of invoking the keyword throw.
    It is used to signal an exceptional condition, and is passed up the call stack to the
    first catchstatement that handles its type.

  2. A tryblock is a set of statements that might generate an exception.


32 0672327112_app_d.qxd 11/19/04 12:30 PM Page 867

Free download pdf