Nous considérons la classe rational
qui avait été présentée dans la partie 1 du premier TD.
#include<exception> #include<stdexcept> #include<iostream> class rational { private: long m_numerator; long m_denominator; template<typename charT, typename charTraits> friend inline std::basic_ostream<charT, charTraits>& operator << ( std::basic_ostream<charT, charTraits>& aStream, const rational& aRational); public: rational() : m_numerator(0), m_denominator(1) {} rational(long theNumerator) : m_numerator(theNumerator), m_denominator(1) {} rational(long theNumerator, long theDenominator) { if (theDenominator == 0) throw std::invalid_argument("denominator is 0"); if (theDenominator < 0) { theNumerator *= -1; theDenominator *= -1; } m_numerator = theNumerator; m_denominator = theDenominator; } rational(const rational& aRational): m_numerator(aRational.m_numerator), m_denominator(aRational.m_denominator) {} operator double() const { return (double)m_numerator / (double)m_denominator; } rational operator + (const rational& anotherRational) const { return rational(m_numerator * anotherRational.m_denominator + m_denominator * anotherRational.m_numerator, m_denominator * m_numerator); } rational operator - (const rational& anotherRational) const { return rational(m_numerator * anotherRational.m_denominator - m_denominator * anotherRational.m_numerator, m_denominator * m_numerator); } rational operator * (const rational& anotherRational) const { return rational(m_numerator * anotherRational.m_numerator, m_denominator * anotherRational.m_denominator); } rational operator / (const rational& anotherRational) const { return rational(m_numerator * anotherRational.m_denominator - m_denominator * anotherRational.m_numerator, m_denominator * m_numerator); } }; template<typename charT, typename charTraits> inline std::basic_ostream<charT, charTraits>& operator << ( std::basic_ostream<charT, charTraits>& aStream, const rational& aRational) { aStream << aRational.m_numerator; if (aRational.m_denominator != 1) aStream << "/" << aRational.m_denominator; return aStream; }
Recenser les opérateurs qui ont été surchargés ?
Définissez les opérations testant si deux nombres rationnelles sont égaux. Attention, il faut est capable de bien détecter que 1/2 == 2/4
sont égaux.
Typiquement, il faudra définir deux opérateurs :
class rational { ... public: bool operator == (const rational&) const; bool operator != (const rational&) const; };
''part4.cpp'' Fichier principal contient les fonctions de test et le main.
''rational.h'' Définition de la classe des nombres rationnels.