What is the name of a member function of a class that has the same name as the class that has no return type?

  1. Extend the IntList class defined above by adding a member function called Length. The function should return the number of items currently in the list. Write the new declaration that would be added to IntList.h as well as the new code that would be added to IntList.C [write the complete code for the new function , not just ... as in the example].

  2. Add a 2-argument constructor to the IntList class to allow an IntList to be initialized to contain n copies of value v. [So the two arguments are n and v, both of type int.] Again, write both the new declaration that would be added to IntList.h, and the new code that would be added to IntList.C.

To use the string class you must #include [be sure that you do not include string.h, because then you will get the header file for C-style strings rather than for the C++ string class].

  • A string variable can be declared with or without an initial value:
      string s1; // s1 is initialized to the empty string string s2["hello"]; // s2 is initialized to the string "hello" string s3 = "goodbye"; // s3 is initialized to the string "goodbye"

  • The string class provides a size function:
      string s1, s2 = "hello"; cout =0; k--] cout =0; k--] s1[k] = 'a'; // change s to "aaaaa" s[10] = 'a'; // ERROR! s only has 5 chars

To use the vector class you must #include . A vector is similar to an array, but vectors provide some operations that cannot be performed using C++ arrays, and vectors can be passed both by value and by reference [unlike C++ arrays, which are always passed by reference]. Unfortunately, there is no bounds checking for vectors [i.e., an index out of bounds does not necessarily cause a runtime error].

  • A vector variable is declared with the type of its elements and its size; for example:
      vector v1[10]; // v1 is a vector of 10 integers vector v2[5]; // v2 is a vector of 5 characters
    The specified size can be any expression that evaluates to a non-negative value.

  • Use indexing to access the elements of a vector as you would for an array:
      vector v[10]; for [int k=0; k

  • The vector class provides a size function:
      vector v1[10]; vector v2[5]; cout | 1 | | | +-> |----| | | | | 2 | | numItems: 10 | | |----| | | | | 3 | | arraySize: 10 | | |----| +---------------+ | | . | | | . | +---------------+ | | . | | | | |----| L: | Items: ---------+ | 10 | | | +----+ | | | numItems: 10 | | | | arraySize: 10 | +---------------+
    Now think about what happens when the body of function f executes. L.AddToEnd discovers that the array is full, so it allocates a new array, copies the values from the old array to the new array, and returns the old array to free storage. Unfortunately, L.AddToEnd doesn't know that I.Items is also pointing to the old array, so when that array is returned to free storage, I.Items becomes a dangling pointer, and any attempt to access the array it points to is likely to lead to trouble.

    TEST YOURSELF NOW

    Consider the StrList class defined below. A StrList stores a list of strings in a linked list pointed to by the StrList's head field. The Lookup operation determines whether a given string is in the list; if it is there, it is moved to the front of the list, and the value true is returned [otherwise, the list is unchanged, and the value false is returned].

      class StrList { public: // constructor StrList[]; // modifiers void AddToFront[string s]; bool Lookup[string s]; // other operations void Print[ostream &output] const; private: struct ListNode { string data; ListNode *next; }; // pointer to the first node of the list ListNode *head; };
    Consider the following code:
      void f[StrList L] { L.Lookup["b"]; } int main[] { StrList S; S.AddToFront["c"]; S.AddToFront["b"]; S.AddToFront["a"]; // S.head points to the linked list: // "a" -> "b" -> "c" f[S]; ... }
    Note that there is no StrList copy constructor [so the compiler will supply one]. Draw variables S and L as they would appear at the very beginning of function f [just after L's copy constructor is called to initialize it to be a copy of S]. Draw a second picture to illustrate what happens as a result of the call to L.Lookup in function f. What goes wrong because there is no StrList copy constructor?

    Recall that the declaration of a class's copy constructor is similar to that of its default [no-argument] constructor: the function has no return type [not even void], and its name is the same as the name of the class. However, unlike the default constructor, the copy constructor has one argument: its type is the class, and it is a const reference parameter. The argument is the object that the copy constructor is supposed to copy. For example:

      class IntList { public: IntList[]; // default constructor IntList[const IntList &L] // copy constructor ... };

    The definition of the copy constructor [the actual code for the function] should be put in a ".C" file, along with the code for the other class member functions. The copy constructor should copy the values of all non-pointer data members, and should copy the objects pointed to by all pointer data members. For example, the copy constructor for the IntList class should perform the following tasks:

    1. allocate a new array of ints of size L.arraySize [L is the copy constructor's IntList parameter]; set Items to point to the new array;
    2. copy the values in the array pointed to by L.Items to the new array;
    3. initialize the numItems and arraySize fields to have the same values as the ones in L.numItems and L.arraySize.
    Here is the code for the IntList copy constructor [note that, like the other constructor functions, the copy constructor can use a member initialization list to initialize data members, as well as using code in the body of the function]:
      IntList::IntList[const IntList & L]: Items[new int[L.arraySize]], numItems[L.numItems], arraySize[L.arraySize] { for [int k=0; k

Chủ Đề