We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- C++
- Other Concepts
- Overload Operators
- Discussions
Overload Operators
Overload Operators
Sort by
recency
|
127 Discussions
|
Please Login in order to post a comment
//Operator Over
include
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
complex operator+(const complex &A, complex &B) { complex z; z.a = A.a + B.a; z.b = A.b + B.b; return z; }
ostream& operator<<(ostream& out, complex z) { out<>s1; cin>>s2; x.input(s1); y.input(s2); complex z=x+y; cout<
cpp
Complex operator+(Complex &l, Complex &r) { Complex t; t.a = l.a + r.a; t.b = l.b + r.b; return t; }
ostream& operator << (ostream &outS, Complex in){ outS << in.a << "+i"<< in.b << endl; return outS; }
Normally a complex number would be written with the i last, e.g. 1+2i as opposed to 1+i2 as we are required to do here.