Hackerrank Operator Overloading Solution
2 min read

Hackerrank Operator Overloading Solution

Hackerrank Operator Overloading Solution

Classes define new types in C++. Types in C++ not only interact by means of constructions and assignments but also via operators. For example:

int a=2, b=1, c;
c = b + a;

The result of variable c will be 3.
Similarly, classes can also perform operations using operator overloading. Operators are overloaded by means of operator functions, which are regular functions with special names. Their name begins with the operator keyword followed by the operator sign that is overloaded. The syntax is:type operator sign (parameters) { /*... body ...*/ }

You are given a main() function which takes a set of inputs to create two matrices and prints the result of their addition. You need to write the class Matrix which has a member a of type vector<vector<int> >. You also need to write a member function to overload the operator +. The function's job will be to add two objects of Matrix type and return the resultant Matrix.

Input Format.

First line will contain the number of test cases . For each test case, there are three lines of input.

The first line of each test case will contain two integers  and  which denote the number of the rows and columns respectively of the two matrices that will follow on the next two lines. These next two lines will each contain  elements describing the two matrices in row-wise format i.e. first  elements belong to the first row,next  elements belong to the second row and so on.

Constraints



, where  is the element in the  row and  column of the matrix.

Output Format

The code provided in the editor will use your class  and overloaded operator function to add the two matrices and give the output.

Sample Input

1
2 2
2 2 2 2
1 2 3 4

Sample Output

3 4 
5 6

Explanation

The sum of first matrix and the second matrix is the matrix given in the output.

Solution in cpp

Approach 1.

class Matrix{
    public:
vector<vector<int> > a;

Matrix & operator + (const Matrix &y) {

    for (int m=0; m<y.a.size(); ++m) {
        for (int n=0; n<y.a[0].size(); ++n) {
            this->a[m][n] = this->a[m][n] + y.a[m][n];
        }
    }

    return *this;
}
};

Approach 2.

class Matrix
{
    public:
    vector<vector<int>> a;

    Matrix & operator + (const Matrix &y) 
    {
        for (int m=0; m<y.a.size(); ++m) 
        {
            for (int n=0; n<y.a[0].size(); ++n)     
            {
                this->a[m][n] = this->a[m][n] + y.a[m][n];
            }
        }

    return *this;
    }
};

Approach 3.

class Matrix {
    public:
    vector<vector<int> > a;
    Matrix operator+(const Matrix& b){
         Matrix mat;
         int n=a.size();int m=a[0].size();
         for(int i=0;i<n;++i) {
            mat.a.push_back(vector<int>());
            for(int j=0;j<m;++j) {
                mat.a[i].push_back(this->a[i][j]+b.a[i][j]);
            }
         }
         return mat;
      }
};

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×