Important Introduction to Programming Short Questions
Chapter: 44
Question 1:
What is the purpose of the constructor in the Matrix class?
Answer:
The constructor initializes the matrix's dimensions and allocates memory for storing the matrix elements. It sets up the matrix with specified rows and columns.
Chapter: 44
Question 2:
How is dynamic memory allocated for the matrix elements in the Matrix class?
Answer:
Memory is allocated using new for a two-dimensional array of double. Pointers are used to create the rows, and each row is an array of double.
Chapter: 44
Question 3:
What is the role of the copy constructor in the Matrix class?
Answer:
The copy constructor creates a new Matrix object as a copy of an existing Matrix, ensuring a deep copy of the matrix elements.
Chapter: 44
Question 4:
Why is a destructor needed in the Matrix class?
Answer:
A destructor deallocates memory previously allocated with new to prevent memory leaks when the Matrix object is destroyed.
Chapter: 44
Question 5:
What is the difference between the member and friend operators in the Matrix class?
Answer:
Member operators act on the object of the class (e.g., Matrix + Matrix), while friend operators can access private members and are not part of the class (e.g., double + Matrix).
Chapter: 44
Question 6:
How does the operator+ function work for adding a double to a matrix?
Answer:
Matrix operator + (double d) const adds a double value to each element of the matrix, returning a new Matrix with the updated values.
Chapter: 44
Question 7:
What does the transpose function do in the Matrix class?
Answer:
The transpose function returns a new Matrix with rows and columns swapped, effectively flipping the matrix over its diagonal.
Chapter: 44
Question 8:
How are the getRows and getCols functions defined in the Matrix class?
Answer:
They are utility functions that return the number of rows and columns of the matrix, respectively, and are marked as const because they do not modify the matrix.
Chapter: 44
Question 9:
What is the purpose of the operator= function in the Matrix class?
Answer:
It is the assignment operator that performs a deep copy of one Matrix to another, ensuring that each matrix has its own independent copy of the data.
Chapter: 44
Question 10:
How does the Matrix operator* function handle matrix multiplication?
Answer:
Matrix operator* (const Matrix & m) multiplies two matrices and returns a new Matrix with the result.