CInt_Vector
Define a class CInt_Vector, whose size can be dyanmically adjusted.
- It has a member function init(int n=10), which will allocate an
array of n integers from the free storage. If you omit the parameter n
when you invoke the init() function, by default it will allocate 40
bytes.
- You may call a member function resize(int k) to adjust the size of
this array. Imagine that if k is great than the original size n, you have to
allocate a new space with 4k bytes, copy the current elements of the
array to the new location, and release the old space allocated for the
previous smaller array.
- This class has a data member m_pData, which points to the starting
location of the array.
- It also has a data member m_Size, which stores
the size of the array. Because m_Size is a private member, you can only
retrieve its value via a public member function length().
- You can access the element in this array via a function at().
For example, "myVector.at(0) = 3; cout << myVector.at(1);".
- Design a member function Print() which will print out the elements
in this array. The output may look like this: "[ 1 3 5 7 ]".
- (Bonus) Try to overload the "<<" operator so that you can
"cout << myVector;" to print out the array.
- Please consider what should be done in the constructor. Will it
call init() automatically?
- What will happen if the programmer calls init() twice?
How would you prevent this?
- You may also need to define a copy constructor.
- What should be done in the destructor?
- Write a short main() to test all functions in this class.