CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  1. #2
    Join Date
    Feb 2009
    Posts
    326

    Re: Overloading a matrix constructor for 1 and 2 dimensional arrays.

    Before we look at possible solutions, just wanted to highlight a few points:

    There were some compilation errors:
    1) Parameter or variable names mustn't begin with a number
    2) While defining the functions the parameter types must be mentioned not just the parameter names
    3) You can't have any function with the same set of parameters and expect it to overload.

    Also use code tags while posting code [code_] [/code_] without the underscore

    Possible solution (though needs some refinement):
    --------------------------------------------------------------
    You would like to initialize a 2 dimensional array using a single dimensional or 2 dimensional array. And you expect the compiler to pick the appropriate type of constructor based on the argument type:


    see if this is ok,
    1) There is a lot of hardcoding in the code so I declared some global variables instead (couldn't think of an alternative)
    2) Could get rid of the hardcoding using dynamic arrays, but then I had to change your program completly to do that
    3) Would be happy if someone can suggest a better solution especially to get rid of the global variables (am not too fond of it)


    Code:
    #include <iostream>
    using std :: cout;
    using std :: endl;
    
    const unsigned int DIMENSION1 = 4;
    const unsigned int DIMENSION2 = 4;
    
    class matrix4
    {
    public:
        matrix4(const float D1Array[]);
        matrix4(const float D2Array[][DIMENSION2]);
        void display() const;
    
    private:
    float elements[DIMENSION1][DIMENSION2];
    };
    int main()
    {
        float a[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
    
        float b[4][4] = {101, 102, 103, 104,
                  105, 106, 107, 108,
                  109, 110, 110, 112,
                  113, 114, 115, 116};
    
        matrix4 mat1(a);
        matrix4 mat2(b);
    
        system("clear");
    
        mat1.display();
        mat2.display();
        
        return(0);
    }
    
    matrix4 :: matrix4(const float D1Array[])
    {
        for( int i = 0; i < DIMENSION1; i++)
        { 
            for( int j = 0; j < DIMENSION2; j++)
            {
                elements[i][j] = D1Array[ i*4 + j ];
            }
        }
    }
    
    matrix4 :: matrix4(const float D2Array[][DIMENSION2])
    {
        cout << "\nDisplay matrix\n";
        
        for( int i = 0; i < DIMENSION1; i++)
        { 
            for( int j = 0; j < DIMENSION2; j++)
            {
                elements[i][j] = D2Array[ i ][ j ];
            }
        }
    
        cout << endl;
    }
    
    
    void matrix4 :: display() const
    {
        cout << "\nDisplay Elements:\n";
        for(int index1 = 0; index1 < DIMENSION1; index1 ++)
        {
          for(int index2 = 0; index2 < DIMENSION2; index2 ++)
          {
            cout << elements[index1][index2] << "\t";
          }
          cout << endl;
        }
    }
    Last edited by Muthuveerappan; September 5th, 2009 at 09:42 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured