#include<iostream>
#include<ctime>
usingnamespace std;
class Matrix
{
public:
Matrix();
Matrix(int r, int c);//----------overloaded
Matrix(const Matrix &obj);
~Matrix();
//
Matrix Add(Matrix m);
Matrix Subtract(Matrix m);
void input();
void Print_Matrix();
void Print();
private:
int** p;
int rows;
int cols;
};
Matrix::Matrix()
{
rows = 2;
cols = 2;
p = newint*[2];
for (int i = 0; i < 2; i++)
p[i] = newint[2];
}
Matrix::Matrix(int r, int c)
{
rows = r;
cols = c;
p = newint*[rows];
for (int i = 0; i < rows; i++)
p[i] = newint[cols];
}
Matrix::Matrix(const Matrix &obj)
{
this->rows = obj.rows;
cols = obj.cols;
this->p = newint*[obj.rows];
for (int i = 0; i < obj.rows; i++)
p[i] = newint[obj.cols];
for (int i = 0; i < obj.rows; i++)
{
for (int j = 0; j < obj.cols; j++)
p[i][j] = obj.p[i][j];
}
}
Matrix Matrix::Add(Matrix m)
{
Matrix P(rows, cols);
if (rows == cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
P.p[i][j] = this->p[i][j] + m.p[i][j];
}
}
return P;
}
else
{
cout << "CAN'T BE ADDED\n";
return P;
}
}
Matrix Matrix::Subtract(Matrix m)
{
Matrix Q(rows, cols);
if (rows == cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Q.p[i][j] = this->p[i][j] - m.p[i][j];
}
}
return Q;
}
else
{
cout << "CAN'T BE SUBTRACTED\n";
return Q;
}
}
void Matrix::input()
{
cout << "INPUT :\n";
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
this->p[i][j] = rand() % 10 + 1;
}
}
}
void Matrix::Print_Matrix()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << "i: " << i << endl << "j: " << j << endl;
cout << this->p[i][j] << " ";
}
cout << endl;
}
}
Matrix::~Matrix()
{
for (int i = 0; i < rows; i++)
{
delete p[i];
}
delete[] p;
}
int main()
{
int rows = 0, cols = 0;
cout << "ENTER ROWS AND COLUMNS :";
cin >> rows >> cols;
if (rows == cols)
{
Matrix p1(rows, cols);
Matrix p2(p1), p3(p1);
cout << "ENTER FOR 1st:\n";
p1.input();
cout << endl;
p1.Print_Matrix();
cout << "ENTER FOR 2nd :\n ";
p2.input();
cout << endl;
p2.Print_Matrix();
cout << "ADDED :\n";
p3 = p1.Add(p2);
cout << endl;
p3.Print_Matrix();
cout << endl;
cout << "SUBTRACTED :\n";
p3 = p1.Subtract(p2);
cout << endl;
p3.Print_Matrix();
}
else
{
cout << "SIZES ARE DIFFERENT";
return 0;
}
getchar();
return 0;
}
What it is doing is this that it is calling the destructor of Local Variable (P) made in "Add" function BEFORE copying the values of "P" in "p3" in int main...
I want to store the result of the add function in my int main object "p3" but because "P"'s destructor is already called... the result don't get stored and so, the compiler is giving error when it tries to print values of "p3".
What it is doing is this that it is calling the destructor of Local Variable (P) made in "Add" function BEFORE copying the values of "P" in "p3" in int main...
I want to store the result of the add function in my int main object "p3" but because "P"'s destructor is already called... the result don't get stored and so, the compiler is giving error when it tries to print values of "p3".