Okay Scratch all that, I've made some major progress. Take a look at this.
class IntArray
{
private:
int arrayLow, arrayHigh; //low & high index of array
char name; //name of object
int *array; //pointer to beggining of array
public:
IntArray(); //Constructor - create array index 0-9
IntArray(int); //Constructor for arrays starting at 0
IntArray(int, int); //Constructor for arrays not starting at 0
IntArray(IntArray*); //Constructor for making copy of another IntArray object
~IntArray(); //Default Deconstructor
int& IntArray:
perator[](int); //Overload [] for Assigning Values
int low() {return arrayLow;} //Returns lowest index of array
int high() {return arrayHigh;} //Returns highest index of array
void setName(char myName) {name=myName;} //Sets name of object
};
#include "intarray.h"
IntArray::IntArray()
{
arrayLow=0;
arrayHigh=9;
array=new int[arrayHigh-arrayLow];
}
IntArray::IntArray(int size)
{
arrayLow=0; //array starts at 0
arrayHigh=size-1; //highest index of array based on given size
array=new int[size]; //create array memory locations
}
IntArray::IntArray(int low, int high)
{
arrayLow=low; //start of array index
arrayHigh=high; //end of array index
if(arrayLow==arrayHigh)
array=new int[1]; //if high and low are equal create an array 1 unit long
else
array=new int[arrayHigh-arrayLow]; //create array memory locations
}
IntArray::IntArray(IntArray* copyThis)
{
arrayLow=copyThis->low();
arrayHigh=copyThis->high();
if(arrayLow==arrayHigh)
array=new int[1]; //if high and low are equal create an array 1 unit long
else
array=new int[arrayHigh-arrayLow]; //create array memory locations
for(int i=arrayLow;i<=arrayHigh;i++)
array
=copyThis->array;
}
IntArray::~IntArray()
{
//for(int i=arrayLow;i<=arrayHigh;i++)
// delete [] array;
//delete [] array;
}
int& IntArray:perator[] (int forLocation)
{
return array[arrayLow+forLocation];
}
Also some quick requirments:
IntArray a(10), w(10); // Ten elements, indexed 0 to 9
IntArray b(-3, 6); // Ten elements, indexed -3 to 6
IntArray c(6, 8); // Three elements, indexed 6 to 8
IntArray d(5, 5); // Single element array, indexed at 5
IntArray z; // Ten elements, indexed 0 to 9
Okay here's my class I'm working on right now, plus all the info I
think is needed to see what I'm trying to do. Basically I need the
following three lines of code to run
IntArray a(10);
for(int i=a.low();i<=a.high();i++)
a
=i*10;
I'm trying to overload the [] operator. The function ARRAYSTRUCT
IntArray:perator[] (int forLocation) is where things are starting to
fall apart I believe.
//My Class and such
typedef struct { //Array structure
int location;
int value;
} ARRAYSTRUCT;
class IntArray
{
private:
int arrayLow, arrayHigh; //low & high index of array
char name; //name of object
ARRAYSTRUCT *array; //pointer to beggining of array
public:
IntArray(int); //Constructor for arrays starting at 0
// IntArray(int, int); //Constructor for arrays not starting at 0
ARRAYSTRUCT IntArray:perator[](int); //Overload [] - Pass location,
assign value
int low() {return arrayLow;} //Returns lowest index of array
int high() {return arrayHigh;} //Returns highest index of array
void setName(char myName) {name=myName;} //Sets name of object
};
IntArray::IntArray(int size)
{
arrayLow=0; //array starts at 0
arrayHigh=size-1; //highest index of array based on given size
array=new ARRAYSTRUCT[size]; //create array memory locations
for(int i=arrayLow;i<=arrayHigh;i++)
{
array->location=i; //Sets index of array
array->value=NULL; //Sets value of array to NULL
}
}
ARRAYSTRUCT IntArray:perator[] (int forLocation)
{
ARRAYSTRUCT *myLocation=array;
for(int i=0;i<(arrayLow+forLocation);i++) //get myLocation to
correct spot
myLocation++;
return *myLocation;
}
John Harrison said:
- Steve - said:
For a school assignment I need to write a class to work with the
following code. The IntArray b(-3, 6) basically means that I need to
produce an array of integer values that has an index going from -3 to
6. I'm completely lost on how I should create that array. Any shoves
in the right direction would be appreciated.
void test2()
{
system("cls");
cout << "2. Array declared with two integers: IntArray b(-3, 6);" <<
endl << endl;
csis << "2. Array declared with two integers: IntArray b(-3, 6);" <<
endl << endl;
IntArray b(-3, 6);
for(int i = b.low(); i <= b.high(); i++)
b = i * 10;
b.setName('b');
cout << b << endl;
csis << b << endl;
wait();
}
The difficulty in answering this question is that you've given no clue as to
your level of ability. Do you know how to create a class for instance?
If yes then have a go and post the resulting code, it doesn't matter if bits
are missing, it doesn't even matter if it doesn't compile. When we see how
good (or bad!) your coding is we'll be able to give appropriate advice.
If you don't know how to write a class then you've got a lot of catching up
to do.
john