|
|
 |
|
|
|
|
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. |
|
|
|
 |
|
|
|
|
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: |
|
|
|
 |
|
|
|
|
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 |
|
|
|
 |
|
|
|
|
Here is an example of client code: |
|
|
|
 |
|
|
|
|
ExpArray myArray(100);
// Assert: Class object created with array size 100
.
.
.
myArray.ExpandBy(50);
// Assert: Array size is now 150 |
|
|
|
 |
|
|
|
|
(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.) |
|
|
|
 |
|
|
|
|
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. |
|
|
|
|
|
|
|
|
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. |
|
|
|
 |
|
|
|
|
a. Give the specification of the copy-constructor (as it would appear in the class declaration), then give the implementation of the function. |
|
|
|
 |
|
|
|
|
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: |
|
|
|
 |
|
|
|
|
class DynArray
{
|
|
|
|
|
|