-
Notifications
You must be signed in to change notification settings - Fork 2
/
matrix.h
74 lines (62 loc) · 1.71 KB
/
matrix.h
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
#ifndef MATRIX_H
#define MATRIX_H
class iMatrix {
public:
iMatrix(); //empty constructor
iMatrix(unsigned rows, unsigned cols);
int& operator() (unsigned row, unsigned col);
int operator() (unsigned row, unsigned col) const;
void resize(unsigned rows, unsigned cols);
~iMatrix(); // Destructor
// iMatrix(const iMatrix& m); // Copy constructor
iMatrix& operator= (const iMatrix& m); // Assignment operator
private:
unsigned rows_, cols_;
int* data_;
};
//---------------------------------------------------------
// this is matrix.cpp. add: #include "matrix.h" if seperate file
inline
iMatrix::iMatrix(){
rows_=0, cols_=0; //empty
}
inline
iMatrix::iMatrix(unsigned rows, unsigned cols)
: rows_ (rows), cols_ (cols)
//data_ <--initialized below (after the 'if/throw' statement)
{
if (rows == 0 || cols == 0)
cout<<"iMatrix constructor has 0 size"<<endl;
data_ = new int[rows * cols];
}
inline
void iMatrix::resize(unsigned rows, unsigned cols){
if (rows == 0 || cols == 0)
cout<<"iMatrix constructor has 0 size"<<endl;
if (rows_ != 0 || cols_ != 0)
delete[] data_;
rows_=rows;
cols_=cols;
data_ = new int[rows * cols];
}
inline
iMatrix::~iMatrix()
{
if (rows_ != 0 || cols_ != 0)
delete[] data_;
}
inline
int& iMatrix::operator() (unsigned row, unsigned col)
{
if (row >= rows_ || col >= cols_)
cout<<"iMatrix subscript out of bounds"<<endl;
return data_[cols_*row + col];
}
inline
int iMatrix::operator() (unsigned row, unsigned col) const
{
if (row >= rows_ || col >= cols_)
cout<<"const iMatrix subscript out of bounds"<<endl;
return data_[cols_*row + col];
}
#endif