Skip to content

Instantly share code, notes, and snippets.

@AndreaCrotti
Created September 16, 2010 17:34
Show Gist options
  • Save AndreaCrotti/582801 to your computer and use it in GitHub Desktop.
Save AndreaCrotti/582801 to your computer and use it in GitHub Desktop.
// -*- 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