C++源码:动态数组实例(2维)
// C++源码:动态数组实例(2维) Example for DYN ARRAY Class (2d)
//
// ARRAY
#include <iostream.h>
#include <stdlib.h>
const int defau_m=3,defau_n=3;
class IntArray
{
protected:
int size,m,n; //Array size,
int **pia;
public:
IntArray () { }; //constructor 1 (default,inline)
IntArray (int mm=defau_m,int nn=defau_n); //Constructor 2
IntArray (const IntArray& ); //Constructor 3
~IntArray (); //析构函数
IntArray& operator= (IntArray &); //reference: for left value
IntArray operator+ (IntArray &); // if func use &type is also right: left value
// IntArray operator- (IntArray &); // if func use &type is also right: left value
// IntArray operator* (const IntArray &); // if func use &type is also right: left value
// int& operator[] (int,int);
// int getsize () {return size;}
void assign (int value,int index1,int index2);
void chang_s(int mm,int nn);
void show1 (int index1,int index2);
void show ();
}; // End of Class
IntArray::IntArray(int mm,int nn) // constuctor 2
{
m=mm; n=nn; size=m*n;
pia=new int*[m]; // m=rows
for (int i=0;i<m;i++) // n=
pia[i]=new int [n]; //
for (i=0;i<m;i++) // initalize
for (int j=0;j<n;++j)
pia[i][j]=0;
}
IntArray::IntArray(const IntArray &PA) // constuctor 3 (COPY CONSTRUCTOR)
{
m=PA.m; n=PA.n;
size=m*n;
/*
for (int i=0;i<m;i++) //
delete []pia[i];
delete []pia;
//delete pia;
*/
pia=new int*[m]; // m=rows
for (int i=0;i<m;i++) // n=
pia[i]=new int [n]; //
for (i=0;i<m;i++) // initalize
for (int j=0;j<n;++j)
pia[i][j]=0;
// COPY
for (i=0;i<m;i++)
for (int j=0;j<n;j++) //
pia[i][j]=PA.pia[i][j]; // COPY
}
IntArray::~IntArray () //
{
for (int i=0;i<m;i++)
delete []pia[i];
delete []pia;
}
IntArray& IntArray::operator=(IntArray &PA)
{
for (int i=0;i<m;i++) // Free memery
delete []pia[i]; //
delete []pia; //
int **tempP;
m=PA.m; n=PA.n; // Begin operate
size=PA.size;
tempP=new int* [m]; //
for (i=0;i<m;i++) //alloc memery
tempP[i]=new int[n]; //tempP[i]=new int(n); is NOT right ! //
pia=tempP;
/// for (i=0;i<m;i++)
for (i=0;i<m;i++)
for (int j=0;j<n;j++)
pia[i][j]=PA.pia[i][j];
return *this;
}
// Overload "+"
IntArray IntArray::operator+(IntArray &PA)
{
IntArray tempA(m,n);
for (int i=0;i<m;i++)
for (int j=0;j<n;j++)
tempA.pia[i][j]=pia[i][j]+PA.pia[i][j];
return tempA;
}
/* // Overload "-"
IntArray IntArray::operator-(const IntArray &PA)
{
IntArray tempA;
tempA.m=PA.m; tempA.n=PA.n; // initalize !!
tempA.size=PA.size; // initalize !!
if (size!=PA.size)
{
cout<<"Can not sub !\n";
return 1;
}
for (int i=0;i<m;i++)
for (int j=0;j<n;j++)
tempA.pia[i][j]=pia[i][j]-PA.pia[i][j];
return tempA;
}
*/
// Assign ARRAY element a value
void IntArray::assign(int value,int index1,int index2)
{
pia[index1][index2]=value;
}
// chang ARRAY size
void IntArray::chang_s(int mm,int nn) //
{
for (int i=0;i<m;i++)
delete []pia[i];
delete []pia;
m=mm; n=nn; size=m*n;
int **tempPi;
tempPi=new int*[m]; // m=rows
for ( i=0;i<m;i++) // n=
tempPi[i]=new int (n); // "tempPi" piont to a 2 ARRZAY
pia=tempPi;
for (i=0;i<m;i++) // initalize
for (int j=0;j<n;j++)
pia[i][j]=0;
}
void IntArray::show1(int index1,int index2)
{
cout<<pia[index1][index2]<<" ";
}
void IntArray::show()
{
cout<<"size="<<size<<"\n";
for (int i=0;i<m;i++)
{
for (int j=0; j<n;++j)
cout<<pia[i][j]<<" ";
cout<<"\n";
}
cout<<"\n\n"; // END MATIX SHOW
}
main()
{
int m1=1,n1=1,value1,ii,jj;
IntArray A1,A2,A3,A4,A5;
while (m1!=0)
{
// Beginning of (Input a ARRAY, and display it:)
cout<<"\007"<<"Please input the ARRAY1 size:"<<flush;
cin>>m1>>n1;
// PROCESS USER WANT TO EXIT (1)
if ((m1==0)||(n1==0))
{
cout<<" \n\n You want to EXIT !\n BeCAUSE YOU INPUT 0 0";
return 1;
}
A1.chang_s(m1,n1); //Creat a ARRAY, and define it's size = m1*n1
m1=m1+1; n1=n1+1;
for (int i=1;i<m1;i++)
{
for (int j=1;j<n1;++j)
{
cout<<" ARRAY["<<i<<","<<j<<"]="<<flush;
cin>>value1;
ii=i;jj=j;
A1.assign(value1,(ii-1),(jj-1));
}
cout<<"\n";
}
cout<<"\n Call A1.show()\n";
A1.show();
//END OF (Input a ARRAY, and display it:)
// Beginning of (Input a ARRAY, and display it:)
cout<<"\007"<<"Please input the ARRAY2 size:"<<flush;
cin>>m1>>n1;
// PROCESS USER WANT TO EXIT (1)
if ((m1==0)||(n1==0))
{
cout<<" \n\n You want to EXIT !\n BeCAUSE YOU INPUT 0 0";
return 1;
}
A2.chang_s(m1,n1); //Creat a ARRAY, and define it's size
m1=m1+1; n1=n1+1;
for (i=1;i<m1;i++)
{
for (int j=1;j<n1;++j)
{
cout<<" ARRAY["<<i<<","<<j<<"]="<<flush;
cin>>value1;
ii=i; jj=j;
A2.assign(value1,(ii-1),(jj-1));
}
cout<<"\n";
}
A2.show();
//END OF (Input a ARRAY, and display it:)
A3=A1+A2;
// A4=A2-A1;
// A5=A1*A2;
A3.show();
// A4.show();
// A5.show();
} //end of loop {while}
} // end of main()
//注意: 拷贝构造函数需要为当前对象申请空间 !