-4

I ave made a program for matrix addition using operator overloading. I have made the program but it is not taking input for matrix B. Please look at the program and help. In this the matrix inputs are noramlly taken in main with two different objects, then i have used operator overloading for '+' to add 2 object members, ten for displaying also '<<' operator is overloaded.

    #include<iostream>
    using namespace std;
    int s;
    class matrix
    {
    public:
        int **m1,**m;
        friend matrix operator +(matrix &t,matrix &t1);
        friend ostream& operator << (ostream &out,matrix &t);
    };
    matrix operator +(matrix &t,matrix &t1)
    {
        for(int x=0;x<s;x++)
        {
            for(int y=0;y<s;y++)
            {
                t.m1[x][y]+=t1.m1[x][y];
            }
        }
    }
    ostream& operator << (ostream &out,matrix &t2)
    {
        for(int p=0;p<s;p++)
        {
            for(int q=0;q<s;q++)
            {
                out<<t2.m[p][q]<<"  ";
            }        
           out<<endl;
        }
    }
    int main()
    {
        cout<<"Enter the size of matrices(SQUARE MAT ONLY):: ";
        cin>>s;
        matrix t,t1,t2;
        for(int i=0;i<s;i++)
        {
            for(int j=0;j<s;j++)
            {
                cout<<"Enter value ("<<i+1<<","<<j+1<<") for matrix A::";
                cin>>t.m1[i][j];
            }
        }
        for(int i=0;i<s;i++)
        {
            for(int j=0;j<s;j++)
            {
                cout<<"Enter value ("<<i+1<<","<<j+1<<") for matrix B::";
                cin>>t1.m1[i][j];
            }
        }
        t2=t+t1;
        cout<<t2;
    }
user8435633
  • 11
  • 1
  • 3
  • 1
    Where do you acquire the memory for the internal representation of the matrix components? – Jodocus Nov 03 '17 at 17:09
  • 4
    why dont you use `std::vector` ? `**` is good for ascii arts, but not for nice code – 463035818_is_not_an_ai Nov 03 '17 at 17:11
  • Or a standard matrix algebra library. [Eigen](http://eigen.tuxfamily.org/index.php?title=Main_Page), for example. – Thomas Nov 03 '17 at 17:13
  • @tobi303, That is a nice idea bro, I have already made that program with vectors, Just wanted to see if it works with normal **matrix or not..but it's not.. – user8435633 Nov 03 '17 at 17:18
  • Search the internet for "C++ FAQ Matrix addition operator". – Thomas Matthews Nov 03 '17 at 17:34
  • And while you are at the C++ FAQ, look at how they make a 2D matrix with a 1D array. Often results in heeeyuge performance gains. Also keep an eye out for The Rule of Three. Without it you either have a memory leak or a some nasty-crashy behaviour. [What is The Rule of Three?](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) Follow the link and find out! – user4581301 Nov 03 '17 at 17:53

2 Answers2

2

What you implemented looks more like the += operator. It modifies t in-place, and doesn't even return anything, despite its signature (turn up and heed your compiler warnings!). So this is undefined behaviour.

In a nutshell, the + operator should create and return a new matrix object.

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • I have tried this also...like "t3.m1[i][j]=t.m1[i][j]+t1.m1[i][j]" adn after the loo returning the object.matrix "return(t3.m1[i][j])". And compiler isn't showing any errors or even warnings. Can u please edit the code and give it back if possible? please!! – user8435633 Nov 03 '17 at 17:12
1

I am assuming that you are creating a class for a square matrix and want to add two square matrices. I modified your class representation and some code and came up with a following possible solution.

 #include<iostream>
 using namespace std;
 #define s 3 //this is just an example value of s. This should generally be a defined as all caps. Eg: #define MATRIX_SIZE 3
 class matrix
 {
        public:
        int  **m;
        matrix()
        {
            m = new int*[s];
            for(int i = 0 ; i < s ; i++)
            {
                m[i] = new int[s];
            }
        }
        friend matrix operator +(matrix &m1, matrix &m2);
        friend ostream& operator << (ostream &os, matrix &m1);

};

matrix operator +(matrix &m1, matrix &m2)
{
    matrix m3 = matrix();


    for(int i = 0 ;i < s ; i++)
    {
        for(int j = 0 ; j < s ; j++)
        m3.m[i][j] = m1.m[i][j] + m2.m[i][j];


    }
    return m3;
}

ostream& operator <<(ostream &os, matrix& m)

{
    for(int i = 0 ; i< s ; i++)
    {
        for(int j = 0 ; j < s ; j++)
        os << m.m[i][j] <<" ";
        os << "\n";
    }
    return os;
}

int main()
{
    matrix m1 = matrix();
    matrix m2 = matrix ();
    for(int i = 0 ; i < s ; i++)
    {
    for(int j = 0 ;j < s ; j++)
    {
         m1.m[i][j] = i+j;
      m2.m[i][j] = i+j;
    }

    }
    matrix m3 = m1 + m2;
    cout << m3;
    return 0;
}
  • Thanks for the program.Big help.. – user8435633 Nov 03 '17 at 17:48
  • There are some tactical advantages to making the `matrix &` parameters `const matrix &`. `#define MATRIX_SIZE 3` is safer as `const size_t MATRIX_SIZE = 3;` #define is mindless text replacement and can wreck unholy hell on a program. `#define s 3` makes a great demonstration of the sort of weirds that can crop up: https://ideone.com/puMIYY . The error messages are a hell of a lot nicer than they used to be. – user4581301 Nov 03 '17 at 18:01
  • Yes I would agree about making use of const matrix & in situations where the function is not manipulating the function parameters. – Sanjana Gupta Nov 03 '17 at 20:28