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

If you want your output operator to output the data in rows and columns, you'll need nested looping to do that, or at means of knowing when you've output "column" number of elements, then output and endline, and reset that counter, if needed.

I don't anything in the input overload that should keep it from working. What happens when you attempt to use it? Is the array empty? Does input hang?

here is my driver :

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "Array2D.h"

int main()
{
	Array2D salesData(7,3);
	salesData(0,0) = 15;
	salesData(6,2) = salesData(0,0);
	cout<< salesData <<endl;
	cout<<endl;
	cout<<endl;

	Array2D sales(3,4);
	sales(1,2) = 10;
	sales(3,4) = sales(2,3);
	cout<<sales<<endl;
	cout<<endl;
	cout<<endl;

	/*cout<< " Enter  5 integers:"<<endl;
	cin >> salesData >> sales;

	cout<< "After input,arrays contain:\n"
		<<"salesData :\n" <<salesData
		<<"sales:\n" <<sales; */

	//use overloaded inequality(!=) operator
	cout <<"Comparing two arrays-- sales and salesData"<<endl;

	if(salesData != sales)
		cout<<"SalesData and Sales are not equal"<<endl;
	cout<<endl;
	cout<<endl;

	//Create sales1 using salesData as an initializer
	Array2D sales1(salesData);
	cout<<"sales1 after initialization :"<<sales1;

	//use overloaded assignment (=) operator
	cout <<"Assigning salesData to sales:" <<endl;
	sales = sales1;
	cout<< "sales1:\n"<<sales;
	cout<<endl;
	cout<<"sales:\n"<<salesData;
	cout<<endl;

	// use overloaded (==) operator
	if(sales1==sales)
		cout<< "salesData and sales are equal"<<endl; 
}

the output I get is :-

15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
15


0 0 0 0 0 0 10 0 0 0 0 0


Comparing two arrays-- sales and salesData
SalesData and Sales are not equal


sales1 after initialization : 15 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 15Assigning salesData to sales:
sales1:
15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
15
sales:
15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
15
salesData and sales are equal
Press any key to continue . . .

u can see, its printing in the same line istead of rows and column

ostream &operator<<(ostream &output, const Array2D &a)
{
	for( int i = 0; i <a.row;i++)
	{
		output<<setw(4)<<a.ptr[i];
	    for( int j = 0 ; j <a.column;j++)
		{
			output <<setw(4) <<a.ptr[j];
		}
		output<<endl;

	}
	return output;
}

this nested loop is not working..it prints in rows and columns but not right elements

You've got to separate the ptr[index] from the row and column loop counters:

ostream &operator<<(ostream &output, const Array2D &a)
{
         int index = 0;
	for( int i = 0; i <a.row;i++)
	{
	      for( int j = 0 ; j <a.column;j++)
		{
			output <<setw(4) <<a.ptr[index++];
		}
		output<<endl;

	}
	return output;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.