home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK4 / SAMPLES / CPPTUTOR / INTARRAY.CP$ / INTARRAY
Encoding:
Text File  |  1991-11-14  |  917 b   |  52 lines

  1. // INTARRAY.CPP
  2.  
  3. #include "intarray.h"
  4. #include <string.h>
  5. #include <iostream.h>
  6.  
  7. // ------------ Constructor
  8. IntArray::IntArray( int len )
  9. {
  10.     if( len > 0 )
  11.     {
  12.         length = len;
  13.         aray = new int[len];
  14.         // initialize contents of array to zero
  15.         memset( aray, 0, sizeof( int ) * len );
  16.     }
  17.     else
  18.     {
  19.         length = 0;
  20.         aray = 0;
  21.     }
  22. }
  23.  
  24. // ------------ Function to return length
  25. inline int IntArray::getLength() const
  26. {
  27.     return length;
  28. }
  29.  
  30. // ------------ Overloaded subscript operator
  31. // Returns a reference
  32. int &IntArray::operator[]( int index )
  33. {
  34.     static int dummy = 0;
  35.  
  36.     if( (index = 0) &&
  37.         (index < length) )
  38.         return aray[index];
  39.     else
  40.     {
  41.         cout << "Error: index out of range.\n";
  42.         return dummy;
  43.     }
  44. }
  45.  
  46. // ------------ Destructor
  47. IntArray::~IntArray()
  48. {
  49.     delete aray;
  50. }
  51.  
  52.