Originally Posted by
grumpy
Yes.
I've already given you two hints about two possible changes (adding a member function, or changing the specification of an existing member function). There are other types of change you might consider.
I'm not going to be more specific than that - this is your homework, not mine.
Generally, speaking, you need to consider how the specification of an Array<int> would need to differ from an Array<double>. At the least, I would expect an Array<double> to have one or more members with arguments or return type that are double, not int.
As I said, one change you might make to the class is to create an additional member function that provides additional capability.
New code:
// arrayi.t
Code:
#ifndef ARRAYI_H_
#define ARRAYI_H_
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;
template<typename T>
class Array;
template<typename T>
ostream &operator<< (ostream& output, const Array<T> &a);
template<typename T>
istream &operator>> (istream& input, Array<T> &a);
template<typename T>
class Array
{
public:
Array(T = 10); //constructor
Array(const Array<T> &); //copy constructor
~Array(); //destructor
T getSize() const; //return size
Array<T> &operator = (const Array<T> &);
T operator==(const Array<T> &) const;
T operator != (const Array<T> &) const;
T &operator[] (T);
Array<T> operator + (const Array<T>&);
Array<T> operator - (const Array<T>&);
static T getArrayCount(); //get count of existing
//array objects
friend ostream &operator<< <>(ostream &output, const Array<T> &a);
friend istream &operator>> <>(istream &input, Array<T> &a);
private:
T *ptr; //ptr to first array element
T size; //size of the array
static T arrayCount; // #of arrays instantiated
};
#include "arrayi.t"
#endif
// arrayi.t
Code:
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;
// Initialize static data member at file scope
template <typename T>
T Array<T>::arrayCount = 0; // no objects yet
// Default constructor for class Array
template <typename T>
Array<T>::Array(T arraySize)
{
++arrayCount; // count one more object
size = arraySize; // default size is 10
ptr = new T[size]; // create space for array
assert(ptr != 0); // terminate if memory not allocated
T i;
for (i = 0; i < size; i++)
ptr[i] = 0; // initialize array
}
// Copy constructor for class Array
template <typename T>
Array<T>::Array(const Array<T> &init)
{
++arrayCount; // count one more object
size = init.size; // size this object
ptr = new T[size]; // create space for array
assert(ptr != 0); // terminate if memory not allocated
cout << "copy constr arrayi " << endl;
int i;
for (i = 0; i < size; i++)
ptr[i] = init.ptr[i]; // copy init into object
cout << endl << "copy constructor working" << endl;
}
// Destructor for class Array
template <typename T>
Array<T>::~Array()
{
--arrayCount; // one fewer objects
delete [] ptr; // reclaim space for array
}
// Get the size of the array
template <typename T>
T Array<T>::getSize() const
{
return size;
}
// Overloaded assignment operator
template <typename T>
Array<T> &Array<T>::operator=(const Array<T> &right)
{
if (&right != this)
{ // check for self-assignment
delete [] ptr; // reclaim space
size = right.size; // resize this object
ptr = new T[size]; // create space for array copy
assert(ptr != 0); // terminate if memory not allocated
int i;
for (i = 0; i < size; i++)
ptr[i] = right.ptr[i]; // copy array into object
}
//this points to ptr to int, *this returns ptr value
//ie, the address of the array
return *this; // enables x = y = z;
}
// + operator for arrays
template <typename T>
Array<T> Array<T>::operator + (const Array<T> &right)
{
int large, small;
if (size > right.size)
{
large = size;
small = right.size;
}
else
{
large = right.size;
small = size;
}
Array z(large);
int i;
for (i = 0; i < small; i++)
z.ptr[i] = ptr[i] + right.ptr[i];
for (i = small; i < large; i++)
{
if (right.size == small)
z.ptr[i] = ptr[i] ;
else z.ptr[i] = right.ptr[i] ;
}
cout << "# of arrays instantiated " << getArrayCount() << endl;
return z;
}
// - operator for arrays
template <typename T>
Array<T> Array<T>::operator - (const Array<T> &right)
{
int large, small;
if (size > right.size)
{
large = size;
small = right.size;
}
else
{
large = right.size;
small = size;
}
Array z(large);
int i;
for (i = 0; i < small; i++)
z.ptr[i] = ptr[i] - right.ptr[i];
for (i = small; i < large; i++)
{
if (right.size == small)
z.ptr[i] = ptr[i] ;
else z.ptr[i] = right.ptr[i] ;
}
cout << "# of arrays instantiated " << getArrayCount() << endl;
return z;
}
// Determine if two arrays are equal and
// return 1 if true, 0 if false.
template <typename T>
T Array<T>::operator==(const Array<T> &right) const
{
if (size != right.size)
return 0; // arrays of different sizes
int i;
for (i = 0; i < size; i++)
if (ptr[i] != right.ptr[i])
return 0; // arrays are not equal
return 1; // arrays are equal
}
// Determine if two arrays are not equal and
// return 1 if true, 0 if false.
template <typename T>
T Array<T>::operator!=(const Array<T> &right) const
{
if (size != right.size)
return 1; // arrays of different sizes
int i;
for (i = 0; i < size; i++)
if (ptr[i] != right.ptr[i])
return 1; // arrays are not equal
return 0; // arrays are equal
}
// Overloaded subscript operator
template <typename T>
T &Array<T>::operator[](T subscript)
{
// check for subscript out of range error
assert(0 <= subscript && subscript < size);
return ptr[subscript]; // reference return creates lvalue
}
// Return the number of Array objects instantiated
template <typename T>
T Array<T>::getArrayCount()
{
return arrayCount;
}
// Overloaded input operator for class Array;
// inputs values for entire array.
template <typename T>
istream &operator>>(istream &input, Array<T> &a)
{
int i;
for (i = 0; i < a.size; i++)
input >> a.ptr[i];
return input; // enables cin >> x >> y;
}
// Overloaded output operator for class Array
template <typename T>
ostream &operator<<(ostream &output, const Array<T> &a)
{
int i;
for (i = 0; i < a.size; i++)
{
output << a.ptr[i] << ' ';
if ((i + 1) % 10 == 0)
output << endl;
} //end for
if (i % 10 != 0)
output << endl;
return output; // enables cout << x << y;
}
// client_driver
Code:
#include "arrayi.h"
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;
int main()
{
Array<int> A1(4);
Array<int> A2(7);
Array<int> A3;
A1.getSize();
A1.getArrayCount();
cout << "Enter values for first array: ";
cin >> A1;
cout << "Enter values for second array: ";
cin >> A2;
cout << "Difference of first and second array: ";
A3 = A2 - A1;
cout << A3;
}
After posting my new code, things are becoming more clear. Have I accomplished my task successfully? I am unsure whether I would need to use the [], ==, and != operators for. Are they there just in case I need to use them? Also, I am unsure why I would need to called the member class functions getSize() and getArrayCount.
Thank you guys, you are the best!