Overload () operator
Let us see how to overload parenthesis operator for matrix class such that matrix(i,j) returns ith row, jth column element of the matrix.
matrix.h
#ifdef preprocessor directive was inserted by codelite IDE. What it does is to protect us from multiple inclusion of the same file. If matrix_h is not defined then only include its declaration.
Next let is look at data elements of the class viz nr,nc and elements. nr and nc are number of rows and columns of the matrix. Notice that these have only getters not setters because once a matrix is defined, you should not be able to change nr or nc.
Elements is where the elements of matrix are stored. I am using a one dimensional dynamic int array. So each access of element will have to be row*number of columns + col.
The class has a default constructor and constructor with 2 parameters - nr and nc.
Constructors allocate memory to array. And initialize elements to 0. We have totally nr*nc elements in the matrix.
Now we should see how to overload () operator for this class.
The function just returns ith row and jth column element from the matrix. And index is i*numberOfColumns+j because my implementation is using 1 d array.
But a crucial point to remember is return type of operator function. It is int reference. Not just int. Because we must be able to assign values to the elements of the matrix with the help of this operator. So the function returns reference to ith row jth column element of the matrix.
Let us use it in action.
In main function we are creating a matrix of 3X3, reading values into it and displaying it back.
Download the complete program from here
matrix.h
#ifndef MATRIX_H #define MATRIX_H class matrix { int *elements; int nr,nc; public: matrix(); ~matrix(); int getNrows(){return nr;} int getNcols(){return nc;} matrix(int nr,int nc); int & operator() (int row,int col); }; #endif // MATRIX_H
#ifdef preprocessor directive was inserted by codelite IDE. What it does is to protect us from multiple inclusion of the same file. If matrix_h is not defined then only include its declaration.
Next let is look at data elements of the class viz nr,nc and elements. nr and nc are number of rows and columns of the matrix. Notice that these have only getters not setters because once a matrix is defined, you should not be able to change nr or nc.
Elements is where the elements of matrix are stored. I am using a one dimensional dynamic int array. So each access of element will have to be row*number of columns + col.
The class has a default constructor and constructor with 2 parameters - nr and nc.
matrix::matrix() { nr = nc = 2; this->elements = new int[nr*nc]; for(int i=0;i<nr*nc;i++) { elements[i]=0; } } matrix::~matrix() { } matrix::matrix(int nr,int nc) { this->nr = nr; this->nc = nc; this->elements = new int[nr*nc]; for(int i=0;i<nr*nc;i++) { elements[i]=0; } }
Constructors allocate memory to array. And initialize elements to 0. We have totally nr*nc elements in the matrix.
Now we should see how to overload () operator for this class.
int& matrix::operator()(int i,int j) { int index = i*nc; index+=j; return elements[index]; }
The function just returns ith row and jth column element from the matrix. And index is i*numberOfColumns+j because my implementation is using 1 d array.
But a crucial point to remember is return type of operator function. It is int reference. Not just int. Because we must be able to assign values to the elements of the matrix with the help of this operator. So the function returns reference to ith row jth column element of the matrix.
Let us use it in action.
#include <iostream> #include"matrix.h" using namespace std; int main( ) { matrix m(3,3); for(int i=0;i<3;i++) for(int j=0;j<3;j++) { int temp; cout<<"Value:"; cin>>temp; m(i,j)=temp; } for(int i=0;i<m.getNrows();i++) { for(int j=0;j<m.getNcols();j++) { cout<<m(i,j)<<" "; } cout<<endl; } }
In main function we are creating a matrix of 3X3, reading values into it and displaying it back.
Download the complete program from here
Comments
Post a Comment