Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/DataType/Array/ArrayInt.cpp

Go to the documentation of this file.
00001 // Member function definitions for class Array
00002 #include <iostream.h>
00003 #include <iomanip.h>
00004 #include <stdlib.h>
00005 #include <assert.h>
00006 #include "array1.h"
00007 
00008 // Initialize static data member at file scope
00009 int Array::arrayCount = 0;   // no objects yet
00010 
00011 // Default constructor for class Array (default size 10)
00012 Array::Array( int arraySize )
00013 {
00014    size = ( arraySize > 0 ? arraySize : 10 ); 
00015    ptr = new int[ size ]; // create space for array
00016    assert( ptr != 0 );    // terminate if memory not allocated
00017    ++arrayCount;          // count one more object
00018 
00019    for ( int i = 0; i < size; i++ )
00020       ptr[ i ] = 0;          // initialize array
00021 }
00022 
00023 // Copy constructor for class Array
00024 // must receive a reference to prevent infinite recursion
00025 Array::Array( const Array &init ) : size( init.size )
00026 {
00027    ptr = new int[ size ]; // create space for array
00028    assert( ptr != 0 );    // terminate if memory not allocated
00029    ++arrayCount;          // count one more object
00030 
00031    for ( int i = 0; i < size; i++ )
00032       ptr[ i ] = init.ptr[ i ];  // copy init into object
00033 }
00034 
00035 // Destructor for class Array
00036 Array::~Array()
00037 {
00038    delete [] ptr;            // reclaim space for array
00039    --arrayCount;             // one fewer objects
00040 }
00041 
00042 // Get the size of the array
00043 int Array::getSize() const { return size; }
00044 
00045 // Overloaded assignment operator
00046 // const return avoids: ( a1 = a2 ) = a3
00047 const Array &Array::operator=( const Array &right )
00048 {
00049    if ( &right != this ) {  // check for self-assignment
00050       
00051       // for arrays of different sizes, deallocate original
00052       // left side array, then allocate new left side array.
00053       if ( size != right.size ) {
00054          delete [] ptr;         // reclaim space
00055          size = right.size;     // resize this object
00056          ptr = new int[ size ]; // create space for array copy
00057          assert( ptr != 0 );    // terminate if not allocated
00058       }
00059 
00060       for ( int i = 0; i < size; i++ )
00061          ptr[ i ] = right.ptr[ i ];  // copy array into object
00062    }
00063 
00064    return *this;   // enables x = y = z;
00065 }
00066 
00067 // Determine if two arrays are equal and
00068 // return true, otherwise return false.
00069 bool Array::operator==( const Array &right ) const
00070 {
00071    if ( size != right.size )
00072       return false;    // arrays of different sizes
00073 
00074    for ( int i = 0; i < size; i++ )
00075       if ( ptr[ i ] != right.ptr[ i ] )
00076          return false; // arrays are not equal
00077 
00078    return true;        // arrays are equal
00079 }
00080 
00081 // Overloaded subscript operator for non-const Arrays
00082 // reference return creates an lvalue
00083 int &Array::operator[]( int subscript )
00084 {
00085    // check for subscript out of range error
00086    assert( 0 <= subscript && subscript < size );
00087 
00088    return ptr[ subscript ]; // reference return
00089 }
00090 
00091 // Overloaded subscript operator for const Arrays
00092 // const reference return creates an rvalue
00093 const int &Array::operator[]( int subscript ) const
00094 {
00095    // check for subscript out of range error
00096    assert( 0 <= subscript && subscript < size );
00097 
00098    return ptr[ subscript ]; // const reference return
00099 }
00100 
00101 // Return the number of Array objects instantiated
00102 // static functions cannot be const 
00103 int Array::getArrayCount() { return arrayCount; }
00104 
00105 // Overloaded input operator for class Array;
00106 // inputs values for entire array.
00107 istream &operator>>( istream &input, Array &a )
00108 {
00109    for ( int i = 0; i < a.size; i++ )
00110       input >> a.ptr[ i ];
00111 
00112    return input;   // enables cin >> x >> y;
00113 }
00114 
00115 // Overloaded output operator for class Array 
00116 ostream &operator<<( ostream &output, const Array &a )
00117 {
00118    int i;
00119 
00120    for ( i = 0; i < a.size; i++ ) {
00121       output << setw( 12 ) << a.ptr[ i ];
00122 
00123       if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output
00124          output << endl;
00125    }
00126 
00127    if ( i % 4 != 0 )
00128       output << endl;
00129 
00130    return output;   // enables cout << x << y;
00131 }

Generated on Sun Oct 14 18:46:17 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001