Operator overloading

PHOTO EMBED

Sun Jun 26 2022 06:11:23 GMT+0000 (UTC)

Saved by @Ranjan_kumar #c++

#include <bits/stdc++.h>
using namespace std;

class Complex
{
   public:
    int real;
    int img;
   
   Complex(int r=0,int i=0)
   {
      real=r;
      img=i;
   }
   Complex operator+(Complex c)  //operator overloading or
    {                            //(Complex add(Complex c)
      Complex temp;
      temp.real=real+c.real;
      temp.img=img+c.img;
      return temp;
    }
};
int main()
{
   Complex c1(2,5),c2(3,5),c3;
   
   c3=c1+c2;                     // or c3=c1.add(c2);
   cout<<c3.real<<"+i"<<c3.img<<endl;
	return 0;
}
content_copyCOPY