deopen
یک شنبه 22 شهریور 1388, 00:32 صبح
من در کد زیر کلاس Array رو بسط دادم به گونه ای که امکان تغییر بعد و اندازه رو داشته باشه , البته بصورت مجازی 2 بعدیش کردم , در کدم عملگرای پرانتز را برای مقدار دهی و برگشت دادن مقدار overload کردم , کد ممکن مشکلاتی داشته باشه , از دوستان میخوام که تستش کنن و مشکلاتش را بگویند, تا کد خوبی از آب در بیاد.
//Array.h
#ifndef ARRAY_H
#define ARRAY_H
class Array
{
public:
Array( int = 1,int =1);// default constructor
Array( const Array & ); // copy constructor
~Array(); // destructor
int getSize() const; // return size
const Array &operator=( const Array & ); // assignment operator
bool operator==( const Array & ) const; // equality operator
// inequality operator; returns opposite of == operator
bool operator!=( const Array &right ) const
{
return ! ( *this == right ); // invokes Array::operator==
} // end function operator!=
// subscript operator for non-const objects returns modifiable lvalue
int &operator[]( int );
// subscript operator for const objects returns rvalue
int operator[]( int)const;
int &operator()( int);//overload ()
int operator()( int)const;
int &operator()( int,int);
int operator()( int,int)const;
void renew(int,int);
void renew(int);
private:
int i,j;
int size; // pointer-based array size
int *ptr; // pointer to first element of pointer-based array
}; // end class Array
#endif
//Array.cpp
#include <iostream>
using std::cerr;
using std::cout;
using std::cin;
using std::endl;
#include <iomanip>
using std::setw;
#include <cstdlib> // exit function prototype
using std::exit;
#include "Array.h" // Array class definition
// default constructor for class Array (default size 10)
Array::Array( int i,int j ):
i(i),j(j)
{
if (i>0 && j>0)
size=i*j;
// validate arraySize
ptr = new int[ size ]; // create space for pointer-based array
for ( int i = 0; i < size; i++ )
ptr[ i ] = 0; // set pointer-based array element
} // end Array default constructor
// copy constructor for class Array;
// must receive a reference to prevent infinite recursion
Array::Array( const Array &arrayToCopy )
: size( arrayToCopy.size )
{
ptr = new int[ size ]; // create space for pointer-based array
for ( int i = 0; i < size; i++ )
ptr[ i ] = arrayToCopy.ptr[ i ]; // copy into object
} // end Array copy constructor
// destructor for class Array
Array::~Array()
{
delete [] ptr; // release pointer-based array space
} // end destructor
// return number of elements of Array
int Array::getSize() const
{
return size; // number of elements in Array
} // end function getSize
// overloaded assignment operator;
// const return avoids: ( a1 = a2 ) = a3
const Array &Array::operator=( const Array &right )
{
if ( &right != this ) // avoid self-assignment
{
// for Arrays of different sizes, deallocate original
// left-side array, then allocate new left-side array
if ( size != right.size )
{
delete [] ptr; // release space
size = right.size;
i=right.i;
j=right.j;// resize this object
ptr = new int[ size ]; // create space for array copy
} // end inner if
for ( int i = 0; i < size; i++ )
ptr[ i ] = right.ptr[ i ]; // copy array into object
} // end outer if
return *this; // enables x = y = z, for example
} // end function operator=
// determine if two Arrays are equal and
// return true, otherwise return false
bool Array::operator==( const Array &right ) const
{
if ( size != right.size || i != right.i || j != right.j )
return false; // arrays of different number of elements
for ( int i = 0; i < size; i++ )
if ( ptr[ i ] != right.ptr[ i ] )
return false; // Array contents are not equal
return true; // Arrays are equal
} // end function operator==
// overloaded subscript operator for non-const Arrays;
// reference return creates a modifiable lvalue
int &Array::operator[]( int subscript )
{
// check for subscript out-of-range error
if ( subscript < 0 || subscript >= size )
{
cerr << "\nError: Subscript " << subscript
<< " out of range" << endl;
exit( 1 ); // terminate program; subscript out of range
} // end if
return ptr[ subscript ]; // reference return
} // end function operator[]
// overloaded subscript operator for const Arrays
// const reference return creates an rvalue
int Array::operator[]( int subscript)const
{
// check for subscript out-of-range error
if ( subscript < 0 || subscript >= size )
{
cerr << "\nError: Subscript " << subscript
<< " out of range" << endl;
exit( 1 ); // terminate program; subscript out of range
} // end if
return ptr[ subscript ]; // returns copy of this element
} // end function operator[]
int &Array::operator()( int index) {
if ( index < 0 || index >= size )
{
cerr << "\nError: Subscript " << index
<< " out of range" << endl;
exit( 1 ); // terminate program; subscript out of range
} // end if
return ptr[ index ];
}
int Array::operator()( int index) const {
if ( index < 0 || index >= size )
{
cerr << "\nError: Subscript " << index
<< " out of range" << endl;
exit( 1 ); // terminate program; subscript out of range
} // end if
return ptr[ index ];
}
int &Array::operator()( int index,int index2) {
if ( index < 0 || index2 < 0 || (index*index2) >= size || index > i || index2 > j)
{
cerr << "\nError: Subscript " << index<< "," << index2
<< " out of range" << endl;
exit( 1 ); // terminate program; subscript out of range
} // end if
return ptr[ (index*index2) ];
}
int Array::operator()( int index,int index2)const {
if ( index < 0 || index2 < 0 || (index*index2) >= size || index > i || index2 > j)
{
cerr << "\nError: Subscript " << index << "," << index2
<< " out of range" << endl;
exit( 1 ); // terminate program; subscript out of range
} // end if
return ptr[ (index*index2) ];
}
// overloaded input operator for class Array;
// inputs values for entire Array
void Array::renew(int newI,int newJ) {
if (newI!=i && newJ!=j) {
int *cpy=0;
cpy=new int[size];
for (int i=0;i<size;i++)
cpy[i]=ptr[i];
delete [] ptr; // release space
int lastSize=size;
size = newI*newJ;
bool sze;
if (size>lastSize)
sze=true;
else
sze=false;
Array::i=newI;
Array::j=newJ;// resize this object
ptr = new int[ size ];
for (int i=0;i<(sze?lastSize:size);i++)
ptr[i]=cpy[i];
if(sze) {
for (int i=lastSize;i<size;i++)
ptr[i]=0;
}
delete [] cpy;
}
}
void Array::renew(int newI) {
if (newI!=i) {
int *cpy=0;
cpy=new int[size];// release space
for (int i=0;i<size;i++)
cpy[i]=ptr[i];
delete [] ptr;
int lastSize=size;
size = newI;
bool sze;
if (size>lastSize)
sze=true;
else
sze=false;
Array::i=newI;
ptr = new int[ size ];
for (int i=0;i<(sze?lastSize:size);i++)
ptr[i]=cpy[i];
if(sze) {
for (int i=lastSize;i<size;i++)
ptr[i]=0;
}
delete [] cpy;
}
}
#include<conio.h>
int main()
{
Array Arr(4,2);
Arr(2,1)=1;
Arr.renew(6,4);
cout<<Arr(2,1);
getch();
return 0;
} // end main
این کد , کد تغییر یافته که در کتاب deitel معرفی شد می باشد , من امکان 2 بعدی بودن و تغییر بعد و اندازه را به آن اضافه کردم.
//Array.h
#ifndef ARRAY_H
#define ARRAY_H
class Array
{
public:
Array( int = 1,int =1);// default constructor
Array( const Array & ); // copy constructor
~Array(); // destructor
int getSize() const; // return size
const Array &operator=( const Array & ); // assignment operator
bool operator==( const Array & ) const; // equality operator
// inequality operator; returns opposite of == operator
bool operator!=( const Array &right ) const
{
return ! ( *this == right ); // invokes Array::operator==
} // end function operator!=
// subscript operator for non-const objects returns modifiable lvalue
int &operator[]( int );
// subscript operator for const objects returns rvalue
int operator[]( int)const;
int &operator()( int);//overload ()
int operator()( int)const;
int &operator()( int,int);
int operator()( int,int)const;
void renew(int,int);
void renew(int);
private:
int i,j;
int size; // pointer-based array size
int *ptr; // pointer to first element of pointer-based array
}; // end class Array
#endif
//Array.cpp
#include <iostream>
using std::cerr;
using std::cout;
using std::cin;
using std::endl;
#include <iomanip>
using std::setw;
#include <cstdlib> // exit function prototype
using std::exit;
#include "Array.h" // Array class definition
// default constructor for class Array (default size 10)
Array::Array( int i,int j ):
i(i),j(j)
{
if (i>0 && j>0)
size=i*j;
// validate arraySize
ptr = new int[ size ]; // create space for pointer-based array
for ( int i = 0; i < size; i++ )
ptr[ i ] = 0; // set pointer-based array element
} // end Array default constructor
// copy constructor for class Array;
// must receive a reference to prevent infinite recursion
Array::Array( const Array &arrayToCopy )
: size( arrayToCopy.size )
{
ptr = new int[ size ]; // create space for pointer-based array
for ( int i = 0; i < size; i++ )
ptr[ i ] = arrayToCopy.ptr[ i ]; // copy into object
} // end Array copy constructor
// destructor for class Array
Array::~Array()
{
delete [] ptr; // release pointer-based array space
} // end destructor
// return number of elements of Array
int Array::getSize() const
{
return size; // number of elements in Array
} // end function getSize
// overloaded assignment operator;
// const return avoids: ( a1 = a2 ) = a3
const Array &Array::operator=( const Array &right )
{
if ( &right != this ) // avoid self-assignment
{
// for Arrays of different sizes, deallocate original
// left-side array, then allocate new left-side array
if ( size != right.size )
{
delete [] ptr; // release space
size = right.size;
i=right.i;
j=right.j;// resize this object
ptr = new int[ size ]; // create space for array copy
} // end inner if
for ( int i = 0; i < size; i++ )
ptr[ i ] = right.ptr[ i ]; // copy array into object
} // end outer if
return *this; // enables x = y = z, for example
} // end function operator=
// determine if two Arrays are equal and
// return true, otherwise return false
bool Array::operator==( const Array &right ) const
{
if ( size != right.size || i != right.i || j != right.j )
return false; // arrays of different number of elements
for ( int i = 0; i < size; i++ )
if ( ptr[ i ] != right.ptr[ i ] )
return false; // Array contents are not equal
return true; // Arrays are equal
} // end function operator==
// overloaded subscript operator for non-const Arrays;
// reference return creates a modifiable lvalue
int &Array::operator[]( int subscript )
{
// check for subscript out-of-range error
if ( subscript < 0 || subscript >= size )
{
cerr << "\nError: Subscript " << subscript
<< " out of range" << endl;
exit( 1 ); // terminate program; subscript out of range
} // end if
return ptr[ subscript ]; // reference return
} // end function operator[]
// overloaded subscript operator for const Arrays
// const reference return creates an rvalue
int Array::operator[]( int subscript)const
{
// check for subscript out-of-range error
if ( subscript < 0 || subscript >= size )
{
cerr << "\nError: Subscript " << subscript
<< " out of range" << endl;
exit( 1 ); // terminate program; subscript out of range
} // end if
return ptr[ subscript ]; // returns copy of this element
} // end function operator[]
int &Array::operator()( int index) {
if ( index < 0 || index >= size )
{
cerr << "\nError: Subscript " << index
<< " out of range" << endl;
exit( 1 ); // terminate program; subscript out of range
} // end if
return ptr[ index ];
}
int Array::operator()( int index) const {
if ( index < 0 || index >= size )
{
cerr << "\nError: Subscript " << index
<< " out of range" << endl;
exit( 1 ); // terminate program; subscript out of range
} // end if
return ptr[ index ];
}
int &Array::operator()( int index,int index2) {
if ( index < 0 || index2 < 0 || (index*index2) >= size || index > i || index2 > j)
{
cerr << "\nError: Subscript " << index<< "," << index2
<< " out of range" << endl;
exit( 1 ); // terminate program; subscript out of range
} // end if
return ptr[ (index*index2) ];
}
int Array::operator()( int index,int index2)const {
if ( index < 0 || index2 < 0 || (index*index2) >= size || index > i || index2 > j)
{
cerr << "\nError: Subscript " << index << "," << index2
<< " out of range" << endl;
exit( 1 ); // terminate program; subscript out of range
} // end if
return ptr[ (index*index2) ];
}
// overloaded input operator for class Array;
// inputs values for entire Array
void Array::renew(int newI,int newJ) {
if (newI!=i && newJ!=j) {
int *cpy=0;
cpy=new int[size];
for (int i=0;i<size;i++)
cpy[i]=ptr[i];
delete [] ptr; // release space
int lastSize=size;
size = newI*newJ;
bool sze;
if (size>lastSize)
sze=true;
else
sze=false;
Array::i=newI;
Array::j=newJ;// resize this object
ptr = new int[ size ];
for (int i=0;i<(sze?lastSize:size);i++)
ptr[i]=cpy[i];
if(sze) {
for (int i=lastSize;i<size;i++)
ptr[i]=0;
}
delete [] cpy;
}
}
void Array::renew(int newI) {
if (newI!=i) {
int *cpy=0;
cpy=new int[size];// release space
for (int i=0;i<size;i++)
cpy[i]=ptr[i];
delete [] ptr;
int lastSize=size;
size = newI;
bool sze;
if (size>lastSize)
sze=true;
else
sze=false;
Array::i=newI;
ptr = new int[ size ];
for (int i=0;i<(sze?lastSize:size);i++)
ptr[i]=cpy[i];
if(sze) {
for (int i=lastSize;i<size;i++)
ptr[i]=0;
}
delete [] cpy;
}
}
#include<conio.h>
int main()
{
Array Arr(4,2);
Arr(2,1)=1;
Arr.renew(6,4);
cout<<Arr(2,1);
getch();
return 0;
} // end main
این کد , کد تغییر یافته که در کتاب deitel معرفی شد می باشد , من امکان 2 بعدی بودن و تغییر بعد و اندازه را به آن اضافه کردم.