matrix class

This is a discussion on matrix class within the C++ Programming forums, part of the General Programming Boards category; I have matrix class that implements a 2-dimensional array of "integers". I now want to convert this to a class ...

  1. #1
    Unregistered
    Guest

    matrix class

    I have matrix class that implements a 2-dimensional array of "integers". I now want to convert this to a class template so that I can have 2-dimensional arrays of "any types". Not for sure how to start. Any suggestions would be greatly appreciated.
    AQuaman

    #include <algorithm>
    #include <cassert>

    #include "matrix.h"

    Matrix::Matrix()
    : data(0), _length1(0), _length2(0)
    {}

    Matrix::Matrix(unsigned theLength1, unsigned theLength2)
    : _length1(theLength1), _length2(theLength2)
    {
    data = new int[theLength1*theLength2];
    }

    Matrix::Matrix(const Matrix& m)
    : _length1(m._length1), _length2(m._length2)
    {
    data = new int[_length1*_length2];
    copy (m.data, m.data+_length1*_length2, data);
    }

    Matrix::~Matrix()
    {
    delete [] data;
    }

    const Matrix& Matrix:perator= (const Matrix& m)
    {
    if (this != &m)

    {

    if (_length1*_length2 < m._length1*m._length2)

    {

    delete [] data;

    data = new int[m._length1*m._length2];

    }

    _length1 = m._length1;

    _length2 = m._length2;

    copy (m.data, m.data+_length1*_length2, data);

    }
    return *this;
    }

    int& Matrix:perator() (int i1, int i2)
    {
    assert ((i1 >= 0) && (i1 < _length1));
    assert ((i2 >= 0) && (i2 < _length2));
    return data[i1 + _length1*i2];
    }

    const int& Matrix:perator() (int i1, int i2) const
    {
    assert ((i1 >= 0) && (i1 < _length1));
    assert ((i2 >= 0) && (i2 < _length2));
    return data[i1 + _length1*i2];
    }

    bool Matrix:perator== (const Matrix& m) const
    {
    return (_length1 == m._length1)

    && (_length2 == m._length2)

    && equal (data, data+_length1*_length2, m.data);
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    Its best to move all implementation into your .h file.

    The syntax in the .h file looks like:
    Code:
    template<class T> class Matrix 
    {...
    
    };
    
    //non inline constructor
    template<class T> Matrix<T>::Matrix() 
    {} 
    ....
    Then any reference to the type you should use T.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 03:33 PM
  4. Matrix Class & more...
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2002, 04:59 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21