Sams Teach Yourself C++ in 21 Days

(singke) #1
void append( int value );
int is_present( int value ) const;
int is_empty() const { return head == 0; }
int count() const { return theCount; }
private:
class ListCell
{
public:
ListCell( int value, ListCell *cell = 0):val(value),
next(cell){}
int val;
ListCell *next;
};
ListCell *head;
ListCell *tail;
int theCount;
};


  1. Write the implementation for the Listclass (nontemplate) version.

  2. Write the template version of the implementations.

  3. Declare three list objects: a list of Strings, a list of Cats, and a list of ints.
    5.BUG BUSTERS:What is wrong with the following code? (Assume the Listtem-
    plate is defined and Catis the class defined earlier in the book.)
    List Cat_List;
    Cat Felix;
    CatList.append( Felix );
    cout << “Felix is “
    << ( Cat_List.is_present( Felix ) )? “” : “not “
    << “present” << endl;
    Hint(this is tough): What makes Catdifferent from int?

  4. Declare friend operator==for List.

  5. Implement friend operator==for List.

  6. Does operator==have the same problem as in Exercise 5?

  7. Implement a template function for swapthat exchanges two variables.


714 Day 19

Free download pdf