No problem. I hope this helps someone.
Code:
/////////////// test.cpp ///////////////
/// Use dVars.h and see if it works. ///
////////////////////////////////////////
#include <iostream.h>
#include "dVars.h"
int main( void )
{
dInt num( 10 );
num.setItem( 5, 10 );
for(int x=0; x<10; x++)
cout << num.getItem(x) << ' ';
cout << endl;
num.resize( 15 );
num.setItem( 12, 42 );
for(x=0; x<15; x++)
cout << num.getItem(x) << ' ';
cout << endl;
return 0;
}
Code:
/////////////// dVars.cpp //////////////
// The header for a resizable array. ///
////////////////////////////////////////
#ifndef dVars_h
#define dVars_h
class dInt
{
public:
dInt( int arraySize ); // Constructor
~dInt( void ); // Deconstructor
void resize( unsigned int newSize ); // Returns true if the resize is successful.
int getItem( unsigned int itemNumber ); // Returns the value of the item number specified.
bool setItem( unsigned int itemNumber, int itemValue ); // Sets the value
private:
unsigned int size; // Hold the size of the vector.
int *array; // A pointer to the array.
int *temp; // A holder for the data when the array is resized.
};
#endif
And finally...
Code:
/////////////// dVars.cpp ///////////////
#include <iostream.h>
#include <stdlib.h>
#include "dVars.h"
dInt::dInt( int arraySize )
{
if(arraySize > 0)
{
size = arraySize;
array = new int[ size ];
for(int x=0; x<size; x++)
array[x] = 0;
}
}
dInt::~dInt( void )
{
// TODO: Fix this.
// delete array;
// delete temp;
}
int dInt::getItem( unsigned int itemNumber )
{
if(itemNumber <= size)
return array[ itemNumber ];
return 0;
}
bool dInt::setItem( unsigned int itemNumber, int itemValue )
{
if(itemNumber <= size)
{
array[itemNumber] = itemValue;
return 1;
}
else
{
return 0;
}
}
void dInt::resize( unsigned int newSize )
{
temp = new int[ size ];
for(unsigned int x=0; x < size; x++)
{
temp[x] = 0;
temp[x] = array[x];
}
array = new int[ newSize ];
for(x=0; x <= newSize; x++)
array[x] = 0;
for(x=0; x < size; x++)
{
array[x] = temp[x];
}
temp = new int[0];
size = newSize;
return;
}
I'm guessing that I could fine tune this quite a bit.
I didn't do the clean up right, and there isn't enough commenting
in the source. I'm also uncertain as to whether or not I'd even need
to make the temporary array when I resize.
Feedback and constructive criticism are appreciated.