Complex Addition Operator Overloading
Complex Addition Operator Overloading
class complex {
public:
int real;
int img;
complex() {
real=img=0;
}
complex(int x,int y) {
real=x;
img=y;
}
void show() {
cout<<"\n"<<real<<"+"<<img<<"i";
}
friend complex operator+(complex c, complex d);
}; //end of class complex
//operator+ is not part of complex class so have 2 args for + operator overload.
complex operator+(complex c, complex f) {
complex ans;
ans.real = c.real+f.real;
ans.img = c.img+f.img;
return(ans);
}
int main() {
complex x(1, 2), y(0, 7);
complex c = x + y; //overloaded + is called here
c.show();
} // end of main