---
title: "Matrix addition by overloading +"
subject: "Object Oriented Programming"
chapter: 5
key: "semester_3/object_oriented_programming:matrix-add-overload"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/matrix-add-overload
site: BCT Engineering
---

# Matrix addition by overloading +

Object Oriented Programming · Chapter 5: Operator Overloading

- Asked in: 2070 Asar, 6 marks
- Video: [matrix addition and subtraction using operator overloading](https://www.youtube.com/watch?v=yD4h4p8aIXY) — Rishabh UR15CS086
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/matrix-add-overload
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-5

## Question

Write a program to add two matrices by overloading the + operator.

## Short answer

Overload + as member function: create a new Matrix, sum corresponding elements of the calling matrix and the argument, return result. Both matrices must have identical rows and columns. Usage: m3 = m1 + m2.

## Answer

The `+` operator is overloaded as a member function of class `Matrix`. The left matrix is the calling object and the right matrix is the argument. The result is a new matrix whose element `[i][j]` is the sum of the corresponding elements. Both matrices must have the same order (rows and columns).

```cpp
#include <iostream>
using namespace std;

class Matrix {
    int a[10][10];
    int r, c;
public:
    void read() {
        cout << "Enter rows and columns: ";
        cin >> r >> c;
        cout << "Enter " << r * c << " elements:" << endl;
        for (int i = 0; i < r; i++)
            for (int j = 0; j < c; j++)
                cin >> a[i][j];
    }

    Matrix operator+(Matrix m) {
        Matrix t;
        t.r = r;
        t.c = c;
        for (int i = 0; i < r; i++)
            for (int j = 0; j < c; j++)
                t.a[i][j] = a[i][j] + m.a[i][j];
        return t;
    }

    void display() {
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++)
                cout << a[i][j] << "\t";
            cout << endl;
        }
    }
};

int main() {
    Matrix m1, m2, m3;
    cout << "First matrix" << endl;
    m1.read();
    cout << "Second matrix" << endl;
    m2.read();
    m3 = m1 + m2;                 // m1.operator+(m2)
    cout << "Sum of matrices:" << endl;
    m3.display();
    return 0;
}
```

**Sample run (2 x 2):**

```text
First matrix
Enter rows and columns: 2 2
Enter 4 elements:
1 2 3 4
Second matrix
Enter rows and columns: 2 2
Enter 4 elements:
5 6 7 8
Sum of matrices:
6	8
10	12
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. In m3 = m1 + m2 with operator+ as a member function, which matrix is the calling object?
   A. m1, the left operand
   B. m2, the right operand
   C. m3, the result
   D. None, it is static
   Answer: A. m1 + m2 is translated to m1.operator+(m2), so the left matrix is the caller and m2 is the argument. (Key point: Calling object)

2. How is element [i][j] of the result computed?
   A. a[i][j] * m.a[i][j]
   B. a[i][j] + m.a[j][i]
   C. Sum of the whole row of a
   D. a[i][j] + m.a[i][j]
   Answer: D. Matrix addition adds corresponding elements. (Key point: Element-wise sum)

3. What condition must the two matrices satisfy to be added?
   A. Both must be square
   B. Both must have the same number of rows and columns
   C. Columns of the first equal rows of the second
   D. Both must have 10 rows
   Answer: B. Addition needs matrices of the same order; the columns-equal-rows rule belongs to multiplication. (Key point: Same order requirement)

4. What does operator+ return in the program?
   A. A new Matrix t holding the sums, with r and c copied
   B. Nothing; it modifies the left matrix
   C. A float
   D. A reference to m2
   Answer: A. A temporary Matrix t with the same r and c stores the sums and is returned. (Key point: Result object)

## Related in Object Oriented Programming

- [City class with operator overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Acity-class-operator-overload) — Object Oriented Programming, chapter 5
- [Matrix multiplication by overloading *](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amatrix-multiply-overload) — Object Oriented Programming, chapter 5
- [Syntax of operator overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoperator-overloading-syntax) — Object Oriented Programming, chapter 5, asked 4×
- [Operator overloading: need and definition](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoperator-overloading-why-define) — Object Oriented Programming, chapter 5, asked 3×
- [Disadvantage of operator overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoperator-overloading-disadvantage) — Object Oriented Programming, chapter 5
- [Binary and unary operator overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Abinary-unary-overloading) — Object Oriented Programming, chapter 5

## Related in other subjects

- [Add two matrices](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Amatrix-add-two) — Computer Programming, chapter 7
- [Add and subtract matrices in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-matrix-add-subtract) — Computer Programming, chapter 11
- [8085 ALP: add two tables, sum and carry](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-add-two-tables) — Microprocessor, chapter 2
- [Array overflow/underflow; add two arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-overflow-underflow-add) — Computer Programming, chapter 7
