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:39 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
If quicksort is so quick, why bother with anything else? If bubble sort is so bad, why even mention it? For that matter, why are there so many sorting algorithms? Your mission (sho

C CODE FOR PEBBLE MERCHANTS PROBLEM

I want source code for class booking system by using C++ Programming...Urgent

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve betw

In rPeANUt implement the "char getchar()" and "void printstring(char *str)" functions. Using these functions implement the following: void main() {    while (1) {       ch


First line starts with T, number of test cases. Each test case T contains a necklace (N).

please may i have a brief theory about a c++ program to find net salary of employees

Question: a) Give two uses of NFS. b) Give one advantage of using NFS. c) Explain the following lines as they would be interpreted in the /etc/exports file i. /usr/local

Create a mathematical number guessing game. Have the user prompt for the number of games that they want to play. Then each game consists of the following rules. a. The computer