

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The code for matrix assignment operator overloading and analyzes the execution of a function that uses the matrix class. The assignment operator overloading code is reviewed for potential errors, and the function execution is traced line by line, identifying the friend and member functions called at each step.
Typology: Assignments
1 / 3
This page cannot be seen from the preview
Don't miss anything!


Matrix& Matrix::operator=(const Matrix& m) { if (element != m.element) /* check if matrix is being assigned to itself / { row_dim = m.row_dim; col_dim = m.col_dim; element = new T [row_dim]; for (int i=0; i<row_dim; i++) element[i] = new T[col_dim]; for (int i=0; i<row_dim; i++) for (int j=0; j<col_dim; j++) element[i][j] = m.element[i][j]; } return *this; }
Before the assignment operation, the old matrix has to be destroyed, otherwise, it will generate “memory leak”. Insert the following statements at the location showing here. for (int i=0; i<row_dim; i++) // get rid of the old matrix delete [] element[i]; delete [] element;
1 Matrix function (Matrix c){
copy constructor , Matrix(const Matrix&);
2 Matrix c1(c); copy constructor , Matrix(const Matrix&);
3 Matrix c2, c3(4,7); default constructor, Matrix(); Matrix(int rdim, int cdim); //constructor
4 c2 = c; Matrix& operator=(const Matrix&);
5 cout << c2 << endl; friend ostream& operator<<(ostream& out, const Matrix&);
friend Matrix& operator+(const Matrix&, const Matrix&); friend Matrix& operator+(const Matrix&, const Matrix&); friend ostream& operator<<(ostream& out, const Matrix&);
copy constructor , Matrix(const Matrix&);
Matrix c3 is returned to the calling program, a copy of Matrix c3 object is put on the run-time stack. Pop stack and return to the calling program. A copy constructor is called.
Matrix c is passed to function() by value, a local copy of Matrix c object is put on the run-time stack. A copy constructor is called.