Accumulation, Assignments and Index Operators
Learn how to overload accumulation and indexing operators for the Matrix class.
Introduction to the accumulation operators
An accumulation operator is a mathematical function that takes a sequence of values and produces a running total of those values. It’s also sometimes called a summation operator or cumulative sum.
The most common symbol used to represent the accumulation operator is the Greek capital letter Sigma (
The accumulation operator is used in many areas of mathematics, including calculus, statistics, and computer science. In programming, the accumulation operator is often implemented as a loop that iterates through a sequence of values and updates a running total at each step.
In C++, we perform accumulation using the following operators:
+=
=
*=
/=
We’ll be implementing all of these operators except for the /=
operator. You’ve been provided the playground below. Try to implement it yourself.
Exercise playground
3 2 2 1 2 4 5 2 2 7 8 2 2 5 3 4 6 5 3 3 4 5 8 9 4 5 3 7 2 3
Assume that for these operators, you have the operator+()
, operator()
and operator*()
functions available, and you may use them as helping functions in your code. We’ve deliberately hidden the implementations of these functions in the above playground.
The +=
operator
The only difference between +
and +=
operators is that the +=
operator will update the values of the original matrix with the sum of the two matrices, as shown in the implementation below:
const Matrix& Matrix::operator+=(const Matrix& M){Matrix result;result = (*this) + M;*this = result;return *this;}

Line 1: The
operator+=()
function takes aconst Matrix& M
as a parameter, representing the matrix to be added to the current matrix.const
means that the received parameter should be treated only as “for readonly purposes.” 
Line 3: Inside the function, a new
Matrix
object namedresult
is created to store the result of the addition operation. 
Line 4: The addition operation
(*this) + M
is performed, where(*this)
represents the current matrix object. The result of the addition is assigned to theresult
matrix using the assignment operator (=
). 
Line 5: Next, the current matrix object is assigned the value of the result matrix using the assignment operator i.e.,
*this = result
. This updates the current matrix with the added values. 
Line 6: Finally, the
operator+=()
function returns a reference to the current matrix(*this)
after the addition and assignment operation.
Instruction: Write the above code in the Exercise playground to test the correct output.
The =
operator
The operator=()
function subtracts the elements of one matrix (M
) from another matrix (*this
) and assigns the result back to the current matrix.
It follows a twostep process:

Performing the subtraction operation

Updating the current matrix with the result.
Write your own code in the Exercise playground. If you have trouble getting to the solution, you can click the “Show solution” button to see how to implement it.
The *=
operator
The operator*=()
function multiplies the elements of one matrix (M
) with another matrix (*this
) and assigns the result back to the current matrix.
It follows a twostep process:

Performing the multiplication operation.

Updating the current matrix with the result.
Write your own code in the Exercise playground.
If you have trouble getting to the solution, you can click the “Show solution” button to see how to implement it.
The []
operator
The []
operator is a special operator used to access elements within a class or object as if they were in an array. It allows you to treat an object as a collection or container of elements and provides a convenient way to retrieve or modify individual elements using index notation. We can overload the []
operator this way:
int* Matrix::operator[](int ri)const{if (ri < 0  ri >= this>rows)throw("invalid index");elsereturn this>Vs[ri];}

Line 1 The operator takes an input parameter,
ri
, which represents the row index of the matrix. 
Lines 3–4: Inside the operator, we first check if the given row index is valid. If the row index is less than
0
or greater than or equal to the number of rows in the matrix, an exception is thrown with the messageinvalid index
. 
Lines 5–6: If the row index is valid, we return a pointer to the row of the matrix indicated by
ri
. This allows us to access and manipulate the elements in that row using the subscript operator[]
.
Write the above code in the Exercise playground to enable the matrix with arraylike indexing.
Using the []
operator
Let’s look at a use case of the []
operator below:
Matrix & m = Matrix[0];for(int ri=0; ri<m.rows(); ri++){for(int ci=0; ci<m.cols(); ci++){cout << m[ri][ci] << " ";}cout << "\n";}
Take a look at line 7. It is accessing the matrix with double indexing i.e., m[ri][ci]
, first for a row and then for a specific column. How is it working with only one operator[]
overloaded?
Write the above code in the main()
funtion of the Exercise playground to test the correct output. The code is also written as a comment; you can just uncomment that part.