Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.12 KB | None | 0 0
  1. /*
  2.  *
  3.  *  Created on: Oct 17, 2010
  4.  *      Author: Peter
  5.  
  6. In this short programming assignment, you will use an int nontype parameter
  7.     numberofElements and a type parameter T to help create a template
  8.     for the Array class we examined in lecture, and also attached to this programming
  9.     assignment. You will use this template to enable Array objects to be instantiated
  10.     with a specified number of elements of a specified element type at compile time.
  11.  
  12. Write a program with the class template Array with the following requirements:
  13.     • This template must instantiate an Array of any element type.                                        DONE
  14.     • Override the template with a specific definition for an Array of float elements                 DONE
  15.     • Your driver class is housed in Project5LastName.cpp (where LastName is your last name)          DONE
  16.     • Your constructor, destructor, and output functions should all contain an output statement           DONE
  17.         that indicates which definition version is being called, along with any other output needed.
  18.     • You should also provide a working makefile as described in the submission instructions          DONE
  19.  */
  20.  
  21. #ifndef ARRAY_H_
  22. #define ARRAY_H_
  23.  
  24. #include <String>
  25. #include <typeinfo>
  26. #include <iostream>
  27. #include <iomanip>
  28. #include <stdio.h>
  29. #include <cstdlib>
  30. #include <stdlib.h>
  31.  
  32. using namespace std;
  33.  
  34. template < typename T >
  35. class Array {
  36. public:
  37.     Array(); // Default constructor (Array object with 10 elements
  38.     ~Array(); // Destructor
  39.     int getSize() const;
  40.     bool operator==(const Array &anotherArray) const;
  41.     bool operator!=(const Array &anotherArray) const;
  42.     T& operator[](int subscript); // subscript operator
  43.     static int getArrayCount(); // return count of arrays instantiated
  44.     void inputArray();
  45.     void outputArray() const; // output the array elements
  46. private:
  47.     T elements[numberOfElements];
  48.     static int arrayCount; // # of Arrays instantiated
  49. };
  50.  
  51. template< typename T, int numberOfElements >
  52. int Array<T, numberOfElements>::arrayCount = 0;
  53.  
  54. // Constructs an Array object of any type T with the passed size numberOfElements, and increment
  55. // arrayCount to reflect how many Array objects have been instantiated; defaults to type int with 0 elements.
  56. // At compile time, use Array<int>(10) array to instantiate object; Array<int>() array goes to defaults.
  57. template< typename T, int numberOfElements >
  58. Array<T, numberOfElements>::Array() {
  59.  
  60.     if (numberOfElements > 0)
  61.         elements = new T[numberOfElements];
  62.     else
  63.         elements = new T[10];
  64.  
  65.     arrayCount++;
  66.     cout << "Array constructor called for " << typeid(this).name() << endl;
  67.  
  68. }
  69. /*
  70. Array<Float>::Array(int numberOfElements) {
  71.  
  72.     if (numberOfElements > 0)
  73.         elements = new T[numberOfElements];
  74.     else
  75.         elements = new T[10];
  76.  
  77.     arrayCount++;
  78.     cout << "Array constructor called for float array." << endl;
  79.  
  80. }
  81. */
  82.  
  83. // Destroys the created Array and frees up memory. // DONE
  84. template< typename T, int numberOfElements >
  85. Array<T, numberOfElements>::~Array() {
  86.     delete[] elements;
  87.     arrayCount--;
  88.     cout << "Array destructor called for " << typeid(this).name() << endl;
  89. }
  90.  
  91. // Determine whether two Array objects are equal. // DONE
  92. template< typename T, int numberOfElements >
  93. bool Array<T, numberOfElements>::operator==( const Array &anotherArray ) const {
  94.  
  95.     if (getSize() != anotherArray.getSize())
  96.         return false;
  97.  
  98.     for (int i = 0; i < getSize(); i++) {
  99.         if ( *this[i] != anotherArray[i])
  100.             return false;
  101.     }
  102.  
  103.     return true;
  104.  
  105. }
  106.  
  107. // Determines whether two Array objects are not equal, based on the overloaded == operator. // DONE
  108. template< typename T, int numberOfElements >
  109. bool Array<T, numberOfElements>::operator!=( const Array &anotherArray ) const {
  110.     if (*this == anotherArray) return false;
  111.     return true;
  112. }
  113.  
  114. // Returns the element at the position specified by the subscript. // RABBLE?
  115. template< typename T, int numberOfElements >
  116. T& Array<T, numberOfElements>::operator[](int subscript) {
  117.  
  118.     if ( subscript < 0 || subscript >= getSize() )
  119.     {
  120.         cerr << "\nError: Subscript " << subscript << " is out of range" << endl;
  121.         exit(1);
  122.     }
  123.  
  124.     return elements[subscript];
  125.  
  126. }
  127.  
  128. // Takes in an input array and store a copy in the elements array. // DONE?
  129. template< typename T, int numberOfElements >
  130. void Array<T, numberOfElements>::inputArray() {
  131.  
  132.     for (int i = 0; i < getSize(); i++) {
  133.         cin >> (*this)[i];
  134.     }
  135.  
  136. }
  137.  
  138. // Outputs the elements array with 4 elements per row. // DONE
  139. template< typename T, int numberOfElements >
  140. void Array<T, numberOfElements>::outputArray() const {
  141.  
  142.     for (int i = 0; i < getSize(); i++) { // Loop through the array, printing out ..
  143.         for (int j = 0; j < 4; j++) { // .. 4 elements per row.
  144.             cout << elements[i] << "\t"; // Elements separated with a space.
  145.         }
  146.         cout << endl;
  147.     }
  148.     return;
  149.  
  150. }
  151.  
  152. // Returns the the number of elements in the elements array. // DONE
  153. template< typename T, int numberOfElements >
  154. int Array<T, numberOfElements>::getSize() const {
  155.     return numberOfElements;
  156. }
  157.  
  158. // Returns the number of arrays instantiated, which is kept track of by arrayCount. // DONE
  159. template< typename T, int numberOfElements >
  160. int Array<T, numberOfElements>::getArrayCount() {
  161.     return arrayCount;
  162. }
  163.  
  164. #endif /* ARRAY_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement