CRational_Vector
Define a class CRational_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 80
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 8k 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().
- Design a member function Print() which will print out the elements
in this array. The output may look like this: "[ 1/2 1/3 1/5 1/7 ]".
- (Bonus) Try to overload the "<<" operator so that you can
"cout << myVector;" to print out the array.
- You can access the element in this array via a function at().
For example, "cout << myVector.at(1);".
- Overload the assignment (=) operator so that you can assign a
rational number to myVector.at(0), for example.
- 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.