I have working version of code but mu ostream<< function should print data acoording to the row and column specified.
Here is the codes :-
//
// 4/11/2008
// This is a header file of Array2D class which stores 2D arrays
#ifndef ARRAY2D_H
#define ARRAY2D_H
#include <iostream>
using std::ostream;
using std::istream;
class Array2D
{
friend ostream &operator<<( ostream & , const Array2D &);
friend istream &operator>>( istream &, Array2D &);
public:
Array2D(int,int);
Array2D( const Array2D &);
~Array2D();
int getSize()const;
const Array2D &operator = (const Array2D &);
bool operator ==(const Array2D &) const;
bool operator!=(const Array2D &right) const
{
return!(*this == right);
}
double operator() (int,int) const;
double &operator() (int, int);
private:
int size;
double *ptr;
int row;
int column;
};
#endif
.cpp file
//
// 4/11/2008
// This file includes the member-function definition for class Array2D
#include <iostream>
using std::cerr;
using std::cout;
using std::cin;
using std::endl;
#include <iomanip>
using std::setw;
#include <cstdlib>
using std::exit;
#include "Array2D.h"
// this is a contsructor which initializes all rows & columns to 0
Array2D::Array2D(int rows, int columns)
{
row = rows;
column = columns;
size = rows * columns;
ptr = new double[size];
for(int i = 0; i<size; i++)
{
ptr[i] = 0;
}
}
//this is a destructor
Array2D::~Array2D()
{
delete [] ptr;
}
// this function returns the size of array
int Array2D::getSize() const
{
//size = row * column;
return size;
}
//copy constructor for class Array2D which makes a copy
//of an array
Array2D::Array2D(const Array2D &arrayToCopy)
:size(arrayToCopy.size)
{
size = arrayToCopy.getSize();
ptr = new double[size];
row = arrayToCopy.row;
column = arrayToCopy.column;
for( int i = 0; i<size;i++)
{
ptr[i] = arrayToCopy.ptr[i];
}
}
// overloading () operator to access the array items
double Array2D::operator()(int row1, int col1)const
{
int index;
index = (column *row1) + col1;
return ptr[index];
}
// overloading () operator to access the array items
double &Array2D :: operator()(int row2, int col2)
{
int index;
index = (column *row2) + col2;
return ptr[index];
}
// overloading = operator
const Array2D &Array2D::operator=( const Array2D &right)
{
if( &right != this)
{
delete [] ptr;
row = right.row;
column = right.column;
size = row*column;
ptr = new double[ size];
}
for( int i = 0; i <size; i++)
{
ptr[i] = right.ptr[i];
}
return *this;
}
// overloading == operator to determine if two arrays are equal
bool Array2D::operator==( const Array2D &right) const
{
if( row != right.row)
return false;
else if(column!= right.column)
return false;
for( int i = 0 ; i< size ; i++)
if (ptr[ i] != right.ptr[ i])
return false;
return true;
}
//overloading input operator
istream &operator>>(istream &input , Array2D &a)
{
for(int i = 0; i <a.size ; i++)
{
input>> a.ptr[i];
}
return input;
}
//overloading output operator
ostream &operator<<(ostream &output, const Array2D &a)
{
for( int i = 0; i <a.size;i++)
{
output<<setw(4)<<a.ptr[i];
}
return output;
}
any idea where am I doing wrong??? ostream should print data but my ostream print everything in the same line ....I don't know why??
also in istream>> I am not able to input numbers from user??
thanks again