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 :

  1. #include
    using namespace std;
    #define max 50
    #include
    class Matrix
    {
    public :
    int row,col;
    int A[max][max];
    Matrix(int r,int c);
    void get();
    void input();
    void print();
    Matrix operator ==(Matrix);
    Matrix operator *(Matrix b);
    void prin();
    };

    Matrix::Matrix(int r,int c)
    {
    row=r;
    col=c;

    }

    void Matrix::get()
    {
    int r,c;
    cout << " enter number of rows " <> r;
    cout << " enter number of columns " <> c;
    row=r;
    col=c;

    }
    void Matrix::input()
    {
    for(int i=0;i<row;i++)
    {
    for(int j=0;j<col;j++)
    {
    cout<<"\n Enter "<<i<<","<<j<<" element=";
    cin>>A[i][j];
    }
    }
    }

    void Matrix::print()
    {
    cout<<" Elements is=\n";
    for(int i=0;i<row;i++)
    {
    for(int j=0;j<col;j++)
    {

    cout<<A[i][j]<<",";
    }
    cout<<"\n";
    }
    }

    Matrix Matrix::operator==(Matrix X)
    {
    try
    {
    if (col!=X.row)
    {
    throw(row);
    }
    }
    catch(int row)
    {
    cout<<"Matrix A row = "<<row<<" Matrix B coloum = "<<X.col<<" Not equlal"<<endl;
    system("pause");
    }
    }



    Matrix Matrix::operator*(Matrix X)
    {
    Matrix C(row,X.col);
    for(int i=0;i<row;i++)//row
    {
    for(int j=0;j<X.col;j++)//X.col
    {
    C.A[i][j]=0;
    for(int k=0;k<col;k++)//col
    {
    C.A[i][j]=C.A[i][j]+(A[i][k]*X.A[k][j]);
    }
    }
    }
    return(C);
    }


    void main()
    {
    Matrix A(3,2),B(2,3),C(3,3);
    cout<<"Matrix A"<<endl;
    A.get();
    cout<<"Matrix B"<<endl;
    B.get();

    cout<<"Matrix A"<<endl;
    A.input();
    cout<<"Matrix B"<<endl;
    B.input();

    cout<<"Matrix A"<<endl;
    A.print();
    cout<<"Matrix B"<<endl;
    B.print();

    C=A==B;
    C=A*B;
    cout<<"Multiplication of A and B equals C"<<endl;
    C.print();
    system("pause");
    }

    ReplyDelete
  2. class Matrix
    {
    int A[10][10];
    int m,n;
    public:
    Matrix(int a,int b)
    {
    m = a;
    n = b;
    }

    void readmatrix();
    void printmatrix();
    void addmatrix(Matrix b);
    Matrix operator * (Matrix b);
    };
    void Matrix::readmatrix()
    {
    for(int i=0;i< m;i++)
    {
    for(j=0;j< n;j++)
    cin>>A[i][j];
    }
    }
    }
    void Matrix::printmatrix()
    {
    for(int i=0;i< m;i++)
    {
    for(int j=0;j< n;j++)
    {
    cout< < A[i][j]<<" ";
    }
    cout< < endl;
    }
    }
    void Matrix::addmatrix(Matrix b)
    {
    Matrix c(m,n);
    for(int i=0;i< m;i++)
    {
    for(int j=0;j< n;j++)
    {
    c.A[i][j]=A[i][j]+b.A[i][j];
    }
    }
    cout< < "The Addition Of The Two Matrices Is:"< < endl; c.printmatrix();
    }

    Matrix Matrix::operator*(Matrix b)
    {
    Matrix c(m,m);
    for(int i=0;i< m;i++)
    {
    for(int k=0;k< m;k++)
    {
    c.A[i][k]=0;
    for(int j=0;j< n;j++)
    {
    c.A[i][k] = A[i][j] * b.A[j][k] + c.A[i][k];
    }
    }
    }
    return c;
    }
    void main()
    {
    clrscr();
    cout< < "Enter Order The Two Square Matrices: " ;
    int a;
    cin>>a;

    Matrix x(a,a);
    Matrix y(a,a);

    cout< < endl< < "Enter Elements In First Matrix : ";
    x.readmatrix();

    cout< < endl< < "Enter Elements In The Second Matrix :";
    y.readmatrix();

    cout< < endl< < "The First Matrix:"< < endl;
    x.printmatrix();

    cout< < endl< < "The Second Matrix:"< < endl;
    y.printmatrix();
    x.addmatrix(y);

    Matrix c(a,a);
    c = x * y;

    cout< < "The Multiplication Of The Two Matrices Are:"< < endl;
    c.printmatrix();
    getch();
    }

    /****** OUTPUT *******
    Enter Order The Two Square Matrices: 3

    Enter Elements In First Matrix : 1 0 0 0 1 0 0 0 1

    Enter Elements In The Second Matrix :1 0 0 0 1 0 0 0 1

    The First Matrix:
    1 0 0
    0 1 0
    0 0 1

    The Second Matrix:
    1 0 0
    0 1 0
    0 0 1
    The Addition Of The Two Matrices Is:
    2 0 0
    0 2 0
    0 0 2
    The Multiplication Of The Two Matrices Are:
    1 0 0
    0 1 0
    0 0 1
    */

    ReplyDelete
  3. #include

    using namespace std;

    class matrix
    {

    public:
    int a[3][3];

    matrix()//default constructor
    {
    for(int i=0;i<3;i++)
    {
    for(int j=0;j<3;j++)
    {
    a[i][j]=0;
    }
    }
    }
    void set()// to set matrix elements
    {
    for(int i=0;i<3;i++)
    {
    for(int j=0;j<3;j++)
    {
    cout<<"\n Enter "<<i<<","<<j<<" element=";
    cin>>a[i][j];
    }
    }
    }
    void show()// to show matrix elements
    {
    cout<<"\n Matrix is=\n";
    for(int i=0;i<3;i++)
    {
    for(int j=0;j<3;j++)
    {

    cout<<a[i][j]<<",";
    }
    cout<<"\n";
    }
    }
    /*
    binary * operator will require one more arg, since it is a binary operator
    one arg is the object itself that will call it, other will be passed as arg(in this case x). also this will return a matrix object
    */
    matrix operator*(matrix x)// overloading * for multiplication
    {
    matrix c;// this will hold our result
    for(int i=0;i<3;i++)
    {
    for(int j=0;j<3;j++)
    {
    c.a[i][j]=0;
    for(int k=0;k<3;k++)
    {
    c.a[i][j]=c.a[i][j]+a[i][k]*x.a[k][j];
    }
    }
    }
    return(c);

    }
    };

    int main()
    {
    matrix a,b,c;
    a.set();
    b.set();
    c=a*b;
    /*
    note that compiler will break this statement as
    c=a.operator*(b);
    this is how 2nd arg is passed.
    and this is how object "a" acts as the calling object
    */

    a.show();
    b.show();
    c.show();
    }

    ReplyDelete
  4. using pointer

    #include

    class matrix

    {

    int **A; // pointer to store 2 dimensional array

    int x, y;



    public:

    matrix();

    matrix(int, int);

    void read();

    void input();

    void display();

    matrix operator* (int);


    };

    matrix :: matrix()

    {

    x = 0;

    y = 0;

    }



    matrix :: matrix(int a, int b)

    {

    x = a;

    y = b;

    }



    void matrix :: read()

    {

    std::cout << " enter number of rows " << std::endl;

    std::cin >> x;

    std::cout << " enter number of columns " << std::endl;

    std::cin >> y;

    }



    void matrix :: input()

    {

    int num1, num2, k;

    int i, j;

    A = new int *[x];

    for (k = 0; k < x; k++)
    A[k] = new int [y];

    for (num1=0; num1> A[i][j];

    }

    }

    }



    void matrix :: display()

    {

    int i, j;

    for (i=0; i<x; i++)

    {

    for (j=0; j<y; j++)

    {

    std::cout << " " << A[i][j];

    }

    std::cout << "\n";

    }

    }



    matrix matrix :: operator* (int a)

    {

    matrix temp(x, y);

    int num,num1, num2;

    A = new int *[temp.x];

    for (num=0; num<=temp.x; num++)

    {

    A[num] = new int [temp.y];

    }

    for (num1=0; num1<temp.x; num1++)

    {

    for (num2=0; num2<temp.y; num2++)

    {

    temp.A[num1][num2] = 0;

    }

    }

    int i, j;

    for ( i = 0; i < x; i++)

    {

    for ( j = 0; j < y; j++)

    {

    temp.A[i][j] = a * A[i][j];

    }

    }
    return (temp);

    }







    int main ()

    {

    matrix M1, M2;

    M1.read ();

    M1.input ();

    M1.display ();

    M2 = M1*2;

    M2.display();


    return 0;

    }

    ReplyDelete
  5. simple multiplication

    void MultiplyWithOutAMP() {

    int aMatrix[3][2] = {{1, 4}, {2, 5}, {3, 6}};
    int bMatrix[2][3] = {{7, 8, 9}, {10, 11, 12}};
    int product[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};

    for (int row = 0; row < 3; row++) {
    for (int col = 0; col < 3; col++) {
    // Multiply the row of A by the column of B to get the row, column of product.
    for (int inner = 0; inner < 2; inner++) {
    product[row][col] += aMatrix[row][inner] * bMatrix[inner][col];
    }
    std::cout << product[row][col] << " ";
    }
    std::cout << "\n";
    }
    }

    ReplyDelete
  6. I am having a problem with overloading an operator within a class structure. Below is
    the program that illustrates my problem. It defines a class Matrix, creates two matrices
    and multiplies them.

    I created two multiplication methods. The first is a straight function, the second uses
    the overloaded * operator. The two methods produce different results.
    Code:

    #include

    class Matrix
    {
    private:
    int ROWS, COLUMNS;
    double *M;
    public:
    Matrix() { }; //default constructor
    Matrix(int R, int C){ROWS=R; COLUMNS=C; M=new double[ROWS*COLUMNS];}; //constructor
    ~Matrix() {delete[] M;}; // destructor
    int rows() {return ROWS;}
    int cols() {return COLUMNS;}
    void set(int R, int C) {ROWS=R; COLUMNS=C; M=new double[ROWS*COLUMNS];};
    void m (int R, int C, double V) {M[R*COLUMNS+C]=V;};
    double m (int R, int C) {return M[R*COLUMNS+C];};
    void dump() {for (int r=0; r<ROWS; r++) {for (int c=0; c<COLUMNS; c++)
    cout << M[r*COLUMNS+c]<< " "; cout << endl;};};
    void dump(fstream &fout) {for (int i=0; i<(ROWS*COLUMNS); i++)
    {fout << M[i]; if (i!=(ROWS*COLUMNS-1)) fout << ", ";};};
    Matrix operator * (Matrix &);
    };

    Matrix Matrix::operator* (Matrix &B)
    {
    Matrix C;
    double ans;
    C.set(rows(), B.cols());
    for (int r=0; r<rows(); r++)
    for (int c=0; c<B.cols(); c++)
    {
    ans=0;
    for (int i=0; i<cols(); i++)
    ans=m(r,i)*B.m(i,c)+ans;
    C.m(r,c,ans);
    };
    return(C);
    };

    void multiply(const Matrix &A, const Matrix &B, Matrix &C) //This Works!! Or at least seems to
    {
    double ans;
    C.set(A.rows(), B.cols());
    for (int r=0; r<A.rows(); r++)
    for (int c=0; c<B.cols(); c++)
    {
    ans=0;
    for (int i=0; i<A.cols(); i++)
    ans=A.m(r,i)*B.m(i,c)+ans;
    C.m(r,c,ans);
    };
    };


    void main()
    {
    Matrix A(4,3);
    Matrix B(3,6);
    Matrix C;

    A.m(0,0,11); A.m(0,1,12); A.m(0,2,13);
    A.m(1,0,21); A.m(1,1,22); A.m(1,2,23);
    A.m(2,0,31); A.m(2,1,32); A.m(2,2,33);
    A.m(3,0,41); A.m(3,1,42); A.m(3,2,43);

    B.m(0,0,11); B.m(0,1,21); B.m(0,2,31); B.m(0,3,41);B.m(0,4,51);B.m(0,5,61);
    B.m(1,0,12); B.m(1,1,22); B.m(1,2,32); B.m(1,3,42);B.m(1,4,52);B.m(1,5,62);
    B.m(2,0,13); B.m(2,1,23); B.m(2,2,33); B.m(2,3,43);B.m(2,4,53);B.m(2,5,63);

    C=A*B;
    A.dump(); cout << endl;
    B.dump(); cout << endl;
    C.dump(); cout << endl;

    multiply(A,B,C);
    A.dump(); cout << endl;
    B.dump(); cout << endl;
    C.dump(); cout << endl;

    cout << "Done.\n";
    }

    ReplyDelete
  7. I have given the program for matrix addition, subtraction, multplication and comparison. The class CMatrix is using operator overloading *, =, ==, >> and << along with copy constructor.

    ReplyDelete
  8. #include
    using namespace std;
    class Matrix
    {
    int A[10][10];
    int m,n;
    public:
    Matrix(int a,int b)
    {
    m = a;
    n = b;
    }

    void readmatrix();
    void printmatrix();
    void addmatrix(Matrix b);
    Matrix operator * (Matrix b);
    };
    void Matrix::readmatrix()
    {
    for(int i=0;i< m;i++)
    {
    for(int j=0;j< n;j++)
    cin>>A[i][j];
    }
    }

    void Matrix::printmatrix()
    {
    for(int i=0;i< m;i++)
    {
    for(int j=0;j< n;j++)
    {
    cout<< A[i][j]<<" ";
    }
    cout<< endl;
    }
    }
    void Matrix::addmatrix(Matrix b)
    {
    Matrix c(m,n);
    for(int i=0;i< m;i++)
    {
    for(int j=0;j< n;j++)
    {
    c.A[i][j]=A[i][j]+b.A[i][j];
    }
    }
    cout<< "The Addition Of The Two Matrices Is:"<< endl; c.printmatrix();
    }

    Matrix Matrix::operator*(Matrix b)
    {
    Matrix c(m,m);
    for(int i=0;i< m;i++)
    {
    for(int k=0;k< m;k++)
    {
    c.A[i][k]=0;
    for(int j=0;j< n;j++)
    {
    c.A[i][k] = A[i][j] * b.A[j][k] + c.A[i][k];
    }
    }
    }
    return c;
    }
    int main()
    {
    //clrscr();
    cout<< "Enter Order The Two Square Matrices: " ;
    int a;
    cin>>a;

    Matrix x(a,a);
    Matrix y(a,a);

    cout<< endl<< "Enter Elements In First Matrix : ";
    x.readmatrix();

    cout<< endl<< "Enter Elements In The Second Matrix :";
    y.readmatrix();

    cout<< endl<< "The First Matrix:"<< endl;
    x.printmatrix();

    cout<< endl<< "The Second Matrix:"<< endl;
    y.printmatrix();
    x.addmatrix(y);

    Matrix c(a,a);
    c = x * y;

    cout<< "The Multiplication Of The Two Matrices Are:"<< endl;
    c.printmatrix();
    system("pause");
    }

    ReplyDelete
  9. #include
    using namespace std;
    class Matrix
    {
    int A[10][10];
    int m,n;
    public:
    Matrix(int a,int b)
    {
    m = a;
    n = b;
    }

    void readmatrix();
    void printmatrix();
    void addmatrix(Matrix b);
    void submatrix(Matrix aa);
    };
    void Matrix::readmatrix()
    {
    for(int i=0;i< m;i++)
    {
    for(int j=0;j< n;j++)
    cin>>A[i][j];
    }
    }

    void Matrix::printmatrix()
    {
    for(int i=0;i< m;i++)
    {
    for(int j=0;j< n;j++)
    {
    cout<< A[i][j]<<" ";
    }
    cout<< endl;
    }
    }
    void Matrix::addmatrix(Matrix b)
    {
    Matrix c(m,n);
    for(int i=0;i< m;i++)
    {
    for(int j=0;j< n;j++)
    {
    c.A[i][j]=A[i][j]+b.A[i][j];
    }
    }
    cout<< "The Addition Of The Two Matrices Is:"<< endl; c.printmatrix();
    }
    void Matrix::submatrix(Matrix aa)
    {
    Matrix c(m,n);
    for(int i=0;i< m;i++)
    {
    for(int j=0;j< n;j++)
    {
    c.A[i][j]=A[i][j]-aa.A[i][j];
    }
    }
    cout<< "The subtraction Of The Two Matrices Is:"<< endl; c.printmatrix();
    }

    int main()
    {
    //clrscr();
    cout<< "Enter Order The Two Square Matrices: " ;
    int a;
    cin>>a;

    Matrix x(a,a);
    Matrix y(a,a);

    cout<< endl<< "Enter Elements In First Matrix : ";
    x.readmatrix();

    cout<< endl<< "Enter Elements In The Second Matrix :";
    y.readmatrix();

    cout<< endl<< "The First Matrix:"<< endl;
    x.printmatrix();

    cout<< endl<< "The Second Matrix:"<< endl;
    y.printmatrix();
    x.addmatrix(y);
    x.submatrix(y);


    system("pause");
    }

    ReplyDelete

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