1 Replies - 538 Views - Last Post: 13 March 2011 - 03:04 PM Rate Topic: -----

#1 novacrazy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 117
  • Joined: 01-March 11

Overloading operators to deep copy and add two dynamic multidimensiona

Posted 13 March 2011 - 01:51 PM

I am attempting to create a class to manipulate matrices, and I'm not quite sure how to add two of them together through operator overloading. I honestly have no idea how to overload the = operator for this either. I have considered overloading both the arithmetic operators (+, -, *, /), as well as the = operator to work together to perform the operations I need. Here is what I have so far:
class ___s {public: int ___x; ___s(int x){___x = x;};} ___s(0);
template <class math_class = long double>
class matrix
{
protected:
    int ROW_, COLUMN_;
    bool matrix_set;
    math_class **MATRIX_STO;
public:
    int matrix_number;
    matrix() : ROW_(0), COLUMN_(0), matrix_set(false)
    {
        MATRIX_STO = new math_class*[0];
        MATRIX_STO[0] = new math_class[0];
        matrix_number = ___s.___x++;
    }
    friend matrix operator+(const matrix &one, const matrix &two);
    matrix(int nRows, int nColumns, bool try_again = true)
    {
        using namespace std;
        try
        {
            MATRIX_STO = new math_class*[nRows];
            for (int x = 0; x < nRows; x++)
            {
                MATRIX_STO[x] = new math_class[nColumns];
            }
            ROW_ = nRows; COLUMN_ = nColumns;
            matrix_set = true;
            matrix_number = ___s.___x++;
        }
        catch (bad_alloc&)
        {
            if (try_again == true)
            {
                _try_set(nRows, nColumns, 3, true, false);
            }
            matrix_set = false;
        }
    }
    void _try_set(int nRows, int nColumns, int try_number = 1, bool display_error = true, bool for_operator = false)
    {
        if (matrix_set == false)
        {
            using namespace std;
            try
            {
                MATRIX_STO = new math_class*[nRows];
                for (int x = 0; x < nRows; x++)
                {
                    MATRIX_STO[x] = new math_class[nColumns];
                }
                ROW_ = nRows; COLUMN_ = nColumns;
                matrix_set = true;
                matrix_number = ___s.___x++;
            }
            catch (bad_alloc&)
            {
                for (int current_try = 0; current_try < try_number; current_try++)
                {
                    _try_set(nRows, nColumns, 0, false);
                }
                if (display_error == true)
                {
                    if (!for_operator)
                    {
                        cerr << "Could not allocate enough memory to create matrix of desired size:\n" << nRows << " by " << nColumns << ".\n"<< \
                        try_number << ((try_number > 1) ? " attempts were made." : " attempt was made.") << endl << endl;
                    }
                    if (for_operator)
                    {
                        cerr << "could not allocate requested amount of memory to perform this operation" << ".\n"<< \
                        try_number << ((try_number > 1) ? " attempts were made." : " attempt was made.") << endl << endl;
                    }
                }
                matrix_set = false;
            }
        }
    }
    void set_row(int row, math_class a_row[], int number_of_elements)
    {
        if (matrix_set == true)
        {
            if ((row >= ROW_) || ((number_of_elements - 1) >= COLUMN_)) {std::cerr << "Location Non-existant\n";}
            else
            {
                for (int current_element=0; current_element < number_of_elements; current_element++)
                {
                    MATRIX_STO[row][current_element] = a_row[current_element];
                }
            }
        }
        else {std::cerr << "Matrix not set\n";}
    }
    void set_column(int column, math_class a_column[], int number_of_elements)
    {
        if (matrix_set == true)
        {
            if ((column >= COLUMN_) || ((number_of_elements - 1) >= ROW_)) {std::cerr << "Location Non-existant\n";}
            else
            {
                for (int current_row = 0; current_row < number_of_elements; current_row++)
                {
                    MATRIX_STO[current_row][column] = a_column[current_row];
                }
            }
        }
        else {std::cerr << "Matrix not set\n";}
    }
    math_class get_location(int nRow, int nColumn)
    {
        try
        {
            if (matrix_set == 1)
            {
                return MATRIX_STO[nRow][nColumn];
            }
            else throw "Matrix not set";
        }
        catch (const char* caught)
        {
            std::cerr << caught << ", returning "; return ERROR;
        }
    }
    ~matrix() {___s.___x--;} //will add code to delete MATRIX_STO soon.
};
template <typename M_TYPE>
matrix<M_TYPE> operator+(const matrix<M_TYPE> &one, const matrix<M_TYPE> &two)
{
    //declare basic variables
    matrix<M_TYPE> *TEMP_MATRIX; int H_x, H_y, L_x, L_y;
    //find high and low values for amounts of columns and rows, x = rows, y = columns
    H_x = ((one.ROW_ >= two.ROW_) ? one.ROW_ : two.ROW_);
    H_y = ((one.COLUMN_ >= two.COLUMN_) ? one.COLUMN_ : two.COLUMN_);
    L_x = ((one.ROW_ <= two.ROW_) ? one.ROW_ : two.ROW_);
    L_y = ((one.COLUMN_ <= two.COLUMN_) ? one.COLUMN_ : two.COLUMN_);
    //find which matrix is larger in each dimension, Cone means one has larger column number, Rone means one has larger row number.
    bool Rone_is_bigger = ((one.ROW_ >= two.ROW_) ? true : false);
    bool Cone_is_bigger = ((one.COLUMN_ >= two.COLUMN_) ? true : false);
    //allocate temporary matrix;
    *TEMP_MATRIX = new matrix<M_TYPE>;
    TEMP_MATRIX->_try_set(H_x, H_y, true, true);
    for (int CR = 0; CR < H_x; CR++)    {for (int CC = 0; CC < H_y; CC++) {TEMP_MATRIX[CR][CC] = 0;};}
    for (int current_row = 0; current_row < L_x; current_row++)
    {
        for (int current_column = 0; current_column < L_y; current_column++)
        {
            TEMP_MATRIX,MATRIX_STO[current_row][current_column] = (one.MATRIX_STO[current_row][current_column] + two.MATRIX_STO[current_row][current_column]);
        }
    }
    //and everything sort of falls apart here. 
    //I need it to simply make the empty spaces in the target matrix be filled with the unused parts of the larger source matrix, if that makes any sense... Sort of like adding the 0 of the target matrix's unused locations with the larger source matrix's variables. 
    for (int x = 0; x < H_x; x++)
    {
        if (Rone_is_bigger)
        {
            if (Cone_is_bigger)
            {
                for (int y = L_y; y < H_y; y++)
                {
                    TEMP_MATRIX.MATRIX_STO[x][y] = one.MATRIX_STO[x][y];
                }
            }
        }
        if (!Rone_is_bigger)
        {
            if (!Cone_is_bigger)
            {
                for (int y = L_y; y < H_y; y++)
                {
                    TEMP_MATRIX.MATRIX_STO[x][y] = two.MATRIX_STO[x][y];
                }
            }
        }
    }

}


Yeah, so if I could simply get some help to overload the needed operators and such, that would be nice. My main problem though is how to pass the TEMP_MATRIX to the assignment operator, which I haven't figured out yet. Once its to the assignment operator I can do a deep copy and apply it, then delete the temp matrix. Help would be nice :)

This post has been edited by novacrazy: 13 March 2011 - 01:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Overloading operators to deep copy and add two dynamic multidimensiona

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1371
  • View blog
  • Posts: 4,744
  • Joined: 19-February 09

Re: Overloading operators to deep copy and add two dynamic multidimensiona

Posted 13 March 2011 - 03:04 PM

In the class definition friend is not needed here, the call is not quite right as well.

    friend matrix operator+(const matrix &one, const matrix &two);



Here:
C++ Operator Overloading Guidelines

it is mentioned,

www.cs.caltech.edu said:

Define your binary arithmetic operators using your compound assignment operators.


That may save you some work.

Matrix a, b, c;
  ...
  c = 
      a + b;    // Same as a.operator+(B)/>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1