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.
//Operator Overloading#include<iostream>usingnamespacestd;classComplex{public:inta,b;voidinput(strings){intv1=0;inti=0;while(s[i]!='+'){v1=v1*10+s[i]-'0';i++;}while(s[i]==' '||s[i]=='+'||s[i]=='i'){i++;}intv2=0;while(i<s.length()){v2=v2*10+s[i]-'0';i++;}a=v1;b=v2;}};Complexoperator+(Complexc1,Complexc2){Complexc;c.a=(c1.a+c2.a);c.b=(c1.b+c2.b);returnc;}//b) Operator << should print a complex number in the format "a+istd::ostream&operator<<(std::ostream&outS,Complexcn){outS<<cn.a<<"+i"<<cn.b<<std::endl;returnoutS;}intmain(){Complexx,y;strings1,s2;cin>>s1;cin>>s2;x.input(s1);y.input(s2);Complexz=x+y;cout<<z<<endl;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Overload Operators
You are viewing a single comment's thread. Return to all comments →