Write a class Rational for performing arithmetic with fractions. Use integers to
ID: 3663939 • Letter: W
Question
Write a class Rational for performing arithmetic with fractions. Use integers to represent the (private) variables numerator and denominator. Provide a constructor function that enables an object of this class to be initialized, and which should contain default values in case no input is provided. The constructor should also guarantee that the fraction is stored in re duced form, so that for example) the inputs 2 and 4, for the fraction 4 will be stored with numerator 1 and denominator 2 Also provide public functions for the following operations Addition of two rational numbers, where the result should be stored in reduced form Subtraction of two rational numbers, where the result should be stored in reduced form Multiplication of two rational numbers, where the result should be stored in reduced form Division of two rational numbers, where the result should be stored in reduced form. Printing rational numbers Cusing cout in the form of a b where a is the numerator and b is the denominator Printing rational numbers in floating point formatExplanation / Answer
class Rational { public: Rational& operator += (const Rational& rhs); Rational& operator -= (const Rational& rhs); // same for *= and / friend const Rational operator+(const Rational& lhs, const Rational& rhs); friend const Rational operator-(const Rational& lhs, const Rational& rhs); // same for operator* and // operator/ }; const Rational operator+(const Rational& lhs, const Rational& rhs); // Friend functions const Rational operator+(const Rational& lhs, const Rational& rhs) { return Rational(lhs) += rhs; } const Rational operator-(const Rational& lhs, const Rational& rhs) { return Rational(lhs) -= rhs; } // Member functions Rational& Rational::operator+=(const Rational& rhs) { // Actual implementation here... } Rational& Rational::operator-=(const Rational& rhs) { // Actual implementation here... }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.