# C++: Classes: Example: floatVector.cpp

This program implements some typical vector methods for Example: floatVector.

      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
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
    // floatVector.cpp  implements some of the floatVector methods
    //  Fred Swartz 2001-11-14, 2002-09-11

    #include "floatVector.h"

    //====================================================== constructor
    floatVector::floatVector() {
        _capacity = 0;
        _size = 0;
        _data = 0;
    }//end constructor

    //====================================================== constructor
    floatVector::floatVector(int cap) {
        _capacity = cap;
        _size = 0;
        _data = new float[_capacity];
    }//end constructor

    //======================================================= destructor
        // free space allocated for the array
    floatVector::~floatVector() {
        delete [] _data;
    }//end destructor

    //=============================================================== at
    float& floatVector::at(int position) const {
        return _data[position];
    }//end capacity

    //=============================================================== []
    float& floatVector::operator[](int position) const {
        return _data[position];
    }//end capacity

    //========================================================= capacity
    int floatVector::capacity() const {
        return _capacity;
    }//end capacity

    //======================================================== push_back
        // Add item to end of the vector
    void floatVector::push_back(float item) {
        if (_capacity <= _size) {
            _expand();
        }
        _data[_size] = item;
        _size++;
    }//end push_back

    //============================================================= size
    int floatVector::size() const {
        return _size;
    }//end size

    //========================================================== reserve
        // start at 10, then double capacity each time
    void floatVector::reserve(int cap) {
        if (cap > _capacity) {
            // This requires an expansion.
            float *new_data = new float[cap]; // allo new array
            if (_capacity > 0) {
                // There is a previous allocation to copy and delete
                for (int i=0; i<_size; i++) {
                     new_data[i] = _data[i];
                }
                delete [] _data;   // deallocate the old array
            }
            _capacity = cap;   // update to new capacity
            _data = new_data;  // point to new array
        }
    }//end reserve

    //========================================================== _expand
        // start at 10, then double capacity each time
    void floatVector::_expand() {
        reserve(_capacity>0 ? 2*_capacity : 10);
    }//end _expand