Overload Operators

Sort by

recency

|

132 Discussions

|

  • + 0 comments

    class Complex { private: int real; int imag;

    public:
    Complex(int r = 0, int i = 0)
    {
        real = r;
        imag = i;
    
    }
    
    Complex(const string& s)
    {
        int pos;
        pos = s.find('i');
        if(pos!= string::npos)
        {
            real = stoi(s.substr(0,pos-1));
            imag = stoi(s.substr(pos+1));
            if(s[pos-1] == '-')
            {
                imag = - imag;
            }
        }
    }
    
    Complex operator + (Complex const & Obj)
    {
        Complex res;
        res.real = real + Obj.real;
        res.imag = imag + Obj.imag;
        return res;
    }
    
    void print() { cout << real << "+i" << imag << '\n'; }
    

    }; int main() {

    string s1, s2;
    cin >> s1>>s2;
    
    Complex c1(s1);
    Complex c2(s2);
    
    Complex c3 = c1 + c2;
    
    c3.print();
    
    return 0;
    

    }

  • + 0 comments

    include

    include

    using namespace std;

    class Complex { public: int a, b; friend ostream& operator << (ostream& os, Complex obj); friend Complex operator + (Complex& a_obj, Complex& b_obj); };

    ostream& operator << (ostream& os, Complex obj) { os << obj.a << "+i" << obj.b; return os; }

    Complex operator + (Complex& a_obj, Complex& b_obj) { Complex c_obj; c_obj.a = a_obj.a + b_obj.a; c_obj.b = a_obj.b + b_obj.b; return c_obj; }

    void call_complex(string& s, int& a, int& b) { int num = 0; int i = 1; for (auto x : s) { if (x == '+') { continue; } else if (x == 'i') { a = num; num = 0; i = 1; continue; } num = num * i + (x - 48); i = 10; } b = num; }

    int main() {

    int a, b;
    string s;
    
    getline(cin, s);
    call_complex(s, a, b);
    
    Complex obj1{a, b};
    
    getline(cin, s);
    call_complex(s, a, b);
    
    Complex obj2{a, b};
    
    Complex obj3 = obj1 + obj2;
    cout << obj3 << endl;
    
    return 0;
    

    }

  • + 0 comments

    include

    include

    include

    include

    include

    using namespace std;

    class complex { private: int real; int img;

    public:
    
    
      void setReal(int r);
      void setImg(int i);
      int getReal();
      int getImg();
    
      complex operator+(complex c)
      {
        complex temp;
        temp.real=real+c.real;
        temp.img=img+c.img;
        return temp;
      }
    
    friend ostream & operator<<(ostream & out,complex c);  
    friend istream & operator>>(istream & in,complex &c);  
    

    };

    int main() { complex c1,c2,c3; cin>>c1; cin>>c2;

       c3=c1+c2;
       cout<<c3;
    
    return 0;
    

    } void complex::setReal(int r){real=r;} void complex::setImg(int i){img=i;} int complex::getReal(){return real;} int complex::getImg(){return img;} ostream & operator<<(ostream & out,complex c) { out<>(istream & in,complex &c) { in>>c.real; in.ignore(1,'+'); in.ignore(1,'i'); in>>c.img;

    return in;
    

    }

  • + 0 comments
    Complex operator+(const Complex& c1, const Complex& c2) {
        Complex result;
        result.a = c1.a + c2.a; // Sum of real parts
        result.b = c1.b + c2.b; // Sum of imaginary parts
        return result;
    }
    
    std::ostream& operator<<(std::ostream& os, const Complex& c) {
        os << c.a << "+i" << c.b;
        return os;
    }
    
  • + 0 comments

    Read the editorial soultion, instead of having an input() function, the operator>> should be overloaded to allow for direct input of a Complex object:

    #include <iostream>
    
    class Complex
    {
    public:
        Complex() = default;
        Complex(int a, int b)
            : m_a {a}, m_b {b}
        {
        }
        
        friend Complex operator+(const Complex& c1, const Complex& c2)
        {
            return {c1.m_a + c2.m_a, c1.m_b + c2.m_b};
        }   
        
        friend std::ostream& operator<<(std::ostream& out, const Complex& c)
        {
            out << c.m_a << "+i" << c.m_b; 
            return out;
        }     
        
        friend std::istream& operator>>(std::istream& in, Complex& c)
        {
            in >> c.m_a;
            in.ignore(2); // Ignores '+i'
            in >> c.m_b;
            
            return in;
        }
            
    private:
        int m_a {};
        int m_b {};
    };
    
    int main() 
    {
        Complex c1 {};
        Complex c2 {};
        
        std::cin >> c1 >> c2;
        
        std::cout << c1 + c2;
        
        return 0;
    }