The following code example is taken from the book
Object-Oriented Programming in C++
by Nicolai M. Josuttis, Wiley, 2002
© Copyright Nicolai M. Josuttis 2002
#ifndef FRACTION_HPP
#define FRACTION_HPP
// include standard header files
#include <iostream>
// **** BEGIN Namespace CPPBook ********************************
namespace CPPBook {
class Fraction {
private:
int numer;
int denom;
public:
/* default constructor, constructor from numerator, and
* constructor from numerator and denominator
*/
Fraction (int = 0, int = 1);
/* multiplication
* - new: global friend function, so that an automatic
* \breitevon- new: type conversion of the first operands is possible
*/
friend Fraction operator * (const Fraction&, const Fraction&);
// multiplicative assignment
const Fraction& operator *= (const Fraction&);
/* comparison
* - new: global friend function, so that an automatic
* \breitevon- new: type conversion of the first operands is possible
*/
friend bool operator < (const Fraction&, const Fraction&);
// output to a stream
void printOn (std::ostream&) const;
// input from a stream
void scanFrom (std::istream&);
// explicit type conversion to double
double toDouble () const;
};
/* operator *
* - new: global friend function
* - inline defined
*/
inline Fraction operator * (const Fraction& a, const Fraction& b)
{
/* simply multiply numerator and denominator
* - no reducing yet
*/
return Fraction (a.numer * b.numer, a.denom * b.denom);
}
/* standard output operator
* - overload globally and define inline
*/
inline
std::ostream& operator << (std::ostream& strm, const Fraction& f)
{
f.printOn(strm); // call member function for output
return strm; // return stream for chaining
}
/* standard input operator
* - overload globally and define inline
*/
inline
std::istream& operator >> (std::istream& strm, Fraction& f)
{
f.scanFrom(strm); // call element function for input
return strm; // return stream for chaining
}
} // **** END Namespace CPPBook ********************************
#endif // FRACTION_HPP