Author Topic: C++: Some template array class assignment i had to do  (Read 1220 times)

0 Members and 1 Guest are viewing this topic.

Dreyth

  • Hero Member
  • *****
  • Posts: 2774
  • Respect: +818
    • View Profile
    • Email
C++: Some template array class assignment i had to do
« on: March 08, 2013, 03:46:27 am »
+1
If anyone cares... Did this entirely on my own! Feel proud of myself, but then again it was one of the easier labs of my class:


Array.h
Code: [Select]
#ifndef TEMPLATE_H
#define TEMPLATE_H

#include <iostream>
using namespace std;

template <class T>

class Array {

    friend istream & operator >> (istream &is, Array<T> &a) {
a.inputArray();
return is;
    }

    friend ostream & operator << (ostream &os, const Array<T> &a) {
a.outputArray();
return os;
    };

public:

    // Various constructors:
    Array<T> (); // Default constructor
    Array<T> (int);
    Array<T> (const Array<T> &); // copy constructor
    ~Array<T> (); // destructor

    // Operator overload prototypes:
    Array<T> & operator = (const Array<T> &);
    bool operator == (const Array<T> &);
    bool operator != (const Array<T> &);
    T & operator [] (int); // left hand op
    T operator [] (int) const; // right hand op

private:

    static const int DEFAULTSIZE = 10; // set default size
    T DEFAULTVALUE;                    // for the arrays
    int size;
    T *arr;

    // Function Prototypes:
    bool isEqual (const Array<T> &);
    void inputArray();
    void outputArray() const;

};

#endif

/* =================== Constructors ===================
   ==================================================== */

// Default Constructor
template <class T>
Array<T>::Array() {
    size = DEFAULTSIZE;
    arr = new T [size]; // Allocate memory

    for (int i = 0; i < size; i++)
arr[i] = DEFAULTVALUE; // Set default values
   
}

template <class T>
Array<T>::Array(int a) {
    if (a < 1)
size = DEFAULTSIZE;
    else
size = a;

    arr = new T [size];

    for (int i = 0; i < size; i++)
arr[i] = DEFAULTVALUE;
   
}

// Copy constructor
template <class T>
Array<T>::Array(const Array<T> &b) {
    size = b.size;
    arr = new T [size];

    for (int i = 0; i < size; i++)
arr[i] = b.arr[i];
}

// Destructor
template <class T>
Array<T>::~Array () {
    for (int i = 0; i < size; i++) {
delete arr;
arr = NULL;   // Make sure we sever the connection
    }
}

/* =============== Operator Overloads =================
   ==================================================== */

// Overload = operator for assignment of arrays
template <class T>
Array<T> & Array<T>::operator = (const Array<T> &b) {
    // Delete contents and set size of array
    delete arr;
    size = b.size;
    arr = new T [size];

    // Assign the contents of one array to the other
    for (int i = 0; i < size; i++)
arr[i] = b.arr[i];

    return *this;
}

// The isEqual function makes overloading
// == and != operators much easier!
template <class T>
bool Array<T>::isEqual (const Array<T> &b) {
    if (size != b.size)
return false;     // If sizes are unequal,
                             // the arrays must be as well

    for (int i = 0; i < size; i++) {
if (arr[i] != b.arr[i])
   return false;
    }

    return true;
}

// Overload == operator for equality of arrays
template <class T>
bool Array<T>::operator == (const Array<T> &b) {

    return isEqual(b);

}

// Overload == operator for inequality of arrays
template <class T>
bool Array<T>::operator != (const Array<T> &b) {

    return !isEqual(b);

}

// Overload left hand [] operator
template <class T>
T & Array<T>::operator [] (int a) {

    return arr[a];
}

// Overload right hand [] operator
template <class T>
T Array<T>::operator [] (int a) const {

    return arr[a];

}

/* ================ Member Functions ==================
   ==================================================== */

// Input function
template <class T>
void Array<T>::inputArray () {
    T userArr;
    for (int i = 0; i < size; i++) {
cout << "Element #" << i+1 << " of " << size
    << ": ";
cin >> userArr;
arr[i] = userArr;
    }
}

// Print function
template <class T>
void Array<T>::outputArray () const {
    for (int i = 0; i < size; i++)
cout << arr[i] << ' ';
}

driver.cc
Code: [Select]
#include "Template.h"

int main () {

    int arrSize;
    cout << "What do you want the size of your arrays to be? ";
    cin >> arrSize;
    cout << endl;
   
    Array<int> intArray1(arrSize);
    Array<int> intArray2 = intArray1;
   
    cout << "Here are two integer arrays of size: "
         << arrSize << "." << endl
<< "IntArray1: " << intArray1 << endl
         << "IntArray2: " << intArray2 << endl;
   
    if (intArray1 == intArray2)
        cout << "The two arrays are equal." << endl << endl;
   
    intArray1[0]=12345;
    cout << "We will now set the first element of "
<< "the first array to be "<< intArray1[0]
         << "." << endl << endl;
   
    cout << "Now enter elements for the second array."
<< endl;
    cin >> intArray2;
    cout << endl;
   
    cout << "Here are your two arrays now: " << endl
<< "IntArray1: " << intArray1 << endl
         << "IntArray2: " << intArray2 << endl;
   
    if (intArray1 == intArray2)
        cout << "The two arrays are equal" << endl << endl;
    if (intArray1 != intArray2)
        cout << "The two arrays are unequal" << endl << endl;
   
    // Create a character array of size DEFAULTSIZE
    Array<char> charArray;
    cout << "Default values for charArray: "
<< charArray << endl << endl;
    cout << "Enter elements for your charArray: " << endl;
    cin >> charArray;
    cout << endl << "Here is your charArray: " << endl
<< charArray << endl << endl;
   
    // Create a double array of size 5
    Array<double> doubleArray(5);
    cout << "Default values for doubleArray: "
<< doubleArray << endl << endl;
    cout << "Enter elements for doubleArray; " << endl;
    cin >> doubleArray;
    cout << "Here is your doubleArray: "
         << endl << doubleArray << endl;
   
return 0;
}

edit: i see the styling messed up when i copy/pasted this but oh well.
I'm LAKERS from The Vertical Summit