Creating a Vector Class
Feb 12, 2012 at 9:34pm UTC
So for my assignment I have to create the Vector class and apply it to a main program to include the "Vector.h" file and no include the original Vector class. I have a few of the member functions done but am not sure how to begin with the other ones. If anyone can help me get going it would be greatly appreciated. Thank you.
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
#ifndef VECTOR_H
#define VECTOR_H
#include <vector>
// Vector.h
using namespace std;
template <class T>
class Vector
{
public :
typedef T * iterator;
Vector();
Vector(unsigned int size);
Vector(unsigned int size, const T & initial);
Vector(const Vector<T> & v); // copy constructor
~Vector();
unsigned int capacity() const ; // return capacity of vector (in elements)
unsigned int size() const ; // return the number of elements in the vector
bool empty() const ;
iterator begin(); // return an iterator pointing to the first element
iterator end(); // return an iterator pointing to one past the last element
T & front(); // return a reference to the first element
T & back(); // return a reference to the last element
void push_back(const T & value); // add a new element
void pop_back(); // remove the last element
unsigned int size() const ; // return the number of elements in the vector
bool empty() const ;
iterator begin(); // return an iterator pointing to the first element
iterator end(); // return an iterator pointing to one past the last element
T & front(); // return a reference to the first element
T & back(); // return a reference to the last element
void push_back(const T & value); // add a new element
void pop_back(); // remove the last element
void reserve(unsigned int capacity); // adjust capacity
void resize(unsigned int size); // adjust size
T & operator [](unsigned int index); // return reference to numbered element
Vector<T> & operator =(const Vector<T> &);
private :
unsigned int my_size;
unsigned int my_capacity;
T * buffer;
};
template <class T>
Vector <T>::Vector()
{
my_size = 0;
my_capacity= 0;
buffer = 0;
}
template <class T>
Vector<T>::Vector(unsigned int size)
{
my_size = my_capacity = size;
buffer = new T[size];
for (int i=0; i=size; i++)
buffer[i] = T;
}
template <class T>
Vector<T>::Vector(unsigned int size, const T & initial);
{
}
template <class T>
Vector<T>::Vector(const Vector<T> & v
{
}
template <class T>
Vector<T>::~Vector()
{
if (buffer != NULL)
delete []buffer;
}
template <class T>
unsigned Vector<T>::int capacity() const
{
}
template <class T>
unsigned Vector<T>:: int size() const
{
}
template <class T>
bool Vector<T>::empty() const
{
}
template <class T>
Vector<T>::iterator begin()
{
}
template <class T>
Vector<T>::iterator end()
{
}
template <class T>
Vector<T>::T & front()
{
}
template <class T>
Vector<T>::T & back()
{
}
template <class T>
void Vector<T>::push_back(const T & value)
{
}
template <class T>
void Vector<T>::pop_back()
{
}
template <class T>
void Vector<T>::reserve(unsigned int capacity)
{
}
template <class T>
void Vector<T>::resize(unsigned int size)
{
}
template <class T>
Vector<T>::T & operator [](unsigned int index)
{
}
template <class T>
Vector <T> & Vector<T>::operator =(const Vector<T> &);
{
my_size = v.my_size;
my_capacity = v.my_capacity;
}
template <class T>
Vector <T> & Vector<T>::operator =(const Vector<T> &);
{
my_size = v.my_size;
my_capacity = v.my_capacity;
if (buffer != NULL)
delete []buffer;
buffer = new T[my_capacity];
for (int i = 0; i < my_size; i++)
buffer [i] = v.buffer;
return *this ;
}
#endif
Last edited on Feb 12, 2012 at 9:41pm UTC
Feb 12, 2012 at 10:15pm UTC
If you must define your own class Vector, why you include <vector> ?
Feb 13, 2012 at 5:52am UTC
I figured it out. Thank you.
Topic archived. No new replies allowed.