Overloaded operators don't always have to be a member of a class - if you wish to customise behaviour for operators involving classes which you have no control over, (such as standard library classes) then you can overload the operator outside the class, eg,
Code:
#include <iostream>
#include <ostream>
using namespace std;
class MyDate
{
int day, month, year;
public:
MyDate(int d, int m, int y) : day(d), month(m), year(y) {}
friend ostream& operator<<(ostream& os, MyDate md);
};
ostream& operator<<(ostream& os, MyDate md)
{
os << md.day << '/' << md.month << '/' << md.year << '\n';
return os;
}
int main()
{
MyDate foo(15, 8, 1982);
cout << foo;
}
This snippet outputs: I find this method extremely useful for manipulating text i/o