Answers 867
D
- The following is one possible answer:
friend int operator==( const Type& lhs, const Type& rhs ); - 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: } - Yes, because comparing the array involves comparing the elements,operator!=
must be defined for the elements as well. - 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
- 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. - 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