《c++大学教程实验指导书》源码

源代码在线查看: table.h

软件大小: 225 K
上传用户: ydyzd_2008
关键词: 大学 教程 实验指导书 源码
下载地址: 免注册下载 普通下载 VIP

相关代码

				// Chapter 11 of C++ How to Program
				// table.h
				
				#ifndef TABLE_H
				#define TABLE_H
				
				#include 
				
				using std::ostream;
				using std::istream;
				
				#include 
				
				/* 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 = 0;
				
				   if ( !( 0 				      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 				
				      if ( ( i + 1 ) % columns == 0 )
				         cout 				
				   } // end for
				
				   if ( i % columns != 0 )
				      cout 				
				} // 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.                     *
				 *************************************************************************/			

相关资源