Assignment 4: Implementing a vector class (individual)
 
Assigned: 09/26
Due: 10/6 at 11:59 p.m.

Libraries You May Use:
Note: Nowhere in your code should you include a standard container like vector.

Description:

You are to implement a simple version of the vector class as described in class named Vector (to avoid name clashes with std::vector). Your vector class should provide simple access to the vector's contents (it will be a vector of integers), and your vector should grow (from the end) as needed.

What you must implement:

You will be designing and implementing a new class called "Vector" in the files "Vector.cpp" and "Vector.h".

In your Vector class you will need to implement the following (minimum) functionality from std::vector:
 private:
  int *vec_ptr; //Points to the vector's dynamic array
  int vec_size, vec_capacity; //Same as size and capacity for std::vector

 public:
  Vector();
  /* Description:
   *    Default constructor.  Initializes an empty vector.
   *
   * Post-conditions:
   *    vec_ptr is NULL, vec_size=0, and vec_capacity=0.
   */

   Vector(const Vector &other);
  /* Description:
   *    Copy constructor.  Initializes a dynamic array of the appropriate
   *    size/capacity and copies data from other's array to the new array.
   *
   * Parameters:
   *    other - a constant reference to the vector that is to be copied.
   *
   * Post-conditions:
   *    vec_ptr is initialized, and its array contains other.vec_ptr's data,
   *    vec_size=other.vec_size, and vec_capacity=other.vec_capacity.
   */

  ~Vector();
  /* Description:
   *    Destructor.  Deallocates vec_ptr.
   *
   * Postconditions:
   *    vec_ptr has been deallocated, current_size=0, and capacity=0.
   */
 
   Vector& operator=(const Vector &other);
  /* Description:
   *    Assignment operator.  Initializes a dynamic array of the appropriate
   *    size/capacity and copies data from other's array to the new array.
   *
   * Parameters:
   *    other - a constant reference to the vector that is to be copied.
   *
   * Post-conditions:
   *    vec_ptr is initialized, and its array contains other.vec_ptr's data,
   *    vec_size=other.vec_size, and vec_capacity=other.vec_capacity.
   */
 
  int size();
  /* Description:
   *    Returns the current number of elements in the vector.
   *
   * Return:
   *    Current size of vector.
   */

   int capacity();
  /* Description:
   *    Returns the currently allocated storage capacity of the vector.
   *
   * Return:
   *    Current capacity of vector.
   */

  void push_back(int element);
  /* Description:
   *    Similar to std::vector.push_back(int element), stores element at the end of the vector.
   *
   * Parameters:
   *    element - The item to be inserted at the back of the vector.
   *
   * Postconditions:
   *    Item element has is stored at the end of the vector.
   *
   * Notes:
   *    If necessary, calls reserve() before adding element to the end of the vector.
   *    This reserve doubles the current capacity of the vector.
   */

  void reserve(int n);
  /* Description:
   *    Requests that the vector capacity be resized at least enough to contain n elements.
   *
   *    If n is greater than the current capacity, the function causes the container to reallocate
   *    its storage increasing its capacity to n (or greater).
   *
   *    If n is less than or equal to the current capacity, the call does nothing (no reallocation
   *    or change in vector capacity).
   *
   *    This function has no effect on vector size and cannot alter its elements.
   *
   * Postconditions:
   *    Vector capacity is now n.  Vector size (and contents) remains the same.
   *
   */

 int& operator[](unsigned int index); 
  /* Description:
   *    Returns a reference to the element at position index.
   */

ĦĦĦBonus!!!
Be sure to save your work ahead of time if you wish to attempt the bonus. Your bonus is to augment your Vector class to be a templated (generic) Vector:

 template <typename T> class Vector {
 };
Please include enough test cases in your program to demonstrate that the template works (both numeric and non-numeric types).

Recall from CSCI 222, that templating your class will most likely mean putting both the class definition and the method implementations in your .h file. If you choose to go this route, please disregard the requirement below that you turn in a Vector.cpp. Note: This is the only circumstance where it is acceptable to turn in just one file for your declaration/implementation.

Compiling:

Remember, on Linux you will compile each file to a .o file separately with the -c flag:

g++ -c main.cpp
g++ -c Vector.cpp
and link the object files to create your final executable:
g++ main.o Vector.o -o test
where test is the name of your output executable.

Remember, on Linux you will compile each file to a .o file separately:

Testing:

I expect you to come up with your own test cases in the main(). Your tests should demonstrate that all functionality works including:

  1. Demonstrating the use of push_back by using a loop to add to your vector (should easily be able to accommodate pushing 1000+ integers).
  2. Use the brackets operator[] to initialize/modify/access the elements of your vector.
  3. Use size() to control the bounds of a loop (for printing contents or some other feature).
  4. Use of your assignment operator
  5. Use of your copy constructor
Note: Please make sure to document in your main where you intend to test each of the above features.

Documentation:

In general I will require that your programs are well documented, including at minimum:

What you must turn in:

Electronically a single zip file, with the name LASTNAME_FIRSTNAME.zip. Your zip file should extract into a directory with the same name as the zip (please make sure to put your files in a separate directory before submission). Your zip should include all files necessary to compile and run your program including:

Note: Your program must compile and run on CS1 (or a UD lab machine) or it will not be graded. Please see the syllabus for details.