A Template Array Class
A templatised version of array.h
Changes are:
- Template parameters used instead of typedef data_type
- Two external (to the class) functions handle input and
output.
- The static data member arrays is wrapped in a base
class to allow it to be shared between class instances.
| Header |
#ifndef _TempArray_h_
#define _TempArray_h_
// INCLUDES
//
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <stdexcept>
// NAME: tempArry.H
// TYPE: C++ TEMPLATE CLASS
//
// DESCRIPTION: Interface for Array class
// EXAMPLES: See Driver (tempArry.cpp)
// BUGS:
// SEE ALSO:
////////////////////////////////////////////
using namespace std;
|
|
// base class used to allow sharing of static data member
// among instances of template class
class ArrayBase
{
// # of arrays
static int arrays;
// Return # of array objects
int Count(){ return arrays;}
};
template <class T> class Array :public ArrayBase
{
public:
// LIFECYCLE
Array(int arraySize=10); // constructor`
Array(const Array&); // copy constructor
~Array(); // destructor
// OPERATORS
const Array& operator=(const Array&);
bool operator==(const Array&) const;
bool operator!=(const Array&) const;
T& operator[](int);
// OPERATIONS
// ACCESS
int Size() const; // return size
static int Count(); // return # of Arrays instantiated
// INQUIRY
// DATA
private:
T *p; // points to first element of array
int sz; // size of array
static int arrays; // # of arrays
};
#endif // _TempArray_h_
|
|
Note:
|
|
|
| class Array |
// INCLUDES
#include"temparry.h"
// NAME: tempArry.CPP
// TYPE: C++ SOURCE
// SYNOPSIS:
// Exception handling based on the STL exception class
//
// DESCRIPTION: Implementation and Driver for Array class
// EXAMPLES:
// BUGS:
// SEE ALSO:
///////////////////////////////////////X/////
void ShowArray(Array<int>& a);
void InputArray(Array<int>& a);
|
|
template <class T>
int Array<T>::arrays=0;
// return # of Arrays instantiated
template <class T>
int Array<T>::Count(){return arrays;};
// Default constructor
template <class T> Array<T>::Array(int size) : sz(size)
{
++arrays;
p = new T[sz];
if(!p) throw runtime_error("out of memory");
}
// Copy constructor
template <class T> Array<T>::Array(const Array&
init)
{
++arrays;
sz = init.sz;
p = new T[sz];
if(!p) throw runtime_error("out of memory");
for(int i =0; i<sz; i++) // copy init data
p[i] = init.p[i];
}
// Destructor
template <class T> Array<T>::~Array()
{
--arrays;
delete[]p;
};
// Get size of array
template <class T>
int Array<T>::Size() const{ return sz;}
// Overload subscript operator
template <class T>
T& Array<T>::operator[](int subscript)
{
// check for subscript out of range error
if(subscript < 0 || subscript > sz)
throw range_error("subscript out of range");
return p[subscript]; // reference return creates lvalue
};
template <class T>
bool Array<T>::operator==(const Array& a) const
{
if(sz != a.sz)
return false; // different size
for(int i=0; i <sz; i++)
if(p[i] != a.p[i])
return false; // not equal
return true; // equal
}
// true if != else false
template <class T>
bool Array<T>::operator !=(const Array &a) const
{
if (sz != a.sz) // different size
return true;
for( int i =0; i<sz; i++)
if(p[i] != a.p[i])
return true; // not equal
return false; // equal
}
// Overloaded assignment operator
template <class T>
const Array& Array<T>::operator=(const Array& a)
{
if(&a != this) // check for self assignment
{
delete [] p;
sz = a.sz;
p = new T[sz]; // create space for array copy
if(!p) throw runtime_error("out of memory");
for(int i=0; i<sz; i++) // copy array into object
p[i] =a.p[i];
}
return *this; // enables x = y = z
};
|
|
Note:
|
|
|
|
Driver
|
|
void main()
{
// two static Arrays
Array<int> array1, array2;
try{
// test Array.Count()
cout<<"\nThere are currently "<<array1.Count()<<"
Array objects";
// test InputArray()
cout<<"\n\nEnter array1 data\n";
InputArray(array1);
// test overloaded <<
cout<<"\narray1 data : ";
ShowArray(array1);
// test overloaded []
for(int i=0; i<array1.Size(); i++)
array2[9-i]=array1[i];
// cout<<"\narray2 data : "<<array2;
// test overloaded == and !=
cout<<"\narray1 is ";
if(array1==array2)
cout<<"equal";
if(array1!=array2)
cout<<"not equal";
cout<<" to array2\n";
// test copy constructor
Array<int> array3(array1);
cout<<"\nThere are now "<<array1.Count()<<"
Array objects";
cout<<"\narray3 data : "; ShowArray(array3);
cout<<"\n\narray1 is ";
if(array1==array3)
cout<<"equal";
if(array1!=array3)
cout<<"not equal";
cout<<" to array3\n";
// array1[22] = 'a'; // this throws an out of bounds exception
array1=array2=array3;
// test overloaded == and !=
cout<<"\narray1 is ";
if(array1==array2)
cout<<"equal";
if(array1!=array2)
cout<<"not equal";
cout<<" to array2\n";
cout<<"\n\nPress any key to exit...";
}//try
catch (const exception& e)
{
cout << "\n\nException: " << e.what()
<< endl;
}
while(!getchar())
;
};
void ShowArray(Array<int>& a)
{
for(int i=0; i<a.Size(); i++)
cout<<a[i]<<' ';
cout<<endl;
};
void InputArray(Array<int>& a)
{
for(int i=0; i<a.Size(); i++)
cin>>a[i];
};
|
| Note: |
|
|
|
|