Yes i was given the problem to write a program using the Vecotr class and the following is what i have to implement into the program as member functions:
Overload >> cin
overload << cout
SortV- can use any sorting technique bubble selection or quick doesn't matter
ReverseV-to reverse the numbers in the vector
RemoveDup- to remove any duplicate elements if any.
any help would be appraited.
the header file is tvector.h that i have to use it is as follows
any help would be great thanks guysCode:const int DEFAULT_VECTOR_SIZE = 50; template <class T> class Vector { private: // dynamic list containing arraySize elements of type T T *arr; int arraySize; // create general error message and terminate program void error(const String& msg) const; public: // constructors and destructor // create uninitialized array with arrsize elements Vector(int arrsize = DEFAULT_VECTOR_SIZE); // all arrsize elements initialized to value Vector(int arrsize, const T& value); // initialize with C++ array Vector(T a[], int arrsize); // copy constructor Vector(const Vector<T>& rhs); // destructor. reasons for virtual discussed in Ch. 12 virtual ~Vector(); // overloaded assignment operator Vector<T>& operator= (const Vector<T>& rhs); // index operators T& operator[](int n); // version for constant objects const T& operator[](int i) const; // convert to a C++ array T *c_arr() const; // access and update operations int size() const; // return size void resize(int sz); // modify the size void clear(); // clear the list }; // *********************************************************** // Vector class implementation // *********************************************************** // private function to print an error message and // terminate the program template <class T> void Vector<T>::error(const String& errmsg) const { cerr << errmsg << endl; exit(1); } //constructor template <class T> Vector<T>::Vector(int arrsize): arraySize(arrsize) { if (arraySize < 0) error("Negative Vector size is invalid."); else if (arraySize == 0) { // 0 size array. assign arr to NULL and return arr = NULL; return; } // dynamically create Vector arr = new T[arraySize]; // make sure the allocation succeeded if (arr == NULL) error("Memory allocation failure."); } // constructor. initialize with n element array a template <class T> Vector<T>::Vector(T a[], int arrsize): arraySize(arrsize) { int i; if (arraySize < 0) error("Negative Vector size is invalid."); else if (arraySize == 0) { // 0 size array. assign arr to NULL and return arr = NULL; return; } // dynamically create array arr = new T[arraySize]; // make sure the allocation succeeded if (arr == NULL) error("Memory allocation failure."); // copy array to arr for(i=0; i < arraySize; i++) arr[i] = a[i]; } // constructor. initialize with arrsize copies of value template <class T> Vector<T>::Vector(int arrsize, const T& value): arraySize(arrsize) { int i; if (arraySize < 0) error("Negative Vector size is invalid."); else if (arraySize == 0) { // 0 size array. assign arr to NULL and return arr = NULL; return; } // dynamically create array arr = new T[arraySize]; // make sure the allocation succeeded if (arr == NULL) error("Memory allocation failure."); // each entry of arr has given value for(i=0; i < arraySize; i++) arr[i] = value; } // copy constructor template <class T> Vector<T>::Vector(const Vector<T>& obj) { int i; // copy across fixed data member arraySize arraySize = obj.arraySize; if (arraySize == 0) { // object as 0 size arr = NULL; return; } // allocate new dynamic memory for the array arr = new T[arraySize]; // make sure the allocation succeeded if (arr == NULL) error("Memory allocation failure."); // copy items from obj.arr to the newly allocated array for (i = 0; i < arraySize; i++) arr[i] = obj.arr[i]; } // destructor template <class T> Vector<T>::~Vector() { if (arr != NULL) delete [] arr; } // overloaded assignment template <class T> Vector<T>& Vector<T>::operator= (const Vector<T>& rhs) { int i; // can't assign a Vector object to itself. just return if (this == &rhs) return *this; // if rhs is a 0 size array, handle as a special case if (rhs.arraySize == 0) { if (arr != NULL) delete [] arr; arraySize = 0; arr = NULL; return *this; } // check size to see if arr in current object can be used if (arraySize != rhs.arraySize) { // de-allocate existing memory and allocate new memory delete [] arr; arr = new T[rhs.arraySize]; if (arr == NULL) // vefify allocation succeeded error("Memory allocation failure."); } // copy fixed data member arraySize arraySize = rhs.arraySize; // copy items from obj.arr to current object's array for (i = 0; i < arraySize; i++) arr[i] = rhs.arr[i]; // return reference to the current object return *this; } // indexing operators // non-constant version. provides general access to array // elements template <class T> T& Vector<T>::operator[](int i) { if (i < 0 || i > arraySize-1) { cerr << i << ": "; error("Index out of bounds"); } return arr[i]; } // constant version. can be used with a constant object. // does not allow modification of an array element template <class T> const T& Vector<T>::operator[](int i) const { if (i < 0 || i > arraySize-1) { cerr << i << ": "; error("Index out of bounds"); } return arr[i]; } // convert to a C++ array template <class T> T *Vector<T>::c_arr() const { return arr; } // return the number of elements in the array template <class T> int Vector<T>::size() const { return arraySize; } // modify the size of the array template <class T> void Vector<T>::resize(int sz) { int i, n; // handle case of negative size with error message if (sz < 0) error("Negative new size is invalid."); // handle case of no size change with a return if (sz == arraySize) return; // handle the case of a resize to size 0 if (sz == 0) { delete [] arr; arr = NULL; arraySize = 0; return; } // allocate new list with sz elements T* newlist = new T[sz]; // make sure the allocation succeeded if (newlist == NULL) error("Memory allocation failure."); // set n to be smaller of new size and current size if (sz <= arraySize) n = sz; else n = arraySize; // copy n elements from old list to new list // this may truncate the list for (i = 0; i < n; i++) newlist[i] = arr[i]; // delete original list, set arr to point to newlist, // update arraySize delete[] arr; arr = newlist; arraySize = sz; } template <class T> void Vector<T>::clear() { resize(0); }
achilles



LinkBack URL
About LinkBacks



Just kidding. I think you mean something like