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
implement two stacks in one array A[1...n] in such a way that neither stack overflows unless the total number of elements in both stacks together is n. For this you need to produce

Default Arguments A default argument is a value that is automatically assigned to a formal variable, if the actual argument from the function call is omitted. e.g. void

Project Description: I want a script for an online poker site. It needs to be scanning all the required tables and when the table meets certain criteria it should take a seat at

(a) Write a fragment of code that declares a variable colSp of type ColourSpot, and then moves it to the point (3, -4), and sets its colour to Green. (b) A function QU8 is speci

Project Overview A certain financial institution (bank) wishes to promote its new business products/services by conducting road shows in rural areas. Their aim is to encourage peop

Write a program that predicts users age: Write a program that predicts users' age (0-128 years old) with at most 7 questions. The game starts with asking the user whether he/s

Program is to swap the two values which are enter by user: Program is to swap the two values which are enter by user through the function with values changed using pointers

Define the Meaning of Call by reference? In "call-by- reference", as an alternative of passing the value of a variable, the location number (or the address) of the variable is

Please give me a programming code of first & follow with c or c++ laqnguage .When anyone give me the programming code of first & follow please explain every lines with the help

A Constructive Example Consider an example , to model a user-defined data type for  strings. The object simulates a character array ( string ) using a character pointer and an