240 likes | 312 Views
IsEquals Operator. Code Trace. // main.cpp ... int main() { if(f==g) ... // fraction.h ... class Fraction { friend bool operator==(const Fraction & lhs, const Fraction & rhs ); ... // fraction.cpp bool operator==(const Fraction & lhs, const Fraction & rhs )
E N D
Code Trace // main.cpp ... int main() { if(f==g) ... // fraction.h ... class Fraction { friend bool operator==(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator==(const Fraction & lhs, const Fraction & rhs) • { • return lhs.m_Numerator*rhs.m_Denominator==rhs.m_Numerator*lhs.m_Denominator; • }
Code Trace // main.cpp ... int main() { if(f==g) ... // fraction.h ... class Fraction { friend bool operator==(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator==(const Fraction & lhs, const Fraction & rhs) • { • return lhs.m_Numerator*rhs.m_Denominator==rhs.m_Numerator*lhs.m_Denominator; • } f in main g in main m_Numerator: 9 m_Denominator: 64 m_Numerator: 9 m_Denominator: 32
Code Trace f in main lhs in operator== g in main rhs in operator== // main.cpp ... int main() { if(f==g) ... // fraction.h ... class Fraction { friend bool operator==(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator==(const Fraction & lhs, const Fraction & rhs) • { • return lhs.m_Numerator*rhs.m_Denominator==rhs.m_Numerator*lhs.m_Denominator; • } m_Numerator: 9 m_Denominator: 64 m_Numerator: 9 m_Denominator: 32
Code Trace f in main lhs in operator== g in main rhs in operator== // main.cpp ... int main() { if(f==g) ... // fraction.h ... class Fraction { friend bool operator==(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator==(const Fraction & lhs, const Fraction & rhs) • { • return lhs.m_Numerator*rhs.m_Denominator==rhs.m_Numerator*lhs.m_Denominator; • } m_Numerator: 9 m_Denominator: 64 m_Numerator: 9 m_Denominator: 32 64
Code Trace f in main lhs in operator== g in main rhs in operator== // main.cpp ... int main() { if(f==g) ... // fraction.h ... class Fraction { friend bool operator==(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator==(const Fraction & lhs, const Fraction & rhs) • { • return lhs.m_Numerator*rhs.m_Denominator==rhs.m_Numerator*lhs.m_Denominator; • } m_Numerator: 9 m_Denominator: 64 m_Numerator: 9 m_Denominator: 32 9 64
Code Trace f in main lhs in operator== g in main rhs in operator== // main.cpp ... int main() { if(f==g) ... // fraction.h ... class Fraction { friend bool operator==(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator==(const Fraction & lhs, const Fraction & rhs) • { • return lhs.m_Numerator*rhs.m_Denominator==rhs.m_Numerator*lhs.m_Denominator; • } m_Numerator: 9 m_Denominator: 64 m_Numerator: 9 m_Denominator: 32 576
Code Trace f in main lhs in operator== g in main rhs in operator== // main.cpp ... int main() { if(f==g) ... // fraction.h ... class Fraction { friend bool operator==(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator==(const Fraction & lhs, const Fraction & rhs) • { • return lhs.m_Numerator*rhs.m_Denominator==rhs.m_Numerator*lhs.m_Denominator; • } m_Numerator: 9 m_Denominator: 64 m_Numerator: 9 m_Denominator: 32 32 576
Code Trace f in main lhs in operator== g in main rhs in operator== // main.cpp ... int main() { if(f==g) ... // fraction.h ... class Fraction { friend bool operator==(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator==(const Fraction & lhs, const Fraction & rhs) • { • return lhs.m_Numerator*rhs.m_Denominator==rhs.m_Numerator*lhs.m_Denominator; • } m_Numerator: 9 m_Denominator: 64 m_Numerator: 9 m_Denominator: 32 9 32 576
Code Trace f in main lhs in operator== g in main rhs in operator== // main.cpp ... int main() { if(f==g) ... // fraction.h ... class Fraction { friend bool operator==(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator==(const Fraction & lhs, const Fraction & rhs) • { • return lhs.m_Numerator*rhs.m_Denominator==rhs.m_Numerator*lhs.m_Denominator; • } m_Numerator: 9 m_Denominator: 64 m_Numerator: 9 m_Denominator: 32 576 288
Code Trace f in main lhs in operator== g in main rhs in operator== // main.cpp ... int main() { if(f==g) ... // fraction.h ... class Fraction { friend bool operator==(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator==(const Fraction & lhs, const Fraction & rhs) • { • return lhs.m_Numerator*rhs.m_Denominator==rhs.m_Numerator*lhs.m_Denominator; • } m_Numerator: 9 m_Denominator: 64 m_Numerator: 9 m_Denominator: 32 576 288 false
Code Trace // main.cpp ... int main() { if(f!=g) ... // fraction.h ... class Fraction { friend bool operator!=(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator!=(const Fraction & lhs, const Fraction & rhs) • { • return lhs.m_Numerator*rhs.m_Denominator!=rhs.m_Numerator*lhs.m_Denominator; • }
Code Trace // main.cpp ... int main() { if(f!=g) ... // fraction.h ... class Fraction { friend bool operator!=(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator!=(const Fraction & lhs, const Fraction & rhs) • { • return lhs.m_Numerator*rhs.m_Denominator!=rhs.m_Numerator*lhs.m_Denominator; • } f in main g in main m_Numerator: 9 m_Denominator: 64 m_Numerator: 9 m_Denominator: 32
Code Trace f in main lhs in operator!= g in main rhs in operator!= // main.cpp ... int main() { if(f!=g) ... // fraction.h ... class Fraction { friend bool operator!=(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator!=(const Fraction & lhs, const Fraction & rhs) • { • return lhs.m_Numerator*rhs.m_Denominator!=rhs.m_Numerator*lhs.m_Denominator; • } m_Numerator: 9 m_Denominator: 64 m_Numerator: 9 m_Denominator: 32
Code Trace f in main lhs in operator!= g in main rhs in operator!= // main.cpp ... int main() { if(f!=g) ... // fraction.h ... class Fraction { friend bool operator!=(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator!=(const Fraction & lhs, const Fraction & rhs) • { • return lhs.m_Numerator*rhs.m_Denominator!=rhs.m_Numerator*lhs.m_Denominator; • } m_Numerator: 9 m_Denominator: 64 m_Numerator: 9 m_Denominator: 32 64
Code Trace f in main lhs in operator!= g in main rhs in operator!= // main.cpp ... int main() { if(f!=g) ... // fraction.h ... class Fraction { friend bool operator!=(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator!=(const Fraction & lhs, const Fraction & rhs) • { • return lhs.m_Numerator*rhs.m_Denominator!=rhs.m_Numerator*lhs.m_Denominator; • } m_Numerator: 9 m_Denominator: 64 m_Numerator: 9 m_Denominator: 32 9 64
Code Trace f in main lhs in operator!= g in main rhs in operator!= // main.cpp ... int main() { if(f!=g) ... // fraction.h ... class Fraction { friend bool operator!=(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator!=(const Fraction & lhs, const Fraction & rhs) • { • return lhs.m_Numerator*rhs.m_Denominator!=rhs.m_Numerator*lhs.m_Denominator; • } m_Numerator: 9 m_Denominator: 64 m_Numerator: 9 m_Denominator: 32 576
Code Trace f in main lhs in operator!= g in main rhs in operato!r= // main.cpp ... int main() { if(f!=g) ... // fraction.h ... class Fraction { friend bool operator!=(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator!=(const Fraction & lhs, const Fraction & rhs) • { • return lhs.m_Numerator*rhs.m_Denominator!=rhs.m_Numerator*lhs.m_Denominator; • } m_Numerator: 9 m_Denominator: 64 m_Numerator: 9 m_Denominator: 32 32 576
Code Trace f in main lhs in operator!= g in main rhs in operator!= // main.cpp ... int main() { if(f!=g) ... // fraction.h ... class Fraction { friend bool operator!=(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator!=(const Fraction & lhs, const Fraction & rhs) • { • return lhs.m_Numerator*rhs.m_Denominator!=rhs.m_Numerator*lhs.m_Denominator; • } m_Numerator: 9 m_Denominator: 64 m_Numerator: 9 m_Denominator: 32 9 32 576
Code Trace f in main lhs in operator!= g in main rhs in operator!= // main.cpp ... int main() { if(f!=g) ... // fraction.h ... class Fraction { friend bool operator!=(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator!=(const Fraction & lhs, const Fraction & rhs) • { • return lhs.m_Numerator*rhs.m_Denominator!=rhs.m_Numerator*lhs.m_Denominator; • } m_Numerator: 9 m_Denominator: 64 m_Numerator: 9 m_Denominator: 32 576 288
Code Trace f in main lhs in operator!= g in main rhs in operator!= // main.cpp ... int main() { if(f!=g) ... // fraction.h ... class Fraction { friend bool operator!=(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator!=(const Fraction & lhs, const Fraction & rhs) • { • return lhs.m_Numerator*rhs.m_Denominator!=rhs.m_Numerator*lhs.m_Denominator; • } m_Numerator: 9 m_Denominator: 64 m_Numerator: 9 m_Denominator: 32 576 288 true
Alternative Definition // main.cpp ... int main() { if(f!=g) ... // fraction.h ... class Fraction { friend bool operator!=(const Fraction & lhs, const Fraction & rhs); ... // fraction.cpp • bool operator==(const Fraction & lhs, const Fraction & rhs) • { • return !(lhs==rhs); • }