/ Published in: C++
Expand |
Embed | Plain Text
/*################################################ # simple vector modification # # modfy the simplevector class template presented in this chapter to incluede te mmber functions pus_back and pop_back. These functions should emulate the STL vector class member functions of the same name(see table 16-5) the push_back function should acept an argument and insert its vaqlue at the end of the array. The pop_back function should accept no argument and remove the last element from the array test the class with a driver program */ #include <iostream> #include <new> #include <cstdlib> using namespace std; /*######################################################## # This is same concept as vector but not to use vector # ######################################################*/ template <class T> class SimpleVector { private: T *aptr; int arraySize; void memError(); void subError(); int intArray[]; // array for int double doubleArray[]; // array for double public: SimpleVector() { aptr = 0; arraySize = 0; } SimpleVector(int); SimpleVector(const SimpleVector &); ~SimpleVector(); int size() const { return arraySize;} T getElementAt(int position); T &operator[](const int &); }; /*############################################################ # Constractor for simpleVector class. Sets the size of the # # array and allocates memory for it # # put data into the moc vector # ##########################################################*/ // here is the base of the array template <class T> SimpleVector<T>::SimpleVector(int s) { arraySize = s; try { aptr =new T [s]; } catch (bad_alloc) { memError(); } for(int count = 0;count < arraySize; count++) *(aptr + count) = 0; } /*##################################################### # copy constructor for SimpleVector class # # put data into moc vector # ###################################################*/ template <class T> SimpleVector<T>::SimpleVector(const SimpleVector &obj) { arraySize = obj.arraySize; aptr = new T [arraySize]; if(aptr == 0) memError(); for(int count =0; count < arraySize;count++) *(aptr + count) = *(obj.aptr + count); } /*############################################################ # Distructor for SimpleVector # # decide the size of array in a moc vector # ##########################################################*/ template <class T> SimpleVector<T>::~SimpleVector() { if(arraySize > 0) delete [] aptr; } /*###################################################### # memError function.Displays an error message and # # terminates the program when allocation fails # # catch contents # ####################################################*/ template <class T> void SimpleVector<T>::memError() { cout << "Error:Cannot allocate memory"<<endl; exit(EXIT_FAILURE); } /*########################################################## # subError function.Displays an error message and # # terminates the program when a subscript is out of range# # catch contents # ########################################################*/ template <class T> void SimpleVector<T>::subError() { cout << "Error: Subscript out of range."<<endl; exit(EXIT_FAILURE); } /*################################################### # getElementAt function. the argument is a # # subscript. This function returns the value # # stored at the subcript in the array # # get element into moc vector # ##################################################*/ template <class T> T SimpleVector<T>::getElementAt(int sub) { if(sub < 0 || sub >= arraySize) subError(); return aptr[sub]; } /*####################################################### # Overloaded [] operator. the argument is a subscript.# # This function returns a reference to the element # # in the array indexed by the subscript # # put data into array # #####################################################*/ template <class T> T &SimpleVector<T>::operator[](const int &sub) { if (sub < 0 || sub >= arraySize) subError(); return aptr[sub]; } /*#################################################### # push_back here has some kind of error # ##################################################*/ //need to find where the array is template <class T> T push_back_int(int temp,int count,SimpleVector<int> &intTable) { intTable[count] = temp; } template <class T> T push_back_double(double temp,int count,SimpleVector<double> &doubleTable) { doubleTable[count] = temp; } /*#################################################### # pop_back # ##################################################*/ template <class T> T pop_back_int(int temp,int lastCount,SimpleVector<int> &intTable) { intTable[lastCount-1] = NULL; } template <class T> T pop_back_double(double temp,int lastCount,SimpleVector<double> &doubleTable) { doubleTable[lastCount-1] = NULL; } int main() { const int SIZE = 10; int count,valueA,valueB; SimpleVector<int> intTable(SIZE); SimpleVector<double> doubleTable(SIZE); // push_back for(count = 0; count < SIZE; count++) { valueA = (count * 2); push_back_int<int> (valueA,count,intTable); valueB = (count * 2.14); push_back_double<double> (valueB,count,doubleTable); } cout << "These value in intTable : "<<endl; for( count = 0; count < SIZE; count++) cout << intTable[count] << " "; cout << endl; cout << "These value are in doubleTable : "<<endl; for(count = 0; count < SIZE; count ++) cout << doubleTable[count] << " "; cout << endl; //push back pop_back_int<int> (valueA,count,intTable); pop_back_double<double> (valueB,count,doubleTable); valueA = 0; valueB = 0; cout << "These value in intTable (after pop) : "<<endl; for ( count = 0; count < SIZE; count++) cout << intTable[count] << " "; cout << endl; cout << "These value are in doubleTable (after pop) : "<<endl; for(count = 0; count < SIZE; count ++) cout << doubleTable[count] << " "; cout << endl; cout << "\nAdding 5 to each element of intTable and doubleTable"<<endl; valueA = 0; valueB = 0; for(count = 0; count < SIZE; count++) { valueA = intTable[count] + 5; push_back_int<int> (valueA,count,intTable); valueB = doubleTable[count] + 5.0; push_back_double<double> (valueB,count,doubleTable); } cout << "These value in intTable (after pop) : "<<endl; for ( count = 0; count < SIZE; count++) cout << intTable[count] << " "; cout << endl; cout << "These value are in doubleTasble : "<<endl; for (count = 0; count < SIZE; count++) cout << doubleTable[count] << " "; cout << endl; cout << "\nIncrementing each element of intTable and doubleTable"<<endl; valueA = 0; valueB = 0; for(count = 0; count < SIZE; count++) { valueA += intTable[count]; push_back_int<int> (valueA,count,intTable); valueB += doubleTable[count]; push_back_double<double> (valueB,count,doubleTable); } cout << "These values are in intTable : "<<endl; for(count = 0; count< SIZE; count++) cout << intTable[count]<<" "; cout << endl; cout << "These values are in doubleTable : "<< endl; for(count = 0; count < SIZE; count++) cout << doubleTable[count] << " "; cout << endl; //pop_back return 0; }
Comments
Subscribe to comments
You need to login to post a comment.

I'm so confused! what does this do exactly? hehe