this code is about a matrix class , but there is an error init . if i compile it
ID: 3615964 • Letter: T
Question
this code is about a matrix class , but there is an error init . if i compile it with Dev c++ complier it always indicate thiserror.#include<iostream.h>
#include<conio.h>
using namespace std;
class matrix
{
int **m;
int row, col;
public:
matrix()
{
row=col=0;
m=NULL;
}
matrix(int r ,int c);
~matrix();
void getmatrix();
void showmatrix();
matrix(matrix &m2); //copy constructor
matrix& operator=(matrix &m2);
};
matrix::~matrix()
{
for(int i=0;i<row;i++)
delete m[i];
delete m;
}
matrix::matrix(int r ,int c)
{
row = r;
col = c;
m = new int*[row];
for(int i=0;i<row;i++)
m[i]=new int[col];
}
matrix::matrix(matrix &m2)
{
cout<<" Copy constructor invoked… ";
row = m2.row;
col = m2.col;
m = new int*[row];
for(int i=0;i<row;i++)
m[i]=new int[col];
for(i=0;i<row;i++)
for(int j=0;j<row;j++)
m[i][j]=m2.m[i][j];
}
matrix& matrix::operator=(matrix &m2)
{
cout<<" Assignment Operator Overloading… ";
row = m2.row;
col = m2.col;
m = new int*[row];
for(int i=0;i<row;i++)
m[i]=new int[col];
for(i=0;i<row;i++)
for(int j=0;j<row;j++)
m[i][j]=m2.m[i][j];
return *this;
}
void matrix::getmatrix()
{
for(int i=0;i<row;i++)
for(int j=0; j<col; j++)
cin>>m[i][j];
}
void matrix::showmatrix()
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
cout<<" "<<m[i][j];
cout<<" ";
}
}
int main()
{
int r,c;
clrscr();
cout<<" Enter rows and cols of the matrix… ";
cin>>r>>c;
matrix m1(r,c);
cout<<" Enter the matrix elements one by one…";
m1.getmatrix();
cout<<" Entered matrix is… ";
m1.showmatrix();
//invoking copy constructor
matrix m2=m1;
cout<<" Result of copy constructor is… ";
m2.showmatrix();
matrix m3;
m3=m1;
cout<<" Result of assignment operatoroverloading… ";
m3.showmatrix();
getch();
}
Explanation / Answer
please rate - thanks a couple of i s were out of scope. I've never gotten clrscr to work, I think its mycompiler. the changes are inred #include #include using namespace std; class matrix { int **m; int row, col; public: matrix() { row=col=0; m=NULL; } matrix(int r ,int c); ~matrix(); void getmatrix(); void showmatrix(); matrix(matrix &m2); //copy constructor matrix& operator=(matrix &m2); }; matrix::~matrix() { for(int i=0;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.