C++ Program Consider the class Fraction, defined in Fraction.h and Fraction.cpp.
ID: 3760577 • Letter: C
Question
C++ Program
Consider the class Fraction, defined in Fraction.h and Fraction.cpp. A fraction object has a numerator (number above the fraction bar) and a denominator (number below the fraction bar) and one Fraction can be added, subtracted, divided or multiplied with another Fraction to form yet another Fraction object. The main.cpp program prompts the user for two fractions separated by the operation and works pretty well:
********************************************************************
Enter a fraction operand fraction ... For example: 1/2 + 1/4
1/4 + 1/4
answer = 8/16
Enter a fraction operand fraction ... For example: 1/2 + 1/4
****************************************************************************
Well, there's room for improvement--most of us would've thought one quarter plus one quarter was one half, not eight sixteenths! (Although its mathematically correct!)
Define another class, FractionReduced, which is derived from the Fraction class and has one more function: reduce(), which in the case of the fraction in the above addition example, changes the numerator to 1 and the denominator to 2.
Change the main program to declare the Fraction answer variable to be a FractionReduced object and call its reduce() function prior to its print() function.
Fraction.cpp
main.cpp
Fraction.h
Explanation / Answer
#include #ifndef FRACTION_H #define FRACTION_H using namespace std; class Fraction { private: int num; int denom; static int gcd(int a, int b); void reduce(); public: Fraction(int n=0, int d=1); Fraction(Fraction& f); ~Fraction(); Fraction& operator=(const Fraction& f); friend Fraction operator+(const Fraction& f1, const Fraction& f2); friend ostream& operatorRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.