Hello coders, In this post, you will learn how to solve the **Operator Overloading in C++ Hacker Rank Solution**. This problem is a part of the **HackerRank C++ Programming** Series.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C Programming, HackerRank C++ Programming, HackerRank Java Programming, HackerRank Python Programming, HackerRank Linux Shell, HackerRank SQL Programming, and HackerRank 10 days of Javascript.

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology,Data Structures, RDBMS Programs, Java Programs Solutions, Fiverr Skills Test answers, Google Course Answers, Linkedin Assessment, and Coursera Quiz Answers.

## Operator Overloading in C++ Hacker Rank Solution

### Objective

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

inta=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:

typeoperatorsign(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 T. For each test case, there are three lines of input. The first line of each test case will contain two integers N and M 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 N*M elements describing the two matrices in row-wise format i.e. first M elements belong to the first row,next M elements belong to the second row and so on.

#### Constraints :

- 1 <= T <= 1000
- 1 <= N <= 100
- 1 <= M <= 100
- 1 <= Aij <= 10, where Aij is the element in the ith row and jth Column of the Matrix.

#### Output Format :

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

#### Sample Input :

12222221234

#### Sample Output :

3456

#### Explanation :

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

**Operator Overloading in C++ Hacker Rank Solution**

#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; /* Operator Overloading in C++ - Hacker Rank Solution START */ class Matrix { public: vector<vector<int>> a; Matrix() {} Matrix operator+(const Matrix &o) { Matrix sum(*this); for (size_t i = 0; i < sum.a.size(); i++) { for (size_t j = 0; j < sum.a[i].size(); j++) { sum.a[i][j] += o.a[i][j]; } } return sum; } }; /* Operator Overloading in C++ - Hacker Rank Solution END */ int main () { int cases,k; cin >> cases; for(k=0;k<cases;k++) { Matrix x; Matrix y; Matrix result; int n,m,i,j; cin >> n >> m; for(i=0;i<n;i++) { vector<int> b; int num; for(j=0;j<m;j++) { cin >> num; b.push_back(num); } x.a.push_back(b); } for(i=0;i<n;i++) { vector<int> b; int num; for(j=0;j<m;j++) { cin >> num; b.push_back(num); } y.a.push_back(b); } result = x+y; for(i=0;i<n;i++) { for(j=0;j<m;j++) { cout << result.a[i][j] << " "; } cout << endl; } } return 0; }

**Disclaimer: **The above Problem **(Operator Overloading )** is generated by **Hackerrank** but the Solution is Provided by **Chase2Learn**. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the following contact form thank you.

### FAQ:

#### Which language is best for HackerRank?

###### While **JavaScript** is the best-known language, HackerRank also found that only 5% of respondents say it is their first programming language.

#### Is HackerRank good for beginners?

**HackerRank is very good for beginners** so even if you want to print your first program “Hello World!” then definitely HackerRank gives this opportunity to you. It has a pretty good UI with boilerplate code pre-written that helps beginners to start competitive coding.

#### What is meant by HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedia

#### What is C++ programming used for?

C++ is a general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or “C with Classes”. Wikipedia

#### Where can I find HackerRank solutions in CPP?

in this post you will get all the *solutions* of *HackerRank CPP* Problems.

Finally, we are now, in the end, I just want to conclude some important message for you

**Note:-** I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe.