C++ Notes: OOP: Classes: Example: floatVector - Version 1

Fixed array sizes and Vectors

The fixed size of arrays is often a difficult programming constraint and frequently the source of bugs. The STL (Standard Template Library) vector class is a dynamically expandable array. The floatVector example here shows how vector is implemented, but without the distraction of templates and iterators of the STL vector class. The code is divided into three files, one for testing, one containing the header file, and one which has the implementation of the methods. This first version is a very small subset of the features of the actual vector class.

Enhancements

Many improvements can be made to this class.

  1. Additional functions. Add the following functions that are defined in the vector class, but not yet in floatVector:
    bool   empty() const;          //True if no elements, size()==0.
    float& front() const;          //Returns reference to first element
    void   pop_back();             //Remove last element.
    They should all do the same thing as the corresponding STL: vector<T> template class functions. Modify the test program to test them.
  2. Exceptions. The at function in the vector class throws the out_of_range exception if the subscript is out of range. Change at to do that, but also do the same for subscription and throw an exception in the constructor if the size isn't greater than zero. See Exceptions.
  3. Operator overloading - Write the subscription operator and equality comparison operators. Subscription should do the same thing as the at() function. The equality operator (==) should first compare the sizes. If the sizes are unequal, return false. If the sizes are equal, it's necessary to loop thru the data. If any corresponding elements are unequal, return false.
    float& operator[](int position) const; 
    bool   operator==(const floatVector& v2) const;
  4. Assignment, copy constructor, destructor. Because this class dynamically allocates memory, it's necessary to define assignment (operator=), a copy constructor, and a destructor. See Overloading assignment, Copy constructor, and Destructors.
    floatVector& operator=(const floatVector& fv);
    floatVector(const floatVector& fv); // Copy constructor.
    ~floatVector();                     // Destructor.
  5. Friend function. Define the output operator << by overloading operator<< as a friend function. See Overloading << and >>

    Output format: vector output should produce a left brace, a list of values separated by comma followed by space, and terminated by a right brace. This is like the notation for array initialization. For example
        {1.1, 7.3345, 3.14}
    friend ostream& operator<<(ostream& os, const floatVector& fv);
  6. Template class. Make this into a general template class that will work with any type. Call it xvector to distinguish it from the standard vector class. Use the test program Test Program for xvector.

Header file and test program for first five problems

You may find the following useful for testing solutions to the first 4 problems too, but you will have to comment out the tests and prototypes that you haven't yet implemented..