Code:
#include <iostream>
using namespace std;
template <class Type, int MAXSIZE>
class Array {
private:
int mSize; // capacity of array
Type data[MAXSIZE];
public:
Array(); // create an empty array
~Array();
int num() const; // number of elements in array
void Assign(const Type *data,int mSize );
Type &operator[](int i); // array indexing, for assignment
Type operator +(const Array<Type, MAXSIZE> &a)
{
Array<Type,MAXSIZE> y;
for(int i = 0; i < mSize; i++)
y[i] = data[i]+a[i];
return y;
}
Type operator -(const Array<Type, MAXSIZE> &a)
{
Array<Type,MAXSIZE> y;
for(int i =0; i < mSize; i++)
y[i] = data[i]-a[i];
return y;
}
Type & operator =(Array<Type, MAXSIZE> z){
for(int i = 0; i < cap; i++)
{
data = data[i];
}
return *this;
}
bool operator ==(const Array<Type, MAXSIZE> & ) const;
bool operator > (const Array<Type, MAXSIZE> & ) const;
bool operator < (const Array<Type, MAXSIZE> &) const;
};
template <class Type, int MAXSIZE>
Array<Type, MAXSIZE>::Array():mSize(0){
}
template <class Type, int MAXSIZE>
Array<Type, MAXSIZE>::~Array(){
}
template <class Type, int MAXSIZE>
int Array<Type, MAXSIZE>::num() const
{
return mSize;
}
template <class Type, int MAXSIZE>
void Array<Type, MAXSIZE>::Assign(const Type *data, int mSize)
{
mSize=MAXSIZE;
data=Type;
}
template <class Type, int MAXSIZE>
bool Array<Type,MAXSIZE>:: operator ==(const Array<Type, MAXSIZE> &z) const {
if (data != z.data)
{
return true;
} else {
return false;
}
}
template <class Type, int MAXSIZE>
bool Array<Type, MAXSIZE>:: operator >(const Array<Type, MAXSIZE> &x) const {
return (data > x.data);
}
template <class Type, int MAXSIZE>
bool Array<Type,MAXSIZE>::operator <(const Array<Type, MAXSIZE> &x) const {
return (x.data < data);
}
template <class Type, int MAXSIZE>
Type &Array<Type, MAXSIZE>::operator[](int i) {
return data[i];
}
template <class Type, int MAXSIZE>
ostream &operator<<(ostream &s, const Array<Type, MAXSIZE> &a) { // print array
int i;
for (i=0; i<a.num(); i++)
s << "[" << i << "]=" << a[i] << ", ";
s << "end" << endl;
return s;
}
int main()
{
Array<int,8> test,test2,test3;
int i;
cout << "Test Integer Array output:\n";
for (i=0; i<8; i++)
{
test[i]=i;
cout << test[i];
}
cout << "\nTest2 Integer Array output:\n";
for (i=0; i<8; i++)
{
test2[i]=i;
cout << test2[i];
}
cout << "\nTest3 Integer Array output:\n";
for (i=0; i<5; i++)
{
test3[i]=i;
cout << test3[i];
}
cout << "\n";
if (test == test2)
{
cout << "We have a match!\n";
} else {
cout << "Sorry, no match!\n";
}
if (test3 > test)
{
cout << "Test3 is the bigger array.\n";
} else {
cout << "Test is the bigger array.\n";
}
Array<char,5> chartest="blah";
Array<char,6> chartest2="blah2";
if (chartest == chartest2)
{
cout << "We have a match between chartest and chartest2! \n";
} else {
cout << "No match between chartest and chartest2!\n";
}
return 0;
}
Question 1 - I made the void Assign function declaration in the class and then defined it below, as opposed to making an inline function within the class. I don't believe that should make any difference, but I wanted to confirm.