Operator Overloading

Sort by

recency

|

227 Discussions

|

  • + 0 comments

    Hope this helps for easy solution and better understanding:

    class Matrix{
        public: 
            vector<vector<int>> a;
            
            Matrix operator +(const Matrix& b){
                int n = a.size();
                int m = a[0].size();
                Matrix result;
                result.a.resize(n, vector<int>(m, 0)); // 2D vector mxn with entries=0
                
                for(int i=0; i<n; i++){
                    for (int j=0; j<m; j++){
                        result.a[i][j] = a[i][j] + b.a[i][j];
                    }
                }
                return result;
            }     
    };
    
  • + 0 comments

    Operator overloading makes custom types in C easier to use with familiar operators, improving code clarity. Cricbet99 club registration

  • + 0 comments

    class Matrix{ public: vector> a; Matrix(int n = 0, int m = 0) { a.resize(n, vector(m)); } Matrix operator+(Matrix const& obj){

    int n=a.size(); int m=a[0].size(); Matrix res(n,m); for(int i=0;i

  • + 0 comments

    Hello i want to share my solution, enjoy:

    class Matrix {
    public:
        vector<vector<int>> a;
    
        Matrix operator+(const Matrix& b) {
        Matrix m;
        m.a.resize(b.a.size());
            for (int i = 0; i < a.size(); i++) {
                m.a[i].resize(b.a[i].size());
            }
    
        for (int i = 0; i < b.a.size(); i++) {
            for (int j = 0; j < b.a[i].size(); j++) {
                m.a[i][j] = a[i][j] + b.a[i][j];
            }
        }
        return m;
    }
    };
    
  • + 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;
        }
    };