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
|
132 Discussions
|
Please Login in order to post a comment
class Complex { private: int real; int imag;
}; int main() {
}
include
include
using namespace std;
class Complex { public: int a, b; friend ostream& operator << (ostream& os, Complex obj); friend Complex operator + (Complex& a_obj, Complex& b_obj); };
ostream& operator << (ostream& os, Complex obj) { os << obj.a << "+i" << obj.b; return os; }
Complex operator + (Complex& a_obj, Complex& b_obj) { Complex c_obj; c_obj.a = a_obj.a + b_obj.a; c_obj.b = a_obj.b + b_obj.b; return c_obj; }
void call_complex(string& s, int& a, int& b) { int num = 0; int i = 1; for (auto x : s) { if (x == '+') { continue; } else if (x == 'i') { a = num; num = 0; i = 1; continue; } num = num * i + (x - 48); i = 10; } b = num; }
int main() {
}
include
include
include
include
include
using namespace std;
class complex { private: int real; int img;
};
int main() { complex c1,c2,c3; cin>>c1; cin>>c2;
} void complex::setReal(int r){real=r;} void complex::setImg(int i){img=i;} int complex::getReal(){return real;} int complex::getImg(){return img;} ostream & operator<<(ostream & out,complex c) { out<>(istream & in,complex &c) { in>>c.real; in.ignore(1,'+'); in.ignore(1,'i'); in>>c.img;
}
Read the editorial soultion, instead of having an
input()
function, theoperator>>
should be overloaded to allow for direct input of aComplex
object: