// @topic T53025 12/03/2012 -- Operator Overloading // @brief class Rational implementation #include "rational.h" // rational.cpp Rational::Rational( int num, int denom ) : m_n( num ), m_d( denom ) { } Rational::Rational( Rational const& other ) : m_n( other.m_n ), m_d( other.m_d ) { std::cout << "copy of " << &other << " was made\n"; } /* ik-12/03/2010 commented out temporarily to demo non-member syntax Rational Rational::operator-() const { // uses 2-argument constructor return Rational( -m_n, m_d ); } */ Rational operator-( Rational const& rat ) { // uses 2-argument constructor return Rational( -rat.m_n, rat.m_d ); } std::ostream& operator<<( std::ostream& ostr, Rational const& rat ) { ostr << rat.m_n << '/' << rat.m_d; return ostr; } Rational& Rational::operator=( Rational const& other ) { if ( this == &other ) { // beware of self assignment! return *this; } m_n = other.m_n; m_d = other.m_d; return *this; } bool Rational::operator==( Rational const& other ) const { // This way, 1/2 == 2/4 return ( ( m_n * other.m_d ) == ( m_d * other.m_n ) ); }