By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,049 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

Overloading a binary Minus (-) operator!

P: 3
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.
Jun 19 '07 #1
Share this Question
Share on Google+
6 Replies


100+
P: 256
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?
Jun 19 '07 #2

P: 1
nice question but i am facing trouble
mac paste your solutions so that we can compare
Jun 20 '07 #3

100+
P: 256
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.
Jun 20 '07 #4

P: 3
ok I am posting the code!
Jun 21 '07 #5

P: 3
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();

}
Jun 21 '07 #6

100+
P: 256
your problem is that your doing math on each element from the local matrix mm:
Expand|Select|Wrap|Line Numbers
  1.  Matrix operator - (const Matrix &m)
  2.     {
  3.      Matrix mm;
  4.      for ( int j=0; j<2; j++)
  5.       for ( int i=0; i<2; i++ )
  6.        mm.matrix[i][j] =  mm.matrix[i][j] -  mm.matrix[i][j];
  7.        return mm;
  8.      }
try this instead
Expand|Select|Wrap|Line Numbers
  1.  Matrix operator - (const Matrix &m)
  2.     {
  3.      Matrix mm;
  4.      for ( int j=0; j<2; j++)
  5.       for ( int i=0; i<2; i++ )
  6.        mm.matrix[i][j] =  this->matrix[i][j] -  m.matrix[i][j]; // THIS LINE CHANGED
  7.        return mm;
  8.      }
Jun 21 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.