Matrix Multiplication in C++ using (*) Operator Overloading

Posted By: Java Examples - 7:07 AM

Share

& Comment

We will now try to multiply two matrix by using the concept of operator overloading. The described way is very very easy to understand. We will be overloading "*" operator for this purpose. Although this can be done by any binary operator.
We will start with a basic class Matrix. In this example we will take a 3x3 matrix.

Description:
  • class Matrix has a constructor that will initialize its element to 0.
  • also has a set() function that can be used to set values in the Matrix.
  • A show() function shows the matrix.
  • operator overloading definition for "*" operator

C programming code:

Output of program :

Enter 0,0 element=1

 Enter 0,1 element=2

 Enter 0,2 element=3

 Enter 1,0 element=4

 Enter 1,1 element=5

 Enter 1,2 element=6

 Enter 2,0 element=7

 Enter 2,1 element=8

 Enter 2,2 element=9

 Enter 0,0 element=1

 Enter 0,1 element=2

 Enter 0,2 element=3

 Enter 1,0 element=4

 Enter 1,1 element=5

 Enter 1,2 element=6

 Enter 2,0 element=7

 Enter 2,1 element=8

 Enter 2,2 element=9

 Matrix is=
1,2,3,
4,5,6,
7,8,9,

 Matrix is=
1,2,3,
4,5,6,
7,8,9,

 Matrix is=
30,36,42,
66,81,96,
102,126,150,
Press any key to continue . . .

About Java Examples

I’m passionate about Web Development and Programming and I go to extreme efforts to meet my passion. I’m a believer of learning the fundamentals first. I try to understand everything little bit more than the average.

9 comments :

Post a Comment

Copyright © 2016 C++ Examples ACHCHUTHAN.ORG. Designed by Templateism .