#include // The 'Rational.hpp' file contains all the class and // function declarations, as well as the class definition. // // The 'Rational.cpp' file contains all the function // definitions. You can treat all of these as if they // were in this file. // // This example extends the previous Rational project. // The primary focus here is operator overloading. #include "Rational.hpp" // Function declaration (for this file only) int main(); // Function definition (for this file only) int main() { Rational p{ 3, 4 }; Rational q{ 1, 5 }; // The overloaded << operator allows Rational objects // to be inserted directly into an output stream. std::cout << "p = " << p << std::endl; std::cout << "q = " << q << std::endl; // Demonstrate the three overloads of the + operator. std::cout << std::endl << "Addition:" << std::endl; std::cout << "p + q = " << (p + q) << std::endl; std::cout << "p + 2 = " << (p + 2) << std::endl; std::cout << "2 + q = " << (2 + q) << std::endl; // Demonstrate the three overloads of the - operator. std::cout << std::endl << "Subtraction:" << std::endl; std::cout << "p - q = " << (p - q) << std::endl; std::cout << "p - 2 = " << (p - 2) << std::endl; std::cout << "2 - q = " << (2 - q) << std::endl; // Demonstrate the three overloads of the * operator. std::cout << std::endl << "Multiplication:" << std::endl; std::cout << "p*q = " << (p*q) << std::endl; std::cout << "p*2 = " << (p*2) << std::endl; std::cout << "2*q = " << (2*q) << std::endl; // Demonstrate the three overloads of the / operator. std::cout << std::endl << "Division:" << std::endl; std::cout << "p/q = " << (p/q) << std::endl; std::cout << "p/2 = " << (p/2) << std::endl; std::cout << "2/q = " << (2/q) << std::endl; // Demonstrate the unary + and - operators. // Unary + leaves the rational number unchanged, while // unary - returns its additive inverse. std::cout << std::endl << "Unary + and - operators:" << std::endl; std::cout << "+p = " << (+p) << std::endl; std::cout << "-p = " << (-p) << std::endl; // Use another representation of the same rational // number as p. This demonstrates that comparisons are // based on values, not on the stored numerator and // denominator. Rational r{ 6, 8 }; std::cout << std::endl << "For the comparison examples:" << std::endl; std::cout << "p = " << p << std::endl; std::cout << "q = " << q << std::endl; std::cout << "r = " << r << std::endl; // std::boolalpha causes Boolean values to be displayed // as 'true' and 'false' rather than 1 and 0. std::cout << std::boolalpha; // Demonstrate == with both true and false results. std::cout << std::endl << "Equality:" << std::endl; std::cout << "p == q: " << (p == q) << std::endl; std::cout << "p == r: " << (p == r) << std::endl; std::cout << "p == 1: " << (p == 1) << std::endl; std::cout << "1 == p: " << (1 == p) << std::endl; // Demonstrate != with both true and false results. std::cout << std::endl << "Inequality:" << std::endl; std::cout << "p != q: " << (p != q) << std::endl; std::cout << "p != r: " << (p != r) << std::endl; std::cout << "p != 1: " << (p != 1) << std::endl; std::cout << "1 != p: " << (1 != p) << std::endl; // Demonstrate < with both true and false results. std::cout << std::endl << "Less than:" << std::endl; std::cout << "p < q: " << (p < q) << std::endl; std::cout << "q < p: " << (q < p) << std::endl; std::cout << "p < 1: " << (p < 1) << std::endl; std::cout << "1 < p: " << (1 < p) << std::endl; // Demonstrate <=, including the equality case. std::cout << std::endl << "Less than or equal:" << std::endl; std::cout << "p <= q: " << (p <= q) << std::endl; std::cout << "p <= r: " << (p <= r) << std::endl; std::cout << "p <= 1: " << (p <= 1) << std::endl; std::cout << "1 <= p: " << (1 <= p) << std::endl; // Demonstrate >=, including the equality case. std::cout << std::endl << "Greater than or equal:" << std::endl; std::cout << "p >= q: " << (p >= q) << std::endl; std::cout << "p >= r: " << (p >= r) << std::endl; std::cout << "p >= 1: " << (p >= 1) << std::endl; std::cout << "1 >= p: " << (1 >= p) << std::endl; // Demonstrate > with both true and false results. std::cout << std::endl << "Greater than:" << std::endl; std::cout << "p > q: " << (p > q) << std::endl; std::cout << "q > p: " << (q > p) << std::endl; std::cout << "p > 1: " << (p > 1) << std::endl; std::cout << "1 > p: " << (1 > p) << std::endl; // The following functions are essentially unchanged // from the previous Rational project. std::cout << std::endl << "Other rational operations:" << std::endl; std::cout << "inverse( p ) = " << inverse( p ) << std::endl; Rational s{ -3, 4 }; std::cout << "s = " << s << std::endl; std::cout << "abs( s ) = " << abs( s ) << std::endl; // Demonstrate conversion to a double and to a string. std::cout << std::endl << "Conversions:" << std::endl; std::cout << "real( p ) = " << real( p ) << std::endl; std::cout << "to_string( p ) = " << to_string( p ) << std::endl; return 0; }