// Chapter 8 of C++ How to Program
// doubleScriptedArray.h
#ifndef DARRAY_H
#define DARRAY_H
#include
using std::ostream;
using std::istream;
class DoubleScriptedArray {
friend ostream &operator<<(ostream&, const DoubleScriptedArray & );
/* write declaration for overloaded ostream operator */
friend istream &operator>>( istream &,const DoubleScriptedArray & );
public:
DoubleScriptedArray( int = 10, int = 10 );
DoubleScriptedArray( const DoubleScriptedArray & );
~DoubleScriptedArray();
/* write prototype for overloaded = operator /
const DoubleScriptedArray &operator=( const DoubleScriptedArray &);
bool operator==( const DoubleScriptedArray & ) const;
DoubleScriptedArray operator!=(DoubleScriptedArray &right)const
/ write header for operator != */
{ return ! ( *this == right ); }
int &operator()( int, int );
int &operator()(int ,int )const;// lvalue
/* write prototype for overloaded () operator used as
an rvalue */
private:
int rows; // number of rows in array
int columns; // number of columns in array
int *ptr; // pointer to first element of array
};
#endif
#include
using std::cout;
using std::cin;
using std::endl;
#include
using std::setw;
#include
#include
#include "doubleScriptedArray.h"
DoubleScriptedArray::DoubleScriptedArray( int r, int c )
{
rows = ( r > 0 ? r : 10 );
columns = ( c > 0 ? c : 10 );
ptr = new int[ rows * columns ];
assert( ptr != 0 );
for ( int i = 0; i < rows * columns; i++ )
ptr[ i ] = 0;
}
DoubleScriptedArray::DoubleScriptedArray(
const DoubleScriptedArray &init )
{
rows = init.rows;
columns = init.columns;
ptr = new int[ rows * columns ];
assert( ptr != 0 );
for ( int i = 0; i < rows * columns; i++ )
ptr[ i ] = init.ptr[ i ];
}
/* write definition for destructor */
DoubleScriptedArray::~DoubleScriptedArray()
{
delete []ptr;
}
/* write definition for operator = */
const DoubleScriptedArray &DoubleScriptedArray::operator =(const DoubleScriptedArray &init)
{
rows = init.rows;
columns = init.columns;
ptr = new int[ rows * columns ];
assert( ptr != 0 );
for ( int i = 0; i < rows * columns; i++ )
ptr[ i ] = init.ptr[ i ];
return *this;
}
bool DoubleScriptedArray::operator==(
const DoubleScriptedArray &right ) const
{
if ( rows != right.rows )
return false;
if ( columns != right.columns )
return false;
for ( int i = 0; i < rows * columns; i++ )
if ( ptr[ i ] != right.ptr[ i ] )
return false;
return true;
}
// Overloaded subscript operator for non-const Arrays
// reference return creates an lvalue
int &DoubleScriptedArray::operator()( int s1, int s2 )
{
assert( s1 > 0 && s1 < rows );
assert( s2 > 0 && s2 < columns );
return ptr[ columns * s1 + s2 ];
}
// Overloaded subscript operator for const Arrays
// const reference return creates an rvalue
/* write overloaded subscript operator that returns an rvalue */
int &DoubleScriptedArray::operator()(int s1,int s2 )const
{
assert( s1 > 0 && s1 < rows );
assert( s2 > 0 && s2 < columns );
return ptr[ columns * s1 + s2 ];
}
istream &operator>>( istream &input, const DoubleScriptedArray &a )
{
for ( int i = 0; i < a.rows * a.columns; i++ )
input >> a.ptr[ i ];
return input;
}
/* write function header for overloaded insertion operator */
ostream &operator<<(ostream & output,const DoubleScriptedArray &a )
{
for ( int i = 0; i < a.rows * a.columns; i++ )
{
output << setw( 6 ) << a.ptr[ i ];
if ( ( i + 1 ) % a.columns == 0 )
output << endl;
}
if ( i % a.columns != 0 )
output << endl;
return output;
}
// Chapter 8 of C++ How to Program
// Driver for class DoubleScriptedArray
#include
using std::cout;
using std::cin;
using std::endl;
#include
#include
#include
using namespace std;
#include "doubleScriptedArray.h"
int main()
{
// seed rand function
srand( time( 0 ) );
// create two arrays with different dimensions
DoubleScriptedArray a( 6, 7 ), b( 8, 2 );
cout << "Uninitialized array \"a\" is: \n" << a
<< "Uninitialized array \"b\" is: \n" << b;
// initialize array "a" with random values (0-100)
for ( int i = 0; i < 6; i++ )
for ( int j = 0; j < 7; j++ )
a(i,j)=rand()%100;
/* write statement to insert random elements (reduced
to a range of 0 - 100) into the array via
the overloaded () */
// use overloaded operator=
b = a;
cout << "\nInitialized array \"a\" is now:\n" << a
<< "Assigning b = a:\n" << b;
// check if arrays are equal using overloaded ==
if ( a == b )
cout << "\"a\" was found to be equal to \"b\"\n";
else
cout << "\"a\" was found to be not equal to \"b\"\n";
// retrieve an array element using overloaded operator()
cout << "The element (2, 1) of array \"a\" is: "
<< a( 2, 1 ) << endl;
// change an element of the array using overloaded operator()
a( 2, 1 ) = -1;
cout << "Changed element (2, 1) to -1: \n" << a;
// check if arrays are still equal
if (a==b /* write condition to check if arrays are equal */ )
cout << "\"a\" was found to be equal to \"b\"\n";
else
cout << "\"a\" was found to be NOT equal to \"b\"\n";
return 0;
}