Overload Operators

  • + 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;
    

    }