< previous page page_1043 next page >

Page 1043
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
once memory has been allocated for the array, the array size cannot change while the program is executing. In this problem, you are to design and test a C++ class that represents an expandable arrayone that can grow in size at run time.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Using this chapter's DynArray class as a starting point, create a class named ExpArray. This class has all of DynArray's member functions plus one more:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
void ExpandBy( /* in */ int n );
    // Precondition:
    //     n > 0
    // Postcondition:
    //     Size of array has increased by n elements
    //  && All of the additional n elements equal zero
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Here is an example of client code:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
ExpArray myArray(100);
// Assert: Class object created with array size 100
  .
  .
  .
myArray.ExpandBy(50);
// Assert: Array size is now 150
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
(Hint: To expand the array, you should allocate a new, larger dynamic array on the free store, copy the values from the old dynamic array to the new, and deallocate the old array.)
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Test your class with a suitable test driver and a comprehensive set of test data. Note that your test driver should exercise the other class member functions to be sure they still work correctly.
Case Study Follow-Up
1. Rewrite the RecordList::SelSort function from the SortWithPointers program so that it correctly orders the structs regardless of whether characters in the last names are uppercase or lowercase. (Hint: Temporarily convert all the characters in both strings to uppercase before making the comparison.)
2. Rewrite the RecordList::SelSort function from the SortWithPointers program so that it orders the structs by last name, then first name (in case two or more people have the same last name).
3. We want to add two member functions to the RecordList class of the SortWithPointers program: a copy-constructor and a deep copy operation.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
a. Give the specification of the copy-constructor (as it would appear in the class declaration), then give the implementation of the function.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
b. Give the specification of a CopyFrom function (as it would appear in the class declaration), then give the implementation of the function.
4. In the Dynamic Arrays case study, suppose that the DynArray class had been written to store float rather than int values:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
class DynArray
{

 
< previous page page_1043 next page >