# C++ Notes: Classes: Example: floatVector.h

The header file for the `floatVector` class. Note that the header defines more functions than are currently implemented in the `floatVector.cpp` file. The unimplemented functions are left for an exercise. See Example: floatVector for links to the test and implementation files.

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
    // floatVector.h - Illustrates typical vector features.
    //  Fred Swartz - 2001-11-13 ... 2004-02-17
    #include <iostream>
    using namespace std;

    #ifndef floatVector_H
    #define floatVector_H

    class floatVector {
        friend ostream& operator<<(ostream& os, const floatVector& fv);

      public:
        floatVector();        // default constructor
        floatVector(int cap); // constructor specifying capacity
        floatVector(const floatVector& fv); // copy constructor
        ~floatVector();       // destructor

        float& at(int position) const; // ref to element at position
        float& back() const;           // Value of last elem
        int    capacity() const;       // Max size before reallocation.
        void   clear();                // Delete all items.
        bool   empty() const;          // True if no elements, size()==0.
        void   erase(int position);    // Delete item at position.
        void   insert(int position, float item); // Insert item at position.
        float& front() const;          // Returns first element.
        void   pop_back();             // Remove last element.
        void   push_back(float item);  // Add item to end.
        void   reserve(int cap);       // Insure capacity at least cap.
        int    size() const;           // Number of elements in vector.

        //-- Overloaded operators
        float& operator[](int position) const; // Ref to element at position.
        bool   operator==(floatVector& v) const; // true if vectors equal.
        floatVector& operator=(const floatVector& fv);

        //-- Forward iterator (no reverse iterator - yet)
        typedef float* iterator;  // The iterators are simply pointers.
        iterator begin();         // Iterator of first element.
        iterator end();           // Iterator PAST last element.

      private:
        int    _capacity;   // Current maximum size (number of floats).
        int    _size;       // Current "size"
        float* _data;       // Pointer to array of floats

        void   _expand();   // Allocate more memory.
        void   _ensure();   // Guarantee at least one empty slot.
    };
    #endif