Here's the exact text from the exercise in the book that I am trying to figure out. This should (hopefully) explain better what I am trying to accomplish:
Implement a class genVector that generalizes the vector class to create a safe array with general starting and ending indices. For instance,
Code:
genVector<int> vA(1,10), vB(-1,8);
creates objects vA and vB with index ranges 1 <= i <=10 and -1 <i < 8 , respectively. Objects of type genVector can be indexed within their defined range. For instance,
Code:
int i;
for (i=-1; i<=8; i++) // initalize all vector elements to 0
vB[i]=0;
Derive genVector from the vector class by using public inheritance. Override the index operator so it accepts indices in the correct range. Implement a derived member function resize() that resizes the vector and resets the beginning and ending indices. These actions prevent references to the vector index operator and resize() function unless the programmer uses the scope operator "::".
Code:
template <typename T>
class genVector: public vector<T>
{
public:
genVector(int low, int high);
// vector has high - low+1 elements in range [low,high]
T& operator [] (int i);
// operator verifies that lower <= i <= upper.
// if not, it throws the indexRangeError exception
void resize(int lowIndex, int highIndex);
// resize vector and set range to [lowIndex, highIndex]
private:
int lower;
int upper;
};
Place genVector in header file "genvec.h" and write a program that declares the following objects:
Code:
genVector<char> ucLetter(65,90);
genVector<double> tempVector(-10,25);
Initialize ucLetter so that ucLetter[65]='A',...,ucLetter[90]='Z'. Initialize tempVector so that tempVector[t] is the Fahrenheit equivalent of Celsiius tempature t. Recall that
Display the contents of each vector.
The information listed above is the framework that is provided by the exercise. Hopefully this helps to explain where I'm coming from and why I am approaching things as I am...
I am still trying to figure out the resize() function and the constructor for this, but I think I have the overloaded [] operator pretty close to correct if not exactly right.
Any pointers/assistance would be very much appreciated. I can re-post my code also, if needed.
Thanks for your time!