class Complex\n{\npublic:\n int a,b;\n};<\/code><\/pre>\n\n\n\nOperators are overloaded by means of operator functions, which are regular functions with special names. Their name begins with the operator keyword followed by the operator sign that is overloaded. The syntax is:<\/p>\n\n\n\n
type operator sign (parameters) { \/*... body ...*\/ }<\/code><\/pre>\n\n\n\nYou need to overload operators +<\/code> and <<<\/code> for the Complex<\/em> class.<\/p>\n\n\n\nThe operator +<\/code> should add complex numbers according to the rules of complex addition:<\/p>\n\n\n\n(a+ib)+(c+id) = (a+c) + i(b+d) <\/code><\/pre>\n\n\n\nOverload the stream insertion operator <<<\/code> to add “a + ib<\/strong><\/em>” to the stream:<\/p>\n\n\n\ncout<<c<<endl;<\/code><\/pre>\n\n\n\nThe above statement should print “a + ib<\/strong><\/em>” followed by a newline where a = c.a<\/strong><\/em> and b = c.b<\/strong><\/em>.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe overloaded operator +<\/code> should receive two complex numbers (a + ib<\/strong><\/em> and c + ib<\/strong><\/em>) as parameters. It must return a single complex number.<\/p>\n\n\n\nThe overloaded operator <<<\/code> should add “a + ib<\/strong><\/em>” to the stream where a<\/em><\/strong> is the real part and i<\/strong> is the imaginary part of the complex number which is then passed as a parameter to the overloaded operator.<\/p>\n\n\n\n<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nAs per the problem statement, for the output, print “a + ib<\/strong><\/em>” followed by a newline where a = c.a<\/strong><\/em> and b = c.b<\/strong><\/em>.<\/p>\n\n\n\nSample Input<\/strong><\/p>\n\n\n\n3+i4\n5+i6<\/code><\/pre>\n\n\n\nSample Output<\/strong><\/p>\n\n\n\n8+i10<\/code><\/pre>\n\n\n\nExplanation<\/strong><\/p>\n\n\n\nGiven output after performing required operations (overloading + operator) is 8+i10<\/em>.<\/p>\n\n\n\n<\/span>Solution – Overload Operators in C++<\/strong><\/span><\/h2>\n\n\n\n<\/span>C++<\/strong><\/span><\/h3>\n\n\n\n\/\/Operator Overloading\n\n#include<iostream>\n\nusing namespace std;\n\nclass Complex\n{\npublic:\n int a,b;\n void input(string s)\n {\n int v1=0;\n int i=0;\n while(s[i]!='+')\n {\n v1=v1*10+s[i]-'0';\n i++;\n }\n while(s[i]==' ' || s[i]=='+'||s[i]=='i')\n {\n i++;\n }\n int v2=0;\n while(i<s.length())\n {\n v2=v2*10+s[i]-'0';\n i++;\n }\n a=v1;\n b=v2;\n }\n};\n\n\/\/Overload operators + and << for the class complex\n\/\/+ should add two complex numbers as (a+ib) + (c+id) = (a+c) + i(b+d)\n\/\/<< should print a complex number in the format \"a+ib\"\n\nComplex operator+(const Complex & X, const Complex & Y) {\n Complex Z {X.a + Y.a, X.b + Y.b};\n return Z;\n}\n\nostream & operator<< (ostream & out, const Complex & X) {\n if (X.b < 0) {\n out << X.a << \"-i\" << -X.b;\n } else if(X.b > 0) {\n out << X.a << \"+i\" << X.b;\n } else {\n out << X.a;\n }\n return out;\n}\n\nint main()\n{\n Complex x,y;\n string s1,s2;\n cin>>s1;\n cin>>s2;\n x.input(s1);\n y.input(s2);\n Complex z=x+y;\n cout<<z<<endl;\n}\n<\/pre>\n\n\n\n