Question: C++ overloading operators Add both the specification and the implementation for a Fraction class to the following code, main.cpp, compiles and produces correct output. The
C++ overloading operators
Add both the specification and the implementation for a Fraction class to the following code, main.cpp, compiles and produces correct output. The Fraction class needs to store a numerator and a denominator as ints and has overloaded operators such as those that are necessary in the main() function.
main.cpp below
#includeusing namespace std; /* Class to represent a fraction (with it's numerator and denominator stored * separately) and corresponding operations on a fraction (e.g., adding another * fraction to this one, etc.). */ class Fraction { }; int main() { Fraction aFraction(2,3); Fraction bFraction(3,8); Fraction cFraction; cout << "aFraction: " << aFraction << endl; cout << "bFraction: " << bFraction << endl; cFraction = aFraction + bFraction; cout << aFraction << " + " << bFraction << " = " << cFraction << endl; cFraction = bFraction - aFraction; cout << bFraction << " - " << aFraction << " = " << cFraction << endl; cout << "++(" << aFraction << ") = "; cFraction = ++aFraction; cout << cFraction << endl; cout << "(" << bFraction << ")++ = "; cFraction = bFraction++; cout << cFraction << endl; cin >> aFraction; cout << "You entered: " << aFraction << endl; cin >> bFraction; cout << "You entered: " << bFraction << endl; if( aFraction == bFraction) { cout <<"The fractions are equal "; } else { if( aFraction > bFraction) { cout << aFraction << " is larger than " << bFraction << endl; }else if( aFraction < bFraction) { cout << aFraction << " is smaller than " << bFraction << endl; } else { cout << "ERROR: " << aFraction << " is not ==, > nor < than " << bFraction << endl; } } return 0; }
(run 1 ) input 1 :
2 3 4 5
output 1 :
aFraction: 2/3 bFraction: 3/8 2/3 + 3/8 = 25/24 3/8 - 2/3 = -7/24 ++(2/3) = 5/3 (3/8)++ = 3/8 Enter numerator Enter denominator You entered: 2/3 Enter numerator Enter denominator You entered: 4/5 2/3 is smaller than 4/5
(run 2) input 2:
9 1 8 2
output 2
aFraction: 2/3 bFraction: 3/8 2/3 + 3/8 = 25/24 3/8 - 2/3 = -7/24 ++(2/3) = 5/3 (3/8)++ = 3/8 Enter numerator Enter denominator You entered: 9/1 Enter numerator Enter denominator You entered: 8/2 9/1 is larger than 8/2
(run 3 ) input 3:
2 4 5 10
output 3:
aFraction: 2/3 bFraction: 3/8 2/3 + 3/8 = 25/24 3/8 - 2/3 = -7/24 ++(2/3) = 5/3 (3/8)++ = 3/8 Enter numerator Enter denominator You entered: 2/4 Enter numerator Enter denominator You entered: 5/10 The fractions are equal
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
