Created
September 16, 2010 17:34
-
-
Save AndreaCrotti/582801 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // -*- compile-command: "g++ stream.cpp -o streams" -*- | |
| #include <iostream> | |
| using namespace std; | |
| class ComplexInt { | |
| private: | |
| int r, i; | |
| public: | |
| ComplexInt(int realPart = 0, int imagPart = 0); | |
| // this return a reference to an ostream in this case | |
| friend ostream& operator<<(ostream& s, const ComplexInt& c); | |
| // TODO: see if this is also correct | |
| friend ostream& operator>>(ostream& s, const ComplexInt& c); | |
| }; | |
| // this is for cout (print out things) | |
| ostream& operator<<(ostream& s, const ComplexInt& c) { | |
| s << c.r; | |
| if (c.i >= 0) | |
| s << '+'; | |
| s << c.i << 'i'; | |
| return s; | |
| } | |
| ostream& operator>>(ostream& s, const ComplexInt& c) { | |
| // see also what to do here | |
| } | |
| // then I can implement it | |
| int main(int argc, char *argv[]) | |
| { | |
| int i; | |
| ComplexInt c = ComplexInt::ComplexInt(1, 2); | |
| // the operator does not match correctly here | |
| cout << i << c; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment