Matrix Assignment Operator Overloading and Function Execution Analysis, Assignments of Electrical and Electronics Engineering

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

Pre 2010

Uploaded on 07/23/2009

koofers-user-yr7-1
koofers-user-yr7-1 🇺🇸

8 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1. Is there any error in the following code for assignment operator overloading? If any,
point out the error in the following code, give the reason and fix the error. (10 ptr)
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;
2. The following is a silly function that uses the Matrix class. The function is initially
called as function (a), where a is of type Matrix and has been properly defined
and initialized. Trace the complete execution of this function from the start (line 1) to the
end (line 10) and, for each line of code, write in space beneath it what friend and/or
member function(s). if any, would be called when that line is executed. If more than one
function is called, you must list them in the order that they are called. The relevant
portions of Matrix.h are given below. (Hint: don’t forget about implicit calls to
constructors and destructors.) (10 ptr)
pf3

Partial preview of the text

Download Matrix Assignment Operator Overloading and Function Execution Analysis and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

  1. Is there any error in the following code for assignment operator overloading? If any, point out the error in the following code, give the reason and fix the error. (10 ptr)

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. The following is a silly function that uses the Matrix class. The function is initially called as function (a), where a is of type Matrix and has been properly defined and initialized. Trace the complete execution of this function from the start (line 1) to the end (line 10) and, for each line of code, write in space beneath it what friend and/or member function(s). if any, would be called when that line is executed. If more than one function is called, you must list them in the order that they are called. The relevant portions of Matrix.h are given below. (Hint: don’t forget about implicit calls to constructors and destructors.) (10 ptr)

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&);

  1. c3 = -c; friend Matrix& operator-(const Matrix&); Matrix& operator=(const Matrix&);
  2. c2 = c1 - c; friend Matrix& operator-(const Matrix&, const Matrix&); Matrix& operator=(const Matrix&);
  3. cout << c + c1 + c2 << endl;

friend Matrix& operator+(const Matrix&, const Matrix&); friend Matrix& operator+(const Matrix&, const Matrix&); friend ostream& operator<<(ostream& out, const Matrix&);

  1. return c3;

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.