Hi i am having trouble writing this function:
int& ArrayVector::operator[](int index)
It must do the following
Return reference to index-th element of the array
// If index is greater or equal to size of the array,
// 1. expand the array to cover up to (index+1)
// elements
// 2. copy elements from old array to expanded array
// 3. fill 0 for elements from n-th to (index-1)-th
// 4. return the index-th element
// Also keep track the size of the array
Here is my class so far. I only need help with this function. Thanks!!
class ArrayVector {
private:
int* data;
int n;
public:
ArrayVector() : data(NULL), n(0) { }
int size() const {
return n;
}
// print content of the array separated by “ “
void print() {
if (n == 0)
return;
// print from first element to second last element
for (int i = 0; i < n-1; ++i)
cout << data[i] << " ";
// print last element
cout << data[n-1] << endl;
}
// Return reference to index-th element of the array
// If index is greater or equal to size of the array,
// 1. expand the array to cover up to (index+1)
// elements
// 2. copy elements from old array to expanded array
// 3. fill 0 for elements from n-th to (index-1)-th
// 4. return the index-th element
// Also keep track the size of the array
int& operator[](constint index);
};
int& ArrayVector::operator[](int index) {