Operator Overloading

Sort by

recency

|

226 Discussions

|

  • + 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;
        }
    };
    
  • + 0 comments

    This was a weird one:

    #include <iostream>
    #include <vector>
    #include <cstddef>
    
    class Matrix
    {
    public:
        Matrix() = default;
        Matrix(std::size_t rows, std::size_t columns)
            : m_rows {rows}
            , m_columns {columns}
            , m_matrix {std::vector<std::vector<int>>(m_rows, std::vector<int>(m_columns))}
        {
        }
            
        friend Matrix operator+(const Matrix& m1, const Matrix& m2)
        {
            // The matrix has to have depth
            Matrix m3 {m1.m_rows, m1.m_columns};
            
            for(std::size_t r {0}; r < m1.m_rows; ++r)
            {
                for(std::size_t c {0}; c < m1.m_columns; ++c)
                {
                    m3(r, c) = m1(r, c) + m2(r, c); 
                }
            }
            
            return m3;
        }
        
        friend std::ostream& operator<<(std::ostream& out, const Matrix& m)
        {
            for(const auto& r : m.m_matrix)
            {
                for(const auto& c : r)
                {
                    out << c << ' ';
                }
                
                out << '\n';
            }
            
            return out;
        }
        
        friend std::istream& operator>>(std::istream& in, Matrix& m)
        {
            // Loops through the matrix and populates it
            for(auto& r : m.m_matrix)
            {
                for(auto& c : r)
                {
                    in >> c;
                }
            }
            
            return in;
        }
        
        // Returns a reference to an element
        int& operator()(const std::size_t r, const std::size_t c)
        {
            return m_matrix[r][c];
        }
        
        // 'const' version of 'operator()'
        const int& operator()(const std::size_t r, const std::size_t c) const
        {
            return m_matrix[r][c];
        }
        
    private:
        std::size_t m_rows {};
        std::size_t m_columns {};
        std::vector<std::vector<int>> m_matrix {};
      
    };
    
    int main() 
    {
        int numCases {};
        std::cin >> numCases;
        
        while(numCases--)
        {
            std::size_t rows {};
            std::size_t columns {};
            
            std::cin >> rows >> columns;
            
            // Constructs blank matrices, could also use std::vector::clear()
            Matrix m1 {};
            Matrix m2 {};
            
            m1 = {rows, columns};
            m2 = {rows, columns};
            
            std::cin >> m1 >> m2; 
            
            std::cout << m1 + m2; 
        }
        
        return 0;
    }