Skip to content

Instantly share code, notes, and snippets.

@rosonowski
Created September 23, 2011 13:00
Show Gist options
  • Save rosonowski/1237274 to your computer and use it in GitHub Desktop.
Save rosonowski/1237274 to your computer and use it in GitHub Desktop.
CS 132 Lab2
//Class prototypes go here
#ifndef ARRAYI_H_
#define ARRAYI_H_
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;
template <typename BaseData>
class Array
{
template <typename BaseData>
friend ostream &operator<<(ostream &output, const Array<BaseData> &a);
template <typename BaseData>
friend istream &operator>>(istream &input, Array<BaseData> &a);
public:
Array(int = 10); //constructor
Array(const Array &); //copy constructor
~Array(); //destructor
int getSize() const; //return size
Array &operator = (const Array &) ;
int operator==(const Array &) const; //have to be int
int operator != (const Array &) const; //have to be int
BaseData &operator[] (int);
Array operator + (const Array&);
Array operator - (const Array&);
static int getArrayCount(); //get count of existing
//array objects
private:
BaseData *ptr; //ptr to first array element
int size; //size of the array
static int arrayCount; // #of arrays instantiated
};
#include "arrayi.t"
#endif
#ifndef ARRAYI_T_
#define ARRAYI_T_
template <typename BaseData>
int Array<BaseData>::arrayCount = 0; // no objects yet
// Default constructor for class Array
template <typename BaseData>
Array<BaseData>::Array(int arraySize)
{
++arrayCount; // count one more object
size = arraySize; // default size is 10
ptr = new float[size]; // create space for array
assert(ptr != 0); // terminate if memory not allocated
int i;
for (i = 0; i < size; i++)
ptr[i] = 0; // initialize array
}
// Copy constructor for class Array
template <typename BaseData>
Array<BaseData>::Array(const Array &init)
{
++arrayCount; // count one more object
size = init.size; // size this object
ptr = new BaseData[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 BaseData>
Array<BaseData>::~Array()
{
--arrayCount; // one fewer objects
delete [] ptr; // reclaim space for array
}
// Get the size of the array
template <typename BaseData>
int Array<BaseData>::getSize() const
{
return size;
}
// Overloaded assignment operator
template <typename BaseData>
Array<BaseData> &Array<BaseData>::operator=(const Array &right)
{
if (&right != this)
{ // check for self-assignment
delete [] ptr; // reclaim space
size = right.size; // resize this object
ptr = new BaseData[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 BaseData>
Array<BaseData> Array<BaseData>::operator - (const Array& 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] ; //return right.ptr and negate the value
}
cout << "# of arrays instantiated " << getArrayCount() << endl;
return z;
}
// Determine if two arrays are equal and
// return 1 if true, 0 if false.
template <typename BaseData>
int Array<BaseData>::operator==(const Array &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 BaseData>
int Array<BaseData>::operator!=(const Array &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 BaseData>
BaseData &Array<BaseData>::operator[](int 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 BaseData>
int Array<typename BaseData>::getArrayCount()
{
return arrayCount;
}
// Overloaded input operator for class Array;
// inputs values for entire array.
template <typename BaseData>
istream &operator>>(istream &input, Array<BaseData> &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 BaseData>
ostream &operator<<(ostream &output, const Array<BaseData> &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;
}
#endif
#include "arrayi.h"
//nontemplate goes here
#include "arrayi.h"
int main()
{
Array<float> A1(5), A2(5);
A1[0]=0.5; A1[1]=1.5; A1[2]=2.5; A1[3]=3.5; A1[4]=4.5;
A2[0]=5.5; A2[1]=6.5; A2[2]=7.5; A2[3]=8.5; A2[4]=9.5;
cout << "A1 A2" << endl;
/*for(int i = 0; i<5;i++)
cout << A1[i] << " " << A2[i] << endl;
cout Array<float><< Array<float>::<< A1 - A2;
*/
cout << A1 << A2 << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment