-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayofArray2D.cpp
More file actions
118 lines (99 loc) · 1.97 KB
/
Copy pathArrayofArray2D.cpp
File metadata and controls
118 lines (99 loc) · 1.97 KB
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
111
112
113
114
115
116
117
118
#include<iostream>
using namespace std;
class Array2D
{
int rows,cols;
int** element;
public:
Array2D(int r=0,int c=0);
Array2D(const Array2D& m);
~Array2D();
int& operator()(int i,int j) const;
friend istream& operator>>(istream& in, Array2D&);
friend ostream& operator<<(ostream& out,Array2D&);
int Rows()const
{
return rows;
}
int Cols()const
{
return cols;
}
};
// Parameterised constructor to allocate memory to array of array of int
Array2D::Array2D(int r,int c)
{
if(r<=0 || c<=0) throw "bad initialisers";
rows=r;
cols=c;
element = new int*[r];
for(int i=0;i<r;i++)
{
element[i]= new int[c];
}
}
// copy constructor
Array2D::Array2D(const Array2D& m)
{
rows= m.Rows();
cols=m.Cols();
element= new int*[rows];
for(int i=0;i<cols;i++)
{
element[i]= new int[cols];
}
//to copy each element
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
element[i][j]= m.element[i][j];
}
}
}
//destructor
Array2D::~Array2D()
{
for(int i=0;i<rows;i++)
delete[] element[i]; // deleting the elemenets stored in each 1d array(row)
delete[] element; // deleting the array of the pointers to the 1-D array
}
// overloading () operator to access the values in the 2D array
int& Array2D::operator()(int i,int j)const
{
if(i<0 || i>=rows || j<0 || j>=cols) throw "Out Of Bound Exception";
return element[i][j];
}
// overloading input operator
istream& operator>>(istream& in,Array2D& m)
{
for(int i=0;i<m.Rows();i++)
for(int j=0;j<m.Cols();j++)
in>>m(i,j);
return in;
}
//overloading output operator
ostream& operator<<(ostream& out,Array2D& m)
{
for(int i=0;i<m.Rows();i++)
for(int j=0;j<m.Cols();j++)
out<<m(i,j);
return out;
}
// driver function
int main()
{
try
{
Array2D obj(2,3);
cin>>obj;
cout<<obj;
Array2D obj2(3,2);
cin>>obj2;
cout<<obj2;
}
catch(const char* str)
{
cout<<str;
}
}