HackerRank Operator Overloading in C++ problem solution

In this HackerRank Operator Overloading problem in c++, You are given a main() function that takes a set of inputs to create two matrices and prints the result of their addiction. You need to write the class Matrix which has a member an 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.

HackerRank Operator Overloading in C++ problem solution

HackerRank Operator Overloading in C++ problem solution

class Matrix
{
public:
Matrix(){}
vector< vector<int> > a;


};
Matrix operator+(Matrix& a,Matrix b)
{
Matrix c;
c.a.resize(a.a.size());
for(int i=0;i<c.a.size();i++)
{
c.a[i].resize(a.a[i].size());
}

for(int i=0;i<a.a.size();i++)
{
for(int j=0;j<a.a[i].size();j++)
{
c.a[i][j]=a.a[i][j]+b.a[i][j];
}
}

return c;
}


Post a Comment

0 Comments