Posted By

DrPepper on 11/28/10


Tagged


Versions (?)

ch16 template function


 / Published in: C++
 

  1. /*################################################
  2.   # simple vector modification #
  3.   # 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
  4.   test the class with a driver program
  5.   */
  6.  
  7. #include <iostream>
  8. #include <new>
  9. #include <cstdlib>
  10. using namespace std;
  11.  
  12. /*########################################################
  13.   # This is same concept as vector but not to use vector #
  14.   ######################################################*/
  15. template <class T>
  16. class SimpleVector
  17. {
  18. private:
  19. T *aptr;
  20. int arraySize;
  21. void memError();
  22. void subError();
  23. int intArray[]; // array for int
  24. double doubleArray[]; // array for double
  25. public:
  26. SimpleVector()
  27. { aptr = 0; arraySize = 0; }
  28. SimpleVector(int);
  29. SimpleVector(const SimpleVector &);
  30. ~SimpleVector();
  31. int size() const
  32. { return arraySize;}
  33. T getElementAt(int position);
  34. T &operator[](const int &);
  35. };
  36. /*############################################################
  37.   # Constractor for simpleVector class. Sets the size of the #
  38.   # array and allocates memory for it #
  39.   # put data into the moc vector #
  40.   ##########################################################*/
  41. // here is the base of the array
  42. template <class T>
  43. SimpleVector<T>::SimpleVector(int s)
  44. {
  45. arraySize = s;
  46. try
  47. {
  48. aptr =new T [s];
  49. }
  50. catch (bad_alloc)
  51. {
  52. memError();
  53. }
  54. for(int count = 0;count < arraySize; count++)
  55. *(aptr + count) = 0;
  56. }
  57. /*#####################################################
  58.   # copy constructor for SimpleVector class #
  59.   # put data into moc vector #
  60.   ###################################################*/
  61.  
  62. template <class T>
  63. SimpleVector<T>::SimpleVector(const SimpleVector &obj)
  64. {
  65. arraySize = obj.arraySize;
  66. aptr = new T [arraySize];
  67. if(aptr == 0)
  68. memError();
  69. for(int count =0; count < arraySize;count++)
  70. *(aptr + count) = *(obj.aptr + count);
  71. }
  72. /*############################################################
  73.   # Distructor for SimpleVector #
  74.   # decide the size of array in a moc vector #
  75.   ##########################################################*/
  76.  
  77. template <class T>
  78. SimpleVector<T>::~SimpleVector()
  79. {
  80. if(arraySize > 0)
  81. delete [] aptr;
  82. }
  83. /*######################################################
  84.   # memError function.Displays an error message and #
  85.   # terminates the program when allocation fails #
  86.   # catch contents #
  87.   ####################################################*/
  88.  
  89. template <class T>
  90. void SimpleVector<T>::memError()
  91. {
  92. cout << "Error:Cannot allocate memory"<<endl;
  93. exit(EXIT_FAILURE);
  94. }
  95. /*##########################################################
  96.   # subError function.Displays an error message and #
  97.   # terminates the program when a subscript is out of range#
  98.   # catch contents #
  99.   ########################################################*/
  100.  
  101. template <class T>
  102. void SimpleVector<T>::subError()
  103. {
  104. cout << "Error: Subscript out of range."<<endl;
  105. exit(EXIT_FAILURE);
  106. }
  107. /*###################################################
  108.   # getElementAt function. the argument is a #
  109.   # subscript. This function returns the value #
  110.   # stored at the subcript in the array #
  111.   # get element into moc vector #
  112.   ##################################################*/
  113.  
  114. template <class T>
  115. T SimpleVector<T>::getElementAt(int sub)
  116. {
  117. if(sub < 0 || sub >= arraySize)
  118. subError();
  119. return aptr[sub];
  120. }
  121.  
  122. /*#######################################################
  123.   # Overloaded [] operator. the argument is a subscript.#
  124.   # This function returns a reference to the element #
  125.   # in the array indexed by the subscript #
  126.   # put data into array #
  127.   #####################################################*/
  128.  
  129. template <class T>
  130. T &SimpleVector<T>::operator[](const int &sub)
  131. {
  132. if (sub < 0 || sub >= arraySize)
  133. subError();
  134. return aptr[sub];
  135. }
  136. /*####################################################
  137.   # push_back here has some kind of error #
  138.   ##################################################*/
  139. //need to find where the array is
  140.  
  141. template <class T>
  142. T push_back_int(int temp,int count,SimpleVector<int> &intTable)
  143. {
  144. intTable[count] = temp;
  145. }
  146. template <class T>
  147. T push_back_double(double temp,int count,SimpleVector<double> &doubleTable)
  148. {
  149. doubleTable[count] = temp;
  150. }
  151.  
  152. /*####################################################
  153.   # pop_back #
  154.   ##################################################*/
  155.  
  156. template <class T>
  157. T pop_back_int(int temp,int lastCount,SimpleVector<int> &intTable)
  158. {
  159. intTable[lastCount-1] = NULL;
  160. }
  161.  
  162. template <class T>
  163. T pop_back_double(double temp,int lastCount,SimpleVector<double> &doubleTable)
  164. {
  165. doubleTable[lastCount-1] = NULL;
  166. }
  167.  
  168.  
  169. int main()
  170. {
  171. const int SIZE = 10;
  172. int count,valueA,valueB;
  173. SimpleVector<int> intTable(SIZE);
  174. SimpleVector<double> doubleTable(SIZE);
  175. // push_back
  176. for(count = 0; count < SIZE; count++)
  177. {
  178. valueA = (count * 2);
  179. push_back_int<int> (valueA,count,intTable);
  180. valueB = (count * 2.14);
  181. push_back_double<double> (valueB,count,doubleTable);
  182. }
  183. cout << "These value in intTable : "<<endl;
  184. for(
  185. count = 0; count < SIZE; count++)
  186. cout << intTable[count] << " ";
  187. cout << endl;
  188.  
  189. cout << "These value are in doubleTable : "<<endl;
  190. for(count = 0; count < SIZE; count ++)
  191. cout << doubleTable[count] << " ";
  192. cout << endl;
  193.  
  194. //push back
  195. pop_back_int<int> (valueA,count,intTable);
  196. pop_back_double<double> (valueB,count,doubleTable);
  197. valueA = 0;
  198. valueB = 0;
  199. cout << "These value in intTable (after pop) : "<<endl;
  200. for
  201. ( count = 0; count < SIZE; count++)
  202. cout << intTable[count] << " ";
  203. cout << endl;
  204.  
  205. cout << "These value are in doubleTable (after pop) : "<<endl;
  206. for(count = 0; count < SIZE; count ++)
  207. cout << doubleTable[count] << " ";
  208. cout << endl;
  209.  
  210. cout << "\nAdding 5 to each element of intTable and doubleTable"<<endl;
  211. valueA = 0;
  212. valueB = 0;
  213. for(count = 0; count < SIZE; count++)
  214. {
  215. valueA = intTable[count] + 5;
  216. push_back_int<int> (valueA,count,intTable);
  217. valueB = doubleTable[count] + 5.0;
  218. push_back_double<double> (valueB,count,doubleTable);
  219. }
  220. cout << "These value in intTable (after pop) : "<<endl;
  221. for
  222. ( count = 0; count < SIZE; count++)
  223. cout << intTable[count] << " ";
  224. cout << endl;
  225.  
  226. cout << "These value are in doubleTasble : "<<endl;
  227. for (count = 0; count < SIZE; count++)
  228. cout << doubleTable[count] << " ";
  229. cout << endl;
  230. cout << "\nIncrementing each element of intTable and doubleTable"<<endl;
  231. valueA = 0;
  232. valueB = 0;
  233. for(count = 0; count < SIZE; count++)
  234. {
  235.  
  236. valueA += intTable[count];
  237. push_back_int<int> (valueA,count,intTable);
  238. valueB += doubleTable[count];
  239. push_back_double<double> (valueB,count,doubleTable);
  240. }
  241. cout << "These values are in intTable : "<<endl;
  242. for(count = 0; count< SIZE; count++)
  243. cout << intTable[count]<<" ";
  244. cout << endl;
  245. cout << "These values are in doubleTable : "<< endl;
  246. for(count = 0; count < SIZE; count++)
  247. cout << doubleTable[count] << " ";
  248. cout << endl;
  249. //pop_back
  250. return 0;
  251. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: trusktr on November 30, 2010

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

You need to login to post a comment.