User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,588 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,634 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 2709 | Replies: 1
Reply
Join Date: May 2004
Posts: 21
Reputation: ze_viru$ is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
ze_viru$'s Avatar
ze_viru$ ze_viru$ is offline Offline
Newbie Poster

Help Help-overloading operators

  #1  
Nov 10th, 2004
hi guys,

I want to create a class called DoubleSubScriptedArray.this array should be able to take maybe a 3 by 5 array or 1 by 3.My program is giving me a problem when i try to overload the stream-insertion and stream-extraction operator.Below is my program pliz help me to get rid of thge errors:

#ifndef DOUBLESUBSCRIPTEDARRAY_H

#define DOUBLESUBSCRIPTEDARRAY_H

#include <iostream>

using namespace std;

// class DoubleScriptedArray definition
class DoubleSubscriptedArray {

   friend ostream &operator<<(ostream &,const DoubleSubcriptedArray &);
   friend istream &operator>>( istream &, DoubleSubcriptedArray & );
                               
public:

   DoubleSubcriptedArray( int = 10, int = 10 );            
   
   DoubleSubcriptedArray( const DoubleSubcriptedArray & );   
   
   ~DoubleSubcriptedArray();
       
	const DoubleSubscriptedArray &operator=( const DoubleSubscriptedArray &) 
    
	bool operator==( const DoubleSubcriptedArray & ) const; 

   bool operator!=( const DoubleSubscriptedArray &right)           
   { 
      return ! ( *this == right );

   } // end function operator!=
   
   int &operator()( int, int );   // lvalue
   
   const int &operator(int,int)const; //prototype for overloaded()
   
   static int getArrayCount(); 

private:

   int rows;   // number of rows in array

   int columns;  // number of columns in array

   int *ptr;     // pointer to first element of array

   static int arraycount;

}; // end class DoubleScriptedArray

#endif 
            
#include <iostream>

using std::ostrstream;

using std::istrstream;

using namespace std;

#include <iomanip>

using std::setw;

#include <cstdlib>

#include <new>

#include <cassert>

#include "DoubleSubscriptedArray.h"

int arrayCount = 0;

// constructor
DoubleSubcriptedArray::DoubleScriptedArray( int r, int c )
{
   rows = ( r > 0 ? r : 10 );

   columns = ( c > 0 ? c : 10 );

   ptr = new int[ rows * columns ]; 

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

} // end class DoubleScriptedArray constructor

// copy constructor
DoubleSubcriptedArray::DoubleSubcriptedArray( const DoubleSubcriptedArray &init )
{
   int init;
	rows = init.rows;

   columns = init.columns;

   ptr = new int[ rows * columns ];

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

} // end class DoubleSubcriptedArray copy constructor

//destructor
DoubleSubscriptedArray::~DoubleSubscriptedArray()
{

   delete [] ptr;
    --arrrayCount;

}

//definition for operator =
const DoubleSubscriptedArray &DoubleSubscriptedArray operator=(const DoubleSubscriptedArray &right)
{
	if(&right != this){
		if(rows != right.rows || columns != right.columns){
			delete [] ptr;

			rows = right.rows;

			columns = right.columns;

			prt = new int [rows * columns];

			assert (ptr != 0);

		}

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

	}

	return *this;

}

// function operator== definition
bool DoubleSubcriptedArray::operator==( const DoubleSubcriptedArray &right ) const
{
   if ( rows != right.rows || columns != right.columns )
      return false;    

   for ( int i = 0; i < rows * columns; i++ )

      if ( ptr[ i ] != right.ptr[ i ] )
         return false;

	     return true;        

} // end function operator==


// overloaded subscript operator for non-const Arrays
// reference return creates an lvalue
int &DoubleSubcriptedArray::operator()( int d, int e )
{
   if ( !( d > 0 && e < rows ) )
      d = 0;

   if ( !( d > 0 && e < columns ) )
      e = 0;

   return ptr[ ( columns * d + e ) ]; 

} // end function operator()

// overloaded sububscript operator for const Arrays
// const reference return creates an rvalue
const int &DoubleSubcriptedArray::operator()( int d, int e ) const
{
   if ( !( d > 0 && e < rows ) )
      d = 0;

   if ( !( d > 0 && e < columns ) )
      e = 0;

   return ptr[ ( columns * d + e ) ]; 

} // end function operator()

//Return the number of Array objects instantiated
int DoubleSubscriptedArray::getarrayCount()
{
	return arrayCount;

}

// function operator>> definition
istream &operator>>( istream &input, DoubleSubcriptedArray &p )
{
   for ( int a = 0; a < p.rows * p.columns; a++ )
      input >> p.ptr[ a ];

   return input;

} // end function operator>>

// function operator<< definition
ostream &operator <<(ostream &output, const DoubleSubscriptedArray &p )
{
   for ( int i = 0; i < a.rows * a.columns; i++ ) {
      output << setw( 6 ) << a.ptr[ i ];

      if ( ( i + 1 ) % a.columns == 0 )
         output << endl;

   } // end for

   if ( i % a.columns != 0 )
      output << endl;

   return output; 

} // end function operator<<


#include <iostream>

using std::istream;

using std::ostream;

#include <ctime>

#include "DoubleSubscriptedArray.h"

int main() 
{
   // seed rand function
   srand( time( 0 ) );

   // create two arrays
  DoubleScriptedArray Pack1( 6, 7);

  DoubleScriptedArray Pack2( 8, 2);

   cout << "Uninitialized array \"Pack1\" is: \n" << Pack1 
        << "Uninitialized array \"Pack2\" is: \n" << Pack2;

   // initialize array "a" with random values (0-100)
   for ( int a = 0; a < 6; a++ )

	   for ( int b = 0; b < 7; b++ ){

		   output<<"Enter array elements : ";
			   input>>Pack1;
    Pack2 = Pack1;

   output<< "\nInitialized array \"Pack1\" is now:\n" << Pack1
         << "Assigning Pack2 = Pack1:\n" << Pack2;

   // check if arrays are equal using overloaded ==
   if ( Pack1 == Pack2 )
      output<<"\"Pack1\" was found to be equal to \"Pack2\"\n";
   else
      output<<"\"Pack1\" was found to be not equal to \"Pack2\"\n";

   // retrieve array element using overloaded operator()
   output<<"The element (2, 1) of array \"Pack1\" is: "
         <<Pack1( 2, 1 )<< endl;

   // change element of array using overloaded operator()
   Pack1( 2, 1 ) = -1;
    output<<"Changed element (2, 1) to -1: \n" << Pack1;

   // check if arrays are still equal
   if ( Pack1==Pack2 )
      output<<"\"Pack1\" was found to be equal to \"Pack2\"\n";
   else
      output<<"\"Pack1\" was found to be Not equal to \"Pack2\"\n";

   return 0;

} // end main
:eek:
Last edited by alc6379 : Nov 10th, 2004 at 10:55 am. Reason: added [code] tags
[i]What makes the world to be a dangerous place is not the people who do evil,but is the people who just sit on their buds. :)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,017
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Help-overloading operators

  #2  
Nov 10th, 2004
>Below is my program pliz help me to get rid of thge errors
When you're getting errors, post them. I'm not going to cut and paste your code, compile it, and debug it just for you. That's a lot of work for very little gain.
Member of: Beautiful Code Club.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:07 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC