Don't like ads? PRO users don't see any ads ;-)
Guest

CS2370 Prog 6

By: mattfong on May 19th, 2011  |  syntax: None  |  size: 3.39 KB  |  hits: 51  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //Matthew Fong
  2. //Assignment 6: template classes
  3. //Description: add functions to a pre written class
  4.  
  5. #ifndef SIMPLEVECTOR_H_INCLUDED
  6. #define SIMPLEVECTOR_H_INCLUDED
  7. #include <iostream>
  8. #include <vector>
  9. using namespace std;
  10.  
  11. template <class T>
  12. class simpleVector
  13. {
  14.     private:
  15.         int front;
  16.         int last;
  17.         T *aPtr;
  18.         int arraySize;
  19.  
  20.     public:
  21.         simpleVector()
  22.         {
  23.             aPtr = 0;
  24.             arraySize = 0;
  25.             front = 0;
  26.         }
  27.  
  28.         simpleVector(int s)
  29.         {
  30.             arraySize = s;
  31.             front = 0;
  32.             last = arraySize - 1;
  33.         }
  34.  
  35.         void set(T *array)
  36.         {
  37.             aPtr = new T[arraySize];
  38.  
  39.             for(int i = 0; i < arraySize; i++)
  40.             {
  41.                 aPtr[i] = array[i];
  42.             }
  43.         }
  44.  
  45.         void print()
  46.         {
  47.             for(int i = 0; i < arraySize; i++)
  48.             {
  49.                 cout << aPtr[i] << " ";
  50.             }
  51.             cout << endl;
  52.         }
  53.  
  54.         void printlast()
  55.         {
  56.             cout << last << endl;
  57.         }
  58.  
  59.         void pushBack(T elem)
  60.         {
  61.             for(int i = 0; i < arraySize; i++)
  62.             {
  63.                 if(aPtr[i] == NULL)
  64.                 {
  65.                     last = i-1;
  66.                 }
  67.             }
  68.             aPtr[last] = elem;
  69.         }
  70.  
  71.         void popBack()
  72.         {
  73.             T *newArray;
  74.             newArray = new T[last];
  75.  
  76.             for(int i = 0; i < last; i++)
  77.             {
  78.                 if(aPtr[i] == aPtr[i+1])
  79.                 {
  80.                     last = i+1;
  81.                 }
  82.             }
  83.  
  84.             for(int i = 0; i < last; i++)
  85.             {
  86.                 newArray[i] = aPtr[i+1];
  87.             }
  88.  
  89.             delete [] aPtr;
  90.  
  91.             aPtr = newArray;
  92.         }
  93.  
  94.         void numElements()
  95.         {
  96.             int count = 0;
  97.             for(int i = 0; i < arraySize; i++)
  98.             {
  99.                 if(aPtr[i] != NULL)
  100.                     count++;
  101.             }
  102.             cout << "Number of elements: " << count << endl;
  103.         }
  104.  
  105.         ~simpleVector()
  106.         {
  107.             delete [] aPtr;
  108.         }
  109. };
  110.  
  111. #endif // SIMPLEVECTOR_H_INCLUDED
  112.  
  113. #include "simplevector.h"
  114. #include <iostream>
  115. #include <vector>
  116. using namespace std;
  117.  
  118. int main()
  119. {
  120.     simpleVector<int> integers(10);
  121.     simpleVector<char> characters(26);
  122.  
  123.     int *intArray;
  124.     char *charArray;
  125.     intArray = new int[10];
  126.     charArray = new char[26];
  127.  
  128.     for(int i = 0; i < 26; i++)
  129.     {
  130.         charArray[i] = static_cast<char>('A' + i);
  131.     }
  132.  
  133.     for(int i = 0; i < 5; i++)
  134.     {
  135.         intArray[i] = i+1;
  136.         for(int j = 5; j < 10; j++)
  137.         {
  138.             intArray[j] = 0;
  139.         }
  140.     }
  141.  
  142.     integers.set(intArray);
  143.     characters.set(charArray);
  144.  
  145.     integers.pushBack(6);
  146.     integers.pushBack(7);
  147.     integers.popBack();
  148.     integers.pushBack(8);
  149.     integers.pushBack(9);
  150.     integers.pushBack(10);
  151.     cout << "Integers array:" << endl;
  152.     integers.print();
  153.     integers.numElements();
  154.  
  155.     cout << endl << endl;
  156.     characters.popBack();
  157.     characters.popBack();
  158.     characters.popBack();
  159.     characters.pushBack('a');
  160.     cout << "Character array:" << endl;
  161.     characters.print();
  162.  
  163.     characters.numElements();
  164.     return 0;
  165.  
  166.     delete [] intArray;
  167.     delete [] charArray;
  168. }