The ostream<< non-member function
Our non-member function operator<< gets called here.
ostream& operator<< (ostream& os, cube &c)
os << "cube of length " << c.length;
We can do this only because this function is a friend of cube; otherwise we wouldn’t get access to the private member “length”.
We return the “ostream” after we are done with it because we want to be able to “chain” the << operators ( cout << c1 << c2 << endl;).
Note that we do not write “cube::operator<<“ here; then it would be a member function, which we don’t want.
We may want to also say something about the color of our cube. As it happens (because it’s a well-designed class), the Color class already defines the << operator for itself. However, the color attribute is not with the cube class but with the “body” parent and so we need to go through publicly accessible member functions to obtain a copy of the color object.
ostream& operator<< (ostream& os, cube &c)
Color *cl = c.getColor();
os << "cube of length " << c.length