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

📄 table.h

📁 《c++大学教程实验指导书》源码
💻 H
字号:
// Chapter 11 of C++ How to Program
// table.h

#ifndef TABLE_H
#define TABLE_H

#include <iostream>

using std::ostream;
using std::istream;

#include <new>

/* Write code that specifies Table as a class template
   with type parameter elementType */
class Table {

public:
   Table( int = 10, int = 10 );  // default constructor 
   Table( const Table< elementType > & ); // copy constructor
   ~Table();                               
   
   void print() const;
   void inputValues();
   const Table< elementType > &operator=( 
      const Table< elementType > & );                                                          
   bool operator==( const Table< elementType > & ) const; 

   // determine if two arrays are not equal and
   // return true, otherwise return false (uses operator==)
   bool operator!=( const Table< elementType > &right ) const  
   {   
      return !( *this == right );

   } // end function operator!=

   elementType &operator()( int, int );              
   const elementType &operator()( int, int ) const;  

private:
   int rows;             // number of rows in array
   int columns;          // number of columns in array
   /* Write declaration for private data member ptr, a pointer to
      dynamically allocated array */

}; // end class Table

// default constructor
template < class elementType >
Table< elementType >::Table( int r, int c )
{
   rows = ( r > 0 ? r : 10 );
   columns = ( c > 0 ? c : 10 );
   /* Write code to allocate dynamically an array with rows * 
      columns elements of type elementType */

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

} // end class Table constructor

// constructor
template < class elementType >
Table< elementType >::Table(
   const Table< elementType > &init ) 
{
   rows = init.rows;
   columns = init.columns;

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

} // end class Table constructor

// destructor
template < class elementType >
Table< elementType >::~Table()
{
   delete [] ptr;         

} // end class Table destructor

// template function operator= definition
template < class elementType >
const Table< elementType > &Table< elementType >
   ::operator=( const Table< elementType > &right )
{
   if ( &right != this ) {  
      
      if ( rows * columns != right.rows * right.columns ) {
         delete [] ptr;         
         rows = right.rows;     
         columns = right.columns;     
         ptr = new elementType[ rows * columns ]; 

      } // end if

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

   } // end if

   return *this;   

} // end function operator=

/* Write definition for overloaded operator== */

/* Write definition for overloaded subscript operator for
    non-const Tables; reference return creates an lvalue */

// template function operator() definition
template < class elementType >
const elementType &Table< elementType >
   ::operator()( int s1, int s2 ) const
{
   if ( !( 0 <= s1 && s1 < rows ) )
      s1 = 0;

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

   return ptr[ columns * s1 + s2  ]; // rvalue

} // end function operator()

// template function inputValues definition
template < class elementType >
void Table< elementType >
   ::inputValues()
{
   for ( int i = 0; i < rows * columns; i++ )
      cin >> ptr[ i ];

} // end function inputValues

// template function print definition
template < class elementType >
void Table< elementType >
   ::print() const
{
   for ( int i = 0; i < rows * columns; i++ ) {
      cout << setw( 6 ) << ptr[ i ];

      if ( ( i + 1 ) % columns == 0 )
         cout << endl;

   } // end for

   if ( i % columns != 0 )
      cout << endl;

} // end function print

#endif // TABLE_H



/**************************************************************************
 * (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 + -