#include using namespace std; // ^ we have to include here because ostreams are defined in it class rational { public: // this constructor is called for both rational() and rational(int, int) rational( int t = 0, int b = 1 ): top( t ), bottom( b ) { } // copy contructor rational( const rational & r ): top( r.top ), bottom( r.bottom ) { } // these are normal accessor methods int numerator( ) const { return top; } int denominator( ) const { return bottom; } // this operator is overloaded as a member function // note that it only needs one parameter, as the other // rational being added is *this const rational operator+ ( const rational & r) const; // this is (has to be) a global function, rather than // a member function. since the function uses private // variables directly it must be declared as a friend // to the class friend ostream& operator<< ( ostream & out, const rational & r); private: // private variables. no great surprise int top; int bottom; };