Sams Teach Yourself C++ in 21 Days

(singke) #1

  1. The class must define a default constructor, a copy constructor, and an overloaded
    assignment operator.

  2. STL stands for the Standard Template Library. This library is important because it
    contains a number of template classes that have already been created and are ready
    for you to use. Because these are a part of the C++ standard, any compiler support-
    ing the standard will also support these classes. This means you don’t have to
    “reinvent the wheel!”


Exercises


  1. One way to implement this template:
    0: //Exercise 19.1
    1: template
    2: class List
    3: {
    4:
    5: public:
    6: List():head(0),tail(0),theCount(0) { }
    7: virtual ~List();
    8:
    9: void insert( Type value );
    10: void append( Type value );
    11: int is_present( Type value ) const;
    12: int is_empty() const { return head == 0; }
    13: int count() const { return theCount; }
    14:
    15: private:
    16: class ListCell
    17: {
    18: public:
    19: ListCell(Type value, ListCell cell =
    ➥0):val(value),next(cell){}
    20: Type val;
    21: ListCell
    next;
    22: };
    23:
    24: ListCell head;
    25: ListCell
    tail;
    26: int theCount;
    27: };

  2. The following is one possible answer:
    0: // Exercise 19.2
    1: void List::insert(int value)
    2: {
    3: ListCell *pt = new ListCell( value, head );
    4:


864 Appendix D

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

Free download pdf