The Daily Static
  The Daily Static
UF Archives
Register
UF Membership
Ad Free Site
Postcards
Community

Geekfinder
UFie Gear
Advertise on UF

Forum Rules
& FAQ


Username

Password


Create a New Account

 
 

Back to UserFriendly Strip Comments Index

corwin17's c++ corner: 03/27 operator overloading by corwin172014-03-27 10:45:15
  MY SOLUTION: because I need to move on [LONG] by corwin17 2014-03-27 14:54:39
First, Thank you EVERYONE for your help! I learned quite a lot these last 2 days, but, give that there are still some issues, but I have work-arounds, I've decided to stop trying to figure this out for now, and move on. What I have shouldn't be cumbersome to my program, if not the cleanest with operator overloading. [One outstanding question: do I need a destructor to clear the allocated memory so I don't have memory holes when a function loses focus after a matrix is created?] Now, My matrix class solution (feel free to use or comment on):
//- matrix class adapted from http://www.cplusplus.com/forum/general/97018/#msg524064
class matrix
{
    int     nrow, ncol;
    double  **p;

public:

    //----------------------------------
    //--- Constructor
    //----------------------------------
    matrix(int row, int col)
    {
        nrow = row;
        ncol = col;
        p = new double*[nrow];
        for (int i = 0; i < nrow; ++i)
        {
            p[i] = new double[ncol];
            for (int j = 0; j < ncol; ++j)
                p[i][j] = 0;
        }
    }

    //----------------------------------
    //--- SET ELEMENTS OF MATRIX
    //----------------------------------
    void setElement(const int irow,
                    const int icolumn,
                    const double element)
    {
      p[irow][icolumn] = element;
    }

    //----------------------------------
    //-- GET MATRIX INFORMATION
    //----------------------------------
    double getElement(const int irow, const int icolumn) const
    {
      return p[irow][icolumn];
    }
    int getRows()    const { return nrow; }
    int getColumns() const { return ncol; }


    //----------------------------------
    //-- USE KB TO ENTER DATA (NOT USED)
    //----------------------------------
    void accept()
    {
        std::cout<<"Enter matrix elements: ";
        for(int i = 0; i < nrow; i++)
        {
            for(int j = 0; j < ncol; j++)
            {
                std::cin >> p[i][j];
            }
        }
    }

    //----------------------------------
    //-- SHOW DATA (FOR DIAGNOSTIC ONLY)
    //----------------------------------
    void display()
    {
        std::cout <<"The matrix is:\n";
        for(int i = 0; i < nrow; i++)
        {
            for(int j = 0; j < ncol; j++)
            {
                std::cout << p[i][j] <<" ";
            }
            std::cout <<std::endl;
        }
    }

    //----------------------------------
    // ADD 2 MATRICES
    //----------------------------------
    void matadd(const matrix& A, const matrix& B)
    {
        for(int ii=0 ; ii<nrow ; ii++)
        {
            for(int jj=0 ; jj<ncol ; jj++)
                p[ii][jj] = A.p[ii][jj] + B.p[ii][jj];
        }
    }

    //----------------------------------
    //-- MULTIPLY 2 MATRICES
    //----------------------------------
    void matmultiply(const matrix& A, const matrix& B)
    {
        if(ncol==B.ncol && nrow==A.nrow && A.ncol==B.nrow)
        {
            for(int icol=0 ; icol<B.ncol ; icol++)
            {
                for(int irow=0 ; irow<A.nrow ; irow++)
                {
                    p[irow][icol] = 0.0;
                    for(int kk=0 ; kk<ncol ; kk++)
                        p[irow][icol] += (A.p[irow][kk] * B.p[kk][icol]);
                }
            }
        }
        else
            std::cout << "matrix::matmul FAILED - wrong sizes" << std::endl;
    }

    //----------------------------------
    //-- TRANSPOSE THE MATRIX
    //----------------------------------
    void transpose(const matrix& A)
    {
        for(int ii=0 ; ii<A.nrow ; ii++)
        {
            for(int jj=0 ; jj<A.ncol ; jj++)
                p[jj][ii] = A.p[ii][jj];
        }
    }

    //----------------------------------
    //-- OVERLOADED OPERATOR FOR COPYING
    //----------------------------------
    matrix& operator= (const matrix& A)
    {
        for(int ii=0 ; ii<A.nrow ; ii++)
        {
            for(int jj=0 ; jj<A.ncol ; jj++)
                p[ii][jj] = A.p[ii][jj];
        }
        return *this;
    }
};</pre>
And here is my test main for usage:
 void main()
{
    matrix rf(3,3), ff(3,3), mf(3,3);

    rf.setElement(0,0,1.0);
    rf.setElement(0,1,2.0);
    rf.setElement(0,2,3.0);
    rf.setElement(1,0,4.0);
    rf.setElement(1,1,5.0);
    rf.setElement(1,2,6.0);
    rf.setElement(2,0,7.0);
    rf.setElement(2,1,8.0);
    rf.setElement(2,2,9.0);

    rf.display();

    ff = rf;

    rf.setElement(0,0,-2.0);

    ff.display();
    rf.display();
    mf.display();

   cout << "\nMf = ff + rf\n";
   //mf.matmult(ff,rf);
   mf.matadd(ff,rf);
   ff.display();
   rf.display();
   mf.display();

   cout << "\nMf = transpose(ff)\n";
   mf.transpose(ff);
   ff.display();
   mf.display();

   cout << "\nff = (rf * ff)\n";
   mf.matmultiply(rf,ff);
   rf.display();
   ff.display();
   mf.display();
}
I am satisfied with the results!
[ Reply ]

 

[Todays Cartoon Discussion] [News Index]

Come get yer ARS (Account Registration System) Source Code here!
All images, characters, content and text are copyrighted and trademarks of J.D. Frazer except where other ownership applies. Don't do bad things, we have lawyers.
UserFriendly.Org and its operators are not liable for comments or content posted by its visitors, and will cheerfully assist the lawful authorities in hunting down script-kiddies, spammers and other net scum. And if you're really bad, we'll call your mom. (We're not kidding, we've done it before.)