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

# Matrix multiplication by overloading *

Object Oriented Programming · Chapter 5: Operator Overloading

- Asked in: 2072 Kartik, 7 marks
- Video: [C++ Matrix Class | Binary Operators Overloading | C++ for Beginners | Urdu/Hindi](https://www.youtube.com/watch?v=9GFUrkdhnTM) — CodeIt
- 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-multiply-overload
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-5

## Question

Develop a program using a class with 3×3 matrix as a data member. Overload the * operator so as to multiply two matrices.

## Short answer

For 3×3 Matrix, overload * as member: initialize each element C[i][j] = 0, then sum over k (0 to 2) of A[i][k] * B[k][j]. Return the new Matrix. Usage: C = A * B.

## Answer

The class `Matrix` keeps a 3 x 3 array as its data member. The `*` operator is overloaded as a member function: for the product C = A * B, each element is `C[i][j] = Σ A[i][k] * B[k][j]` for k = 0 to 2.

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

class Matrix {
    int m[3][3];
public:
    void read() {
        cout << "Enter 9 elements (row-wise):" << endl;
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                cin >> m[i][j];
    }

    Matrix operator*(Matrix b) {
        Matrix c;
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++) {
                c.m[i][j] = 0;
                for (int k = 0; k < 3; k++)
                    c.m[i][j] += m[i][k] * b.m[k][j];
            }
        return c;
    }

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

int main() {
    Matrix a, b, c;
    cout << "Matrix A" << endl;  a.read();
    cout << "Matrix B" << endl;  b.read();
    c = a * b;                    // a.operator*(b)
    cout << "A * B =" << endl;
    c.display();
    return 0;
}
```

**Sample run:** with A = [1 2 3; 4 5 6; 7 8 9] and B = identity matrix, the output is

```text
A * B =
1	2	3
4	5	6
7	8	9
```

With B = [1 1 1; 1 1 1; 1 1 1] the output is rows `6 6 6`, `15 15 15`, `24 24 24`.

**Note:** matrix multiplication is not commutative, so the order of the operands matters: the left operand is the invoking object.

## Check yourself

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

1. Which formula gives element C[i][j] of A * B?
   A. A[i][j] * B[i][j]
   B. Sum over k of A[i][k] * B[k][j]
   C. Sum over k of A[k][i] * B[j][k]
   D. A[i][j] + B[j][i]
   Answer: B. Each element is the sum of row i of A times column j of B. (Key point: Product formula)

2. Why does the code set c.m[i][j] = 0 before the k loop?
   A. Because arrays are always zero
   B. To free memory
   C. Because += needs a starting value of 0, otherwise garbage is added
   D. To transpose the matrix
   Answer: C. The element is built with +=, so it must start at 0 or the uninitialised value is added. (Key point: Accumulator initialisation)

3. Why does the order of operands matter in a * b?
   A. Matrix multiplication is not commutative, and the left operand is the invoking object
   B. C++ evaluates right to left
   C. The right operand is always modified
   D. Only square matrices have an order
   Answer: A. A*B differs from B*A in general, and a * b calls a.operator*(b). (Key point: Non-commutative)

4. What is A * B when B is the 3 x 3 identity matrix?
   A. The zero matrix
   B. The transpose of A
   C. A matrix of all ones
   D. A itself
   Answer: D. Multiplying by the identity leaves a matrix unchanged. (Key point: Identity result)

## 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
- [Overload index operator [] for array class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aindex-operator-array-class) — Object Oriented Programming, chapter 5
- [Matrix addition by overloading +](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amatrix-add-overload) — Object Oriented Programming, chapter 5
- [Time class: +, <= and << overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Atime-operator-overload) — Object Oriented Programming, chapter 5
- [Binary operator member function one argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Abinary-member-one-argument) — 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×

## Related in other subjects

- [Stiffness matrix: element matrices, member forces, BMD](https://www.bctengineer.com/question.md?key=semester_5%2Ftheory_of_structures_ii%3Astiffness-element-matrices-600kn-bmd) — Theory of Structures II, chapter 3
- [Significance of arrays; multiply 3×3 matrices](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-significance-multiply-matrices) — Computer Programming, chapter 7
- [Cascaded two-ports: ABCD matrices multiply](https://www.bctengineer.com/question.md?key=semester_3%2Felectrical_circuit_theory%3Acascade-transmission-matrix-product) — Electrical Circuit Theory, chapter 7
- [Steps to develop a computer program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asteps-develop-program) — Computer Programming, chapter 2, asked 6×
