Operator Overloading

  • + 0 comments
    class Matrix {
    public:
        vector<vector<int>> a;
        
        Matrix operator+(const Matrix& other) {
            Matrix result;
            int n = a.size();
            int m = a[0].size();
            
            for (int i = 0; i < n; i++) {
                vector<int> row;
                for (int j = 0; j < m; j++) {
                    row.push_back(a[i][j] + other.a[i][j]);
                }
                result.a.push_back(row);
            }
            
            return result;
        }
    };