In C++, the "+" operator is overloaded (as member operator function) in user…
2022
In C++, the "+" operator is overloaded (as member operator function) in user defined class Matrix that adds two matrices and returns resultant matrix.
Which of the following is a signature of this operator function?
- A.
Matrix operator + (Matrix A, Matrix B)
- B.
Matrix operator + (Matrix B)
- C.
Matrix + operator (Matrix A, Matrix B)
- D.
Void + operator (Matrix B)
Attempted by 101 students.
Show answer & explanation
Correct answer: B
For a binary operator overloaded as a member function, the left operand is the object that calls the function and the right operand is the single explicit parameter. So A + B is internally treated as A.operator+(B). The function must return a Matrix because adding two matrices gives another Matrix. Therefore, the correct signature is Matrix operator+(Matrix B). Hence, Option B is correct.