Overloading a binary Minus (-) operator!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nelsupremo
    New Member
    • Jun 2007
    • 3

    Overloading a binary Minus (-) operator!

    I am not fluent in overloading stuff .. there was a question that I can across on net .. any bright ideas?

    The program will contain a class Matrix, This class will contain a private data member Array[][] which store int values. The class will further contain a Default constructor, get() function which takes values for array from the user and also contain a Display function witch display the array on the screen,

    In main function create three objects Mat1, Mat2, Mat3 of this class, first call get() and Display() functions with Mat1 and Mat2 objects then implement the statement Mat3 = Mat1 - Mat2; and call Display() function with Mat3.

    Note: The matrix should be 2x2.
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    I'm not sure what your asking for help with. Are you stuck trying to overload the minus operator? It sounds like you have a good understanding of the problem you need to solve, but what code do you have so far that your stuck on?

    Comment

    • Nerd999
      New Member
      • Jun 2007
      • 1

      #3
      nice question but i am facing trouble
      mac paste your solutions so that we can compare

      Comment

      • mac11
        Contributor
        • Apr 2007
        • 256

        #4
        Originally posted by Nerd999
        nice question but i am facing trouble
        mac paste your solutions so that we can compare
        I never made a solution, and I don't have any code to post for one. I only asked for more detail about his problem so that I might help.

        Comment

        • Nelsupremo
          New Member
          • Jun 2007
          • 3

          #5
          ok I am posting the code!

          Comment

          • Nelsupremo
            New Member
            • Jun 2007
            • 3

            #6
            My code is not giving the desired answer! Please check it out .. and also if possible give some better method of entering elements in the matrix!


            #include <stdio.h>
            #include <conio.h>
            #include <iostream.h>

            class Matrix
            {
            public:
            Matrix()
            {
            }
            void input()
            {
            cout <<"\nEnter the elements into the matrix \n";
            for (int i=0; i<2; i++)
            for ( int j=0; j<2; j++)
            cin >> matrix [i][j];
            }

            void display ()
            {
            cout <<"\n\nThe elements in the matrix are: \n";
            for ( int i=0; i<2; i++ )
            {
            for ( int j=0; j<2; j++)
            cout << matrix [i][j] << "\t";
            cout <<"\n";
            }
            }

            Matrix operator - (const Matrix &m)
            {
            Matrix mm;
            for ( int j=0; j<2; j++)
            for ( int i=0; i<2; i++ )
            mm.matrix[i][j] = mm.matrix[i][j] - mm.matrix[i][j];
            return mm;
            }

            private:

            int matrix [2][2];
            };


            void main (void)

            {
            Matrix m1,m2,m3;
            clrscr();
            cout <<"\nEnter the elements into the first matrix .. \n";
            m1.input();
            cout <<"\nEnter the elements into the second matrix .. \n";
            m2.input();

            m3=m1-m2;

            cout <<"\nThe resultant matrix: ";
            m3.display();

            getche();

            }

            Comment

            • mac11
              Contributor
              • Apr 2007
              • 256

              #7
              your problem is that your doing math on each element from the local matrix mm:
              [CODE=cpp]
              Matrix operator - (const Matrix &m)
              {
              Matrix mm;
              for ( int j=0; j<2; j++)
              for ( int i=0; i<2; i++ )
              mm.matrix[i][j] = mm.matrix[i][j] - mm.matrix[i][j];
              return mm;
              }[/CODE]

              try this instead
              [CODE=cpp]
              Matrix operator - (const Matrix &m)
              {
              Matrix mm;
              for ( int j=0; j<2; j++)
              for ( int i=0; i<2; i++ )
              mm.matrix[i][j] = this->matrix[i][j] - m.matrix[i][j]; // THIS LINE CHANGED
              return mm;
              }[/CODE]

              Comment

              Working...