Overload Operators

Sort by

recency

|

131 Discussions

|

  • + 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;
    }
    
  • + 0 comments
    //Operator Overloading
    
    #include<iostream>
    
    using namespace std;
    
    class Complex
    {
    public:
        int a,b;
        void input(string s)
        {
            int v1=0;
            int i=0;
            while(s[i]!='+')
            {
                v1=v1*10+s[i]-'0';
                i++;
            }
            while(s[i]==' ' || s[i]=='+'||s[i]=='i')
            {
                i++;
            }
            int v2=0;
            while(i<s.length())
            {
                v2=v2*10+s[i]-'0';
                i++;
            }
            a=v1;
            b=v2;
        }
    };
    
    Complex operator+(Complex c1, Complex c2){
        Complex c;
        c.a = (c1.a + c2.a);
        c.b = (c1.b + c2.b);
        return c;
    }
    
    //b) Operator << should print a complex number in the format "a+i
    std::ostream& operator << (std::ostream &outS, Complex cn){
        outS << cn.a << "+i"<< cn.b << std::endl;
        return outS;
    }
    
    int main()
    {
        Complex x,y;
        string s1,s2;
        cin>>s1;
        cin>>s2;
        x.input(s1);
        y.input(s2);
        Complex z=x+y;
        cout<<z<<endl;
    }