Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Array.cpp
- *
- * Created on: Oct 17, 2010
- * Author: NLA
- In this short programming assignment, you will use an int nontype parameter
- numberofElements and a type parameter T to help create a template
- for the Array class we examined in lecture, and also attached to this programming
- assignment. You will use this template to enable Array objects to be instantiated
- with a specified number of elements of a specified element type at compile time.
- Write a program with the class template Array with the following requirements:
- • This template must instantiate an Array of any element type. DONE
- • Override the template with a specific definition for an Array of float elements NOT DONE
- • Your driver class is housed in Project5LastName.cpp (where LastName is your last name) DONE
- • Your constructor, destructor, and output functions should all contain an output statement DONE
- that indicates which definition version is being called, along with any other output needed.
- • You should also provide a working makefile as described in the submission instructions DONE
- */
- #include "Array.h"
- #include <String>
- #include <typeinfo>
- #include <iostream>
- #include <iomanip>
- #include <stdio.h>
- #include <cstdlib>
- //static int arrayCount; // # of Arrays instantiated
- //T elements[ numberOfElements ];
- template< typename T >
- int Array<T>::arrayCount = 0;
- // Constructs an Array object of any type T with the passed size numberOfElements, and increment
- // arrayCount to reflect how many Array objects have been instantiated; defaults to type int with 0 elements.
- // At compile time, use Array<int>(10) array to instantiate object; Array<int>() array goes to defaults.
- template< typename T >
- Array<T>::Array(int numberOfElements) {
- if (numberOfElements > 0)
- elements = new T[numberOfElements];
- else
- elements = new T[0];
- arrayCount++;
- cout << "Array constructor called for " << typeid(this).name() << " array." << endl;
- }
- // Destroys the created Array and frees up memory.
- template< typename T >
- Array<T>::~Array() {
- arrayCount--;
- cout << "Array destructor called for " << typeid(this).name() << " array." << endl;
- }
- // Determine whether two Array objects are equal. // DONE
- template< typename T >
- bool Array<T>::operator==( const Array &anotherArray ) const {
- if (getSize() != anotherArray.getSize())
- return false;
- for (int i = 0; i < getSize(); i++) {
- if ( *this[i] != anotherArray[i])
- return false;
- }
- return true;
- }
- // Determines whether two Array objects are not equal, based on the overloaded == operator. // DONE
- template< typename T >
- bool Array<T>::operator!=( const Array &anotherArray ) const {
- if (*this == anotherArray) return false;
- return true;
- }
- // Returns the element at the position specified by the subscript. // RABBLE?
- template< typename T >
- T& Array<T>::operator[](int subscript) {
- if ( subscript < 0 || subscript >= getSize() )
- {
- cerr << "\nError: Subscript " << subscript << " is out of range" << endl;
- exit(1);
- }
- return elements[subscript];
- }
- // Takes in an input array and store a copy in the elements array. // RABBLE
- template< typename T >
- void Array<T>::inputArray() {
- for (int i = 0; i < getSize(); i++) {
- cin >> *this[i];
- }
- }
- // Outputs the elements array with 4 elements per row. // DONE
- template< typename T >
- void Array<T>::outputArray() const {
- for (int i = 0; i < getSize(); i++) { // Loop through the array, printing out ..
- for (int j = 0; j < 4; j++) { // .. 4 elements per row.
- cout << elements[i] << "\t"; // Elements separated with a space.
- }
- cout << endl;
- }
- return;
- }
- // Returns the the number of elements in the elements array. // DONE
- template< typename T >
- int Array<T>::getSize() const {
- return elements.size();
- }
- // Returns the number of arrays instantiated, which is kept track of by arrayCount. // DONE
- template< typename T >
- int Array<T>::getArrayCount() {
- return arrayCount;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement