Absolute C++ (4th Edition) part 34. KEY BENEFIT: C++ programming concepts and techniques are presented in a straightforward style using understandable language and code. KEY TOPICS: C++ Basics; Flow of Control; Function Basics; Parameters and Overloading; Arrays; Structures and Classes; Constructors; Operator Overloading, Friends, and References; Strings; Pointers and Dynamic Arrays; Separate Compilation and Namespaces; Streams and File I/O; Recursion; Inheritance; Polymorphism and Virtual Functions; Templates; Linked Data Structures; Exception Handling; Standard Template Library; Patterns and UML. MARKET: Useful for both beginning and intermediate C++ programmers. . | 334 Operator Overloading Friends and References friend istreamS operator istreamS inputstream Percent aPercent friend ostreamS operator ostreamS outputstream const Percents aPercent There would normally also be other members and friends. private int value Overloading and The input and output operators and can be overloaded just like any other operators. If you want the operators to behave as expected for cin cout and file I O then the value returned should be of type istream for input and ostream for output and the value should be returned by reference. Declarations class Class_Name . public . friend istreamS operator istream Parameters Class_Name Parameter_2 friend ostreamS operator ostreamS Parameter_3 const Class_NameS. Parameters . The operators do not need to be friends but cannot be members of the class being input or output. Definitions istreamS operator istreamS Parameters Class_NameS. Parameter_2 References and More Overloaded Operators 335 ostream operator ostream Parameter_3 const Class_Name Parameter_4 If you have enough accessor and mutator functions you can overload and as nonfriend functions. However it is natural and more efficient to define them as friends. Tip What Mode of Returned Value to Use A function can return a value of type T in four different ways By plain old value as in the function declaration T f By constant value as in the function declaration const T f By reference as in the function declaration T f By const reference as in the function declaration const T f There is not unanimous agreement on which to use when. So do not expect too much consistency in usage. Even when an author or programmer has a clear policy they seldom manage to follow it without exception. Still some points are clear. If you are returning a simple type like int or char there is no point in using a const when returning by value or by reference. So programmers typically do not use a const on the return type when it is a simple type. If you want the simple value .