⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 doublescriptedarray.cpp

📁 《c++大学教程实验指导书》源码
💻 CPP
字号:
// Chapter 8 of C++ How to Program
// doublescriptedarray.cpp
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <iomanip>

using std::setw;

#include <cstdlib>

#include <new>

#include "doublescriptedarray.h"

// constructor
DoubleScriptedArray::DoubleScriptedArray( int r, int c )
{
   rows = ( r > 0 ? r : 10 );
   columns = ( c > 0 ? c : 10 );
   ptr = new int[ rows * columns ]; 

   for ( int i = 0; i < rows * columns; i++ )
      ptr[ i ] = 0;  

} // end class DoubleScriptedArray constructor

// copy constructor
DoubleScriptedArray::DoubleScriptedArray( const DoubleScriptedArray &init )
{
   rows = init.rows;
   columns = init.columns;

   ptr = new int[ rows * columns ];

   for ( int i = 0; i < rows * columns; i++ )
      ptr[ i ] = init.ptr[ i ];  

} // end class DoubleScriptedArray copy constructor

/* Write definition for destructor */

/* Write definition for operator = */

// function operator== definition
bool DoubleScriptedArray::operator==( const DoubleScriptedArray &right ) const
{
   if ( rows != right.rows || columns != right.columns )
      return false;    

   for ( int i = 0; i < rows * columns; i++ )

      if ( ptr[ i ] != right.ptr[ i ] )
         return false; 

   return true;        

} // end function operator==

// overloaded subscript operator for non-const Arrays
// reference return creates an lvalue
int &DoubleScriptedArray::operator()( int s1, int s2 )
{
   if ( !( s1 > 0 && s1 < rows ) )
      s1 = 0;

   if ( !( s2 > 0 && s2 < columns ) )
      s2 = 0;

   return ptr[ ( columns * s1 + s2 ) ]; 

} // end function operator()

// overloaded subscript operator for const Arrays
// const reference return creates an rvalue
/* Write overloaded subscript operator that returns an rvalue */

// function operato>> definition
istream &operator>>( istream &input, DoubleScriptedArray &a )
{
   for ( int i = 0; i < a.rows * a.columns; i++ )
      input >> a.ptr[ i ];

   return input;

} // end function operator>>

// function operator<< definition
/* Write function header for overloaded insertion operator */
{
   for ( int i = 0; i < a.rows * a.columns; i++ ) {
      output << setw( 6 ) << a.ptr[ i ];

      if ( ( i + 1 ) % a.columns == 0 )
         output << endl;

   } // end for

   if ( i % a.columns != 0 )
      output << endl;

   return output; 

} // end function operator<<



/**************************************************************************
 * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice      *
 * Hall. All Rights Reserved.                                             *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 *************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -