c++字符串的问题( 二 )


// Fig. 8.4: arrayl.h// Simple class Array (for integers}#ifndef ARRAY1_H#define ARRAY1_H#include \u0026lt; iostream.h\u0026gt;class Array { friend ostream \u0026amp;operator\u0026lt;\u0026lt;( ostream \u0026amp;, const Array \u0026amp; ); friend istream \u0026amp;operator\u0026gt;\u0026gt;( istream \u0026amp;, Array \u0026amp; );public: Array( int = 10 ); // default constructor Array( const Array \u0026amp; ); // copy constructor ~Array(); // destructor int getSize() const; // return size const Array \u0026amp;operator=( const Array \u0026amp; ); // assign arrays bool operator==( const Array \u0026amp; ) const; // compare equal // Determine if two arrays are not equal and // return true, otherwise return false (uses operator==). bool operator!=( const Array \u0026amp;right ) const { return ! ( *this == right ); } int \u0026amp;operator ( int ); // subscript operator const int \u0026amp;operator( int ) const; // subscript operator static int getArrayCount(); // Return count of // arrays instantiated. private: int size; // size of the array int *ptr; // pointer to first element of array static int arrayCount; // # of Arrays instantiated } ; #endif // Fig 8.4: arrayl.cpp // Member function definitions for class Array #include \u0026lt; iostream.h\u0026gt; #include \u0026lt; iomanip.h\u0026gt; #include \u0026lt; stdlib.h\u0026gt; #include \u0026lt; assert.h\u0026gt; #include "array1.h" // Initialize static data member at file scope int Array::arrayCount = 0; // no objects yet // Default constructor for class Array (default size 10) Array::Array( int arraySize ) { size = ( arraySize \u0026gt; 0 ? arraySize : 10 ); ptr = new int ; // create space for array assert( ptr != 0 ); // terminate if memory not allocated ++arrayCount; // count one more object for (int i = 0; i \u0026lt; size; i++ ) ptr = 0; // initialize array } // Copy constructor for class Array // must receive a reference to prevent infinite recursion Array::Array( const Array \u0026amp;init ) : size( intit.size ) { ptr = new int ; // create space for array assert( ptr != 0 ); // terminate if memory not allocated ++arrayCount; // count one more object for (int i = 0; i \u0026lt; size; i++ ) ptr init.ptr; // copy init into object } // Destructor foi class Array Array::~Array() { delete ptr; // reclaim space for array --arrayCount; // one fewer objects } // Get the size of the array int Array::getSize() const { return size; } // Overloaded assignment operator // const return avoids: ( al = a2 } = a3 const Array \u0026amp;Array::operator=( const Array \u0026amp;right ) { if ( \u0026amp;right != this ) { // check for self-assignment // for arrays of different sizes, deallocate original // left side array, then allocate new left side array. if ( size != right.size ) { delete ptr; // reclaim space size = right.size; // resize this object ptr = new int; // create space for array copy assert( ptr != 0 ); // terminate if not allocated } for (int i = 0; i \u0026lt; size; i++ ) ptr = right.ptr; // copy array into object } return *this; // enables x = y = z; } // Determine if two arrays are equal and // return true, otherwise return false. bool Array::oprator==( const Array \u0026amp;right )const { if ( size != right.size ) return false; // arrays of different sizes for (int i =0; i \u0026lt; size; i++ ) if ( ptr != right.ptr ) return false; // arrays are not equal return true; // arrays are equal } // reference return creates an lvalue int \u0026amp;Array::operator ( int subscript ) { // check for subscript out of range error assert( 0 \u0026lt;= subscript \u0026amp;\u0026amp; subscript \u0026lt; size ); return ptr; // reference return } // Overloaded subscript operator for const Arrays // const reference return creates an value const int \u0026amp;Array::operator (int subscript ) const { // check for subscript out of range error assert( 0 \u0026lt;= subscript \u0026amp;\u0026amp; subscript \u0026lt; size ); return ptr; // const reference return } // Return the number of Array objects instantiated // static functions cannot be const int Array::getArrayCount() { return arrayCount; } // Overloaded input operator for class Array; // inputs values for entire array. istream \u0026amp;operator\u0026gt;\u0026gt;( istream \u0026amp;input, Array \u0026amp;a ) { for ( int i = 0; i \u0026lt; a.size; i++ ) input \u0026gt;\u0026gt; a.ptr; return input; // enables cin \u0026gt;\u0026gt; x \u0026gt;\u0026gt; y; } // Overloaded output operator for class Array ostream \u0026amp;operator\u0026lt;\u0026lt;( ostream \u0026amp;output, const Array \u0026amp;a ) { int i; for ( i = O; i \u0026lt; a.size; i++ ) { output \u0026lt;\u0026lt; setw( 12 ) \u0026lt;\u0026lt; a.ptr; if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output output \u0026lt;\u0026lt; endl; } if( i % 4 != 0 ) output \u0026lt;\u0026lt; endl; return output; // enables cout \u0026lt;\u0026lt; ~ \u0026lt;\u0026lt; y; }


推荐阅读