|
|
|
|
|
|
|
Deallocate the dynamic array pointed to by arr
Set size = array2.size
Set arr = new int[size] //Allocate a new dynamic array
IF arr is NULL
Print error message
Halt the program
FOR i going from 0 through size - 1
Set arr[i] = array2.arr[i] |
|
|
|
|
|
|
|
|
|
Testing: To test the CopyFrom function, we could use the client code we presented earlier: |
|
|
|
|
|
|
|
|
for (index = 0; index << numlements; index++)
x.Store(index + 100, index);
y.CopyFrom(x);
for (index = 0; index << numElements; index++)
cout << y.ValueAt(index); |
|
|
|
|
|
|
|
|
If the value of numElements is 20, the output should be the values 100 through 119, demonstrating that y is a copy of x. |
|
|
|
|
|
|
|
|
Translating all of these pseudocode algorithms into C++, we obtain the following implementation file for the DynArray class. |
|
|
|
|
|
|
|
|
//******************************************************************
// IMPLEMENTATION FILE (dynarray.cpp)
// This file implements the DynArray class member functions
//******************************************************************
#include dynarray.h
#include <iostream.h>
#include <stddef.h> // For NULL
#include <stdlib.h> // For exit()
// Private members of class:
// int* arr; Pointer to array on free store
// int size; Size of array
//******************************************************************
DynArray::DynArray( /* in */ int arrSize )
// Constructor |
|
|
|
|
|