1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
=
#include <iostream>
using namespace std;
typedef double* DoubleArrayPtr;
class TwoD
{
public:
TwoD();
TwoD(int,int);
TwoD(const TwoD& copyMe);//This is a copy constructor,problem ?
TwoD& operator =(const TwoD& rightSide);//Overloaded !
~TwoD();//Destructor,xD,how I love this name
const void setEntry(int theRow,int theCol);
const double returnEntry(int theRow,int theCol) const;
friend const TwoD operator +(const TwoD& anArray1,const TwoD* anArray2);
private:
double *a;
int maxRows;
int maxCols;
};
int main()
{/*
int d1,d2;
cout << "Row -> Column for teh array \n";
cin >> d1 >> d2;
IntArrayPtr *m = new IntArrayPtr[d1];
int i,j;
for(i = 0;i<d1;i++)
m[i] = new int[d2];
cout << "Enter " << d1 << " rows of " << d2 << " intergers each ";
for(i=0;i<d1;i++)
for(j=0;j<d2;j++)
cin >> m[i][j];
cout << "Echo the 2 dim array :";
for(i=0;i<d1;i++)
{
for(j=0;j<d2;j++)
cout << m[i][j] << " ";
cout << endl;
}
for(i=0;i<d1;i++)
delete [] m[i];
delete [] m;
*/
return 0;
}
TwoD::TwoD():maxRows(5),maxCols(5)
{
double* *a = new double*[maxRows];
for (int i = 0; i < maxRows; i++)
a[i] = new double[maxCols];
}
TwoD::TwoD(int no1,int no2):maxRows(no1),maxCols(no2)
{
double* *a = new double*[maxRows];
for (int i = 0; i < maxRows; i++)
a[i] = new double[maxCols];
}
TwoD::~TwoD()
{
for(int i=0;i<maxRows;i++)
delete [] a[i];
delete [] a;
}
TwoD::TwoD(const TwoD& copyMe):maxRows(copyMe.maxRows),maxCols(copyMe.maxCols)
{
a = new double[maxRows];
for(int i=0;i<maxRows;i++)
a[i] = new double[maxCols];
for(int i=0;i<maxRows;i++)
for(int j=0;j<maxCols;j++)
a[i][j] = copyMe.a[i][j];
}
TwoD& TwoD::operator =(const TwoD& rightSide)
{
if(maxRows != rightSide.maxRows || maxCols != rightSide.maxCols)
{
for(int i=0;i<maxRows;i++)
for(int j=0;j<maxCols;j++)
delete [] a[i];
delete [] a;
a = new double[maxRows];
for(int i=0;i<maxRows;i++)
a[i] = new double[maxCols];
}
}
const void setEntry(int theRow,int theCol)
{
double value;
cout << "maxrow = " << maxRows << " maxcol = " << maxCols << endl;
cin >> value;
a[theRow][theCol]=value;
}
|