Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *
- * Created on: Oct 17, 2010
- * Author: Peter
- 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 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
- */
- #ifndef ARRAY_H_
- #define ARRAY_H_
- #include <String>
- #include <typeinfo>
- #include <iostream>
- #include <iomanip>
- #include <stdio.h>
- #include <cstdlib>
- #include <stdlib.h>
- using namespace std;
- template < typename T >
- class Array {
- public:
- Array(); // Default constructor (Array object with 10 elements
- ~Array(); // Destructor
- int getSize() const;
- bool operator==(const Array &anotherArray) const;
- bool operator!=(const Array &anotherArray) const;
- T& operator[](int subscript); // subscript operator
- static int getArrayCount(); // return count of arrays instantiated
- void inputArray();
- void outputArray() const; // output the array elements
- private:
- T elements[numberOfElements];
- static int arrayCount; // # of Arrays instantiated
- };
- template< typename T, int numberOfElements >
- int Array<T, numberOfElements>::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, int numberOfElements >
- Array<T, numberOfElements>::Array() {
- if (numberOfElements > 0)
- elements = new T[numberOfElements];
- else
- elements = new T[10];
- arrayCount++;
- cout << "Array constructor called for " << typeid(this).name() << endl;
- }
- /*
- Array<Float>::Array(int numberOfElements) {
- if (numberOfElements > 0)
- elements = new T[numberOfElements];
- else
- elements = new T[10];
- arrayCount++;
- cout << "Array constructor called for float array." << endl;
- }
- */
- // Destroys the created Array and frees up memory. // DONE
- template< typename T, int numberOfElements >
- Array<T, numberOfElements>::~Array() {
- delete[] elements;
- arrayCount--;
- cout << "Array destructor called for " << typeid(this).name() << endl;
- }
- // Determine whether two Array objects are equal. // DONE
- template< typename T, int numberOfElements >
- bool Array<T, numberOfElements>::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, int numberOfElements >
- bool Array<T, numberOfElements>::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, int numberOfElements >
- T& Array<T, numberOfElements>::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. // DONE?
- template< typename T, int numberOfElements >
- void Array<T, numberOfElements>::inputArray() {
- for (int i = 0; i < getSize(); i++) {
- cin >> (*this)[i];
- }
- }
- // Outputs the elements array with 4 elements per row. // DONE
- template< typename T, int numberOfElements >
- void Array<T, numberOfElements>::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 numberOfElements >
- int Array<T, numberOfElements>::getSize() const {
- return numberOfElements;
- }
- // Returns the number of arrays instantiated, which is kept track of by arrayCount. // DONE
- template< typename T, int numberOfElements >
- int Array<T, numberOfElements>::getArrayCount() {
- return arrayCount;
- }
- #endif /* ARRAY_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement