In C++, we’re motivated by the idea of operator overloading to discuss friend functions and classes. Friend functions are not methods of a class, but are able to access the private attributes and return a type that is not the class.

Like with methods, we must have a prototype in the class definition with the friend keyword (in the public space). But we cannot implement a friend function within the class — it must be implemented outside (and we are still able to access object members).

For instance:

// in class defn
public:
	friend ostream& operator<<(ostream& os, Complex rhs);
 
// somewhere else
ostream& operator<<(ostream& os, Complex rhs) {
	os << rhs.real << " + " << rhs.im << "j";
	return os;
}