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

CS2370 Lab 6

By: mattfong on May 17th, 2011  |  syntax: None  |  size: 2.72 KB  |  hits: 69  |  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. //Lab 6: Templates
  3. //Description: using templates to create 2 arrays (ints and chars)
  4.  
  5. #ifndef LAB6_H_INCLUDED
  6. #define LAB6_H_INCLUDED
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. template <class T> //Template class for the array types
  11. class myCollection
  12. {
  13.     private:
  14.         T *theArray;
  15.         int size;
  16.  
  17.     public:
  18.         myCollection(int i)
  19.         {
  20.             size = i; //Sets size to whatever i is
  21.         }
  22.  
  23.         void set(T *array)
  24.         {
  25.             theArray = new T[size]; //Allocates memory
  26.  
  27.             for(int i = 0; i < size; i++)
  28.             {
  29.                 theArray[i] = array[i]; //Copys the informatiom from array to theArray
  30.             }
  31.         }
  32.  
  33.         void print() //Prints out theArray
  34.         {
  35.             for (int i = 0; i < size; i++)
  36.             {
  37.                 cout << theArray[i] << " ";
  38.             }
  39.             cout << endl;
  40.         }
  41.  
  42.         ~myCollection() //Destructor that deletes theArray
  43.         {
  44.             delete [] theArray;
  45.         }
  46. };
  47.  
  48.  
  49. #endif // LAB6_H_INCLUDED
  50.  
  51.  
  52. #include "lab6.h"
  53. #include <iostream>
  54. using namespace std;
  55.  
  56. template <class T>
  57. void Min(T *array);
  58.  
  59. int main()
  60. {
  61.     myCollection<int> intArray(10); //Creates an object of int type
  62.     myCollection<char> charArray(10); //Creates an object of char type
  63.     int userint; //Input for the user's ints
  64.     char userchar; //Input for the user's chars
  65.  
  66.     int *iArray; //Pointer to be used for set
  67.     char *cArray; //Pointer to be used for set
  68.  
  69.     iArray = new int[10]; //Allocating memory
  70.     cArray = new char[10]; //Allocating memory
  71.  
  72.     for(int i = 0; i < 10; i++) //For loop for setting the ints and letters to the arrays
  73.     {
  74.         cout << "Enter a number and a letter" << endl;
  75.         cin >> userint >> userchar;
  76.         iArray[i] = userint;
  77.         cArray[i] = userchar;
  78.     }
  79.  
  80.     intArray.set(iArray); //Sets the myCollection int array
  81.     charArray.set(cArray); //Sets the myCollection char array
  82.  
  83.     intArray.print(); //Prints the int array
  84.     charArray.print(); //Prints the char array
  85.  
  86.     Min<int> (iArray); //Calls the Min function for the int array
  87.     Min<char> (cArray); //Calls the Min function for the char array
  88.  
  89.     delete [] iArray; //Deallocates the memory
  90.     delete [] cArray; //Deallocates the memory
  91.  
  92.     return 0;
  93. }
  94.  
  95. template <class T>
  96. void Min(T *array)
  97. {
  98.     T lowest = array[0]; //Sets array to the first element of the array
  99.     for(int i = 0; i < 10; i++)
  100.     {
  101.         if(lowest > array[i+1]) //If lowest is greater than the next element
  102.         {
  103.             lowest = array[i+1]; //Lowest becomes the next element
  104.         }
  105.     }
  106.     cout << "The smallest element is : " << lowest << endl;
  107. }