Advertisement
Guest User

Untitled

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