Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create a class RationalNumber (fractions) with the following capabilities: a.) Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions
Create a class RationalNumber (fractions) with the following capabilities: a.) Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions that are not in reduced form and avoids negative denominators. b.) Overload the addition, subtraction, multiplication and division operators for this class. c.) Overload the relational and equality operators for this class. In addition to the capabilities outlined above, the RationalNumber class should overload the stream insertion (<<) and stream extraction (>>) operators. The stream extraction operator should prevent a 0 denominator in a fraction, reduce fractions that are not in reduced form, and prevent negative denominators. Negative or 0 denominators should be set to 1. Sample output 1 Enter a Rational Number (n/d): 1/3 Enter a Rational Number (n/d): 2/4 1/3 + 1/2 = 5/6 1/3 - 1/2 = -1/6 1/3 * 1/2 = 1/6 1/3 / 1/2 = 2/3 1/3 <= 1/2 according to the overloaded > operator 1/3 < 1/2 according to the overloaded >= operator 1/3 1/3 1/3 != 1/2 according to the overloaded == operator 1/3 != 1/2 according to the overloaded != operator Driver Program: #include "RationalNumber.h" int main() { // RationalNumber c( 1, 3 ), d( 2, 4 ), x; RationalNumber c, d, x; // test overloaded stream extraction operator cout cin >> c; cout cin >> d; x = c + d; // test overloaded operators + and = cout x = c - d; // test overloaded operators - and = cout x = c * d; // test overloaded operators * and = cout x = c / d; // test overloaded operators / and = cout // test overloaded > operator cout << c << ( ( c > d ) ? " > " : " << " according to the overloaded > operator "; // test overloaded >= operator cout << c << ( ( c >= d ) ? " >= " : " << " according to the overloaded >= operator "; // test overloaded cout << c << ( ( c < d ) ? " < " : " >= " ) // test overloaded cout << c << ( ( c <= d ) ? " <= " : " > " ) // test overloaded == operator cout // test overloaded != operator cout return 0; } // end main
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started