The implementation file includes definitions for the public member functions and external convenience functions. Notice that the definitions for the member functions use the scope resolution operator (::) so that they are defined within the Rational class.
// // rational3.C // // Defining the class rational // -- third attempt, separate compilation // // by A.B. Maccabe 2/27/97 // #include "rational3.h" // Constructors Rational::Rational( int n = 0, int d = 1 ) { num = n; denom = d; } // I/O void Rational::read( istream &in ) { char slash; in >> num >> slash >> denom; } void Rational::write( ostream &out ) { out << num << '/' << denom; } // arithmetic Rational Rational::neg( ) { return Rational( -num, denom ); } Rational Rational::add( Rational rat ) { return Rational( num*rat.denom + denom*rat.num, denom*rat.denom ); } Rational Rational::sub( Rational rat ) { return add( rat.neg() ); } Rational Rational::mpy( Rational rat ) { return Rational( num*rat.num, denom*rat.denom ); } Rational Rational::div( Rational rat ) { return Rational( num*rat.denom, denom*rat.num ); } // define overloaded operators Rational operator +( Rational r1, Rational r2 ) { return r1.add(r2); } ostream & operator <<( ostream &os, Rational r ) { r.write( os ); return os; } istream & operator >>( istream &is, Rational &r ) { r.read( is ); return is; }