How do i develop a subscript operator for a matrix class?, C/C++ Programming

Employ operator () instead of operator[].

While you have multiple subscripts, the cleanest way to do it is along with operator () instead of with operator[]. The reason is that operator[] always takes specifically one parameter, however operator() can take any number of parameters (in the case of rectangular matrix, two parameters are required).

For instance:

class Matrix {

public:

Matrix(unsigned rows, unsigned cols);

double& operator() (unsigned row, unsigned col);  subscript operators frequently come in pairs double operator() (unsigned row, unsigned col) const;  subscript operators frequently come in pairs

...

~Matrix(); // Destructor

Matrix(const Matrix& m); // Copy constructor

Matrix& operator= (const Matrix& m); // Assignment operator

... private:

unsigned rows_, cols_;

double* data_;

};

inline

Matrix::Matrix(unsigned rows, unsigned cols)

: rows_ (rows)

, cols_ (cols)

//data_ <--initialized below (after the 'if/throw' statement)

{

if (rows == 0 || cols == 0)

throw BadIndex("Matrix constructor contain 0 size");

data_ = new double[rows * cols];

}

inline

Matrix::~Matrix()

{

delete[] data_;

}

inline

double& Matrix::operator() (unsigned row, unsigned col)

{

if (row >= rows_ || col >= cols_)

throw BadIndex("Matrix subscript is beyond bounds");

return data_[cols_*row + col];

}

inline

double Matrix::operator() (unsigned row, unsigned col) const

{

if (row >= rows_ || col >= cols_)

throw BadIndex("const Matrix subscript is beyond bounds");

return data_[cols_*row + col];

}

You can then access an element of Matrix m via m(i,j) instead of m[i][j]:

int main()

{

Matrix m(10,10); m(5,8) = 106.15; std::cout << m(5,8);

...

}

Posted Date: 3/16/2013 3:17:26 AM | Location : United States







Related Discussions:- How do i develop a subscript operator for a matrix class?, Assignment Help, Ask Question on How do i develop a subscript operator for a matrix class?, Get Answer, Expert's Help, How do i develop a subscript operator for a matrix class? Discussions

Write discussion on How do i develop a subscript operator for a matrix class?
Your posts are moderated
Related Questions
The #define Directive The #define directive explains a macro which is a text string represented by a name. Whenever the pre-processor finds this name in the program, it is repl

I need to include files so you can understand easier.

A game is played with three piles of stones and two players. At her turn, a player removes one or more stones from the piles. However, if she takes stones from more than one pile,

If an array holds integer, each of which is four bytes long, how many bytes from the base location of the array is the location of the fifth element?

Explain the Bit Wise Operators? C language has distinction of supporting special operators recognized as bit wise operators for manipulation of data at bit level. These operato

Which one would you prefer - a macro or a function? Actually it depends on the purpose of program! - In case of macros, corresponding code is inserted directly into your sou

There is a pebble merchant. He sells the pebbles, that are used for shining the floor. His main duty is to take the length of the room’s sides. But he sometimes mistakes doing tha

a program to accept average, if average is 75 to 85 print it out else bypass using counter. enter and process 5 records

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome