// Project Rational // File rational.cpp #include using namespace std; #include "Rational.h" // Default initialization is to 0/1. Rational::Rational() { numer = 0; denom = 1; } // Integer initialization n is // represented as n/1. Rational::Rational(int n) { numer = n; denom = 1; } // Initialize to n/d. Rational::Rational(int n, int d) { numer = n; denom = d; } // Overloaded == operator. // a/b == c/d iff a*d == b*c. bool Rational::operator==(Rational other) { return (this -> numer * other.denom == this -> denom * other.numer); } // Overloaded + operator. // (a/b)+(c/d) := (a*d+b*c)/(b*d). Rational Rational::operator+(Rational other) { int n = numer * other.denom + denom * other.numer; int d = denom * other.denom; Rational r(n, d); return r; } // Overloaded * operator. // (a*b)/(c*d) := (a*c)/(b*d). Rational Rational::operator*(Rational other) { int n = numer * other.numer; int d = denom * other.denom; Rational r(n, d); return r; } // Print Rational object. void Rational::print() { cout << numer << "/" << denom; } // Overloaded file output function. ostream& operator<<(ostream &out, Rational r) { r.print(); return out; }