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
|
#include <iostream>
class Matrix
{
private:
int **matrix;
int nRow;
int nCol;
public:
Matrix(const int nRow, const int nCol);
void setElement(const int i, const int j, const int e);
~Matrix();
};
Matrix::Matrix(const int nRow, const int nCol)
{
this->nRow = nRow;
this->nCol = nCol;
matrix = new int *[nRow];
for(int i = 0; i < nRow; i++)
matrix[i] = new int[nCol];
}
void Matrix::setElement(const int i, const int j, const int e)
{
matrix[i][j] = e;
}
Matrix::~Matrix()
{
for(int i = 0; i < nRow; i++)
{
delete[] matrix[i];
std::cout << "Deleting row #" << i << '\n';
}
// delete matrix;
delete[] matrix; // this was alocated using []
std::cout << "Deleting matrix\n";
matrix = 0;
}
int main(int argc, char** argv)
{
Matrix tab(2, 2);
tab.setElement(0, 0, 1);
tab.setElement(0, 1, 2);
tab.setElement(1, 0, 3);
tab.setElement(1, 1, 4);
tab.print_screen();
// No need to delete this, it was not created using new.
//delete tab;
return (EXIT_SUCCESS);
}
|