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

Assignment Help:

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);

...

}


Related Discussions:- How do i develop a subscript operator for a matrix class?

Explain the scope resolution operator, The Scope Resolution Operator( :: ) ...

The Scope Resolution Operator( :: ) Global variables are explained outside any functions and thus can be used by all the functions defined thereafter. However, if a global vari

Explain about the expressions in c language, Explain about the Expressions ...

Explain about the Expressions in c Language? An expression is the combination of constants, variables and operators arranged as per the syntax of the language. Some of the illu

What do you mean by a sequential access file, What do you mean by a sequent...

What do you mean by a sequential access file? - When writing programs which store and retrieve data in a file, it's possible to designate that file into various forms. - A s

I need keylogger extension on chrome, Project Description: I want someon...

Project Description: I want someone to create and install a keylogger on a chrome browser. This could be a relatively simple job, please have experience with this type of work.

.C# mouse and maze program, .Create a Csharp solution that will show a maze...

.Create a Csharp solution that will show a maze in the user interface and will show a mouse walking through the maze attempting to locate the exit door (the cheese).

I need whatsapp software in my website, I need whatsapp software in my webs...

I need whatsapp software in my website Project Description: i need whatsapp software in my website same this whatsapp if anyone can make to me this in my website Skills

C++, Write C++ code for calculating the time table

Write C++ code for calculating the time table

Calculating interest, The interest charged on a loan banking facility is ca...

The interest charged on a loan banking facility is calculated based on principal amount, rate and time. implement a C program that can be used to automate the calculation of the in

How can dereferencing the pointer this, Dereferencing the Pointer this ...

Dereferencing the Pointer this Sometimes a member function requires to make a copy of the invoking instance so that it can change the copy without affecting the original instan

Explain about the floating point constants in c language, Explain about the...

Explain about the Floating point Constants in c language? A floating point constant is the number that contains either a fraction or decimal part. If an exponent is present its

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd