Fraction Class In C++ Introduction In this assignment we will be writing a Fract
ID: 3695730 • Letter: F
Question
Fraction Class
In C++
Introduction
In this assignment we will be writing a Fraction class that overloads the operators for basic arithmetic operations (addition, subtraction, multiplication, and division).
Assignment
Write a Fraction class that overloads the arithmetic operators (+, -, *, /). Minimally, you fraction class should include the following methods that behave as directed:
• Fraction()
o This is a default constructor for the fraction class. It should load a value of 1 into the denominator and a value of 0 into the numerator
• Fraction(int numerator, int denominator)
o This is an overloaded version of the constructor. It accepts inputs for the numerator and denominator and creates a fraction that starts out with those values. Be mindful to sanitize the input to ensure a zero is not loaded into the denominator.
• int getNumerator()
o This is a getter method for the numerator.
• void setNumerator()
o This is a setter method for the numerator.
• int getDenominator()
o This is a getter method for the denominator.
• void setDenominator()
o This is a setter method for the denominator. Ensure the user enters valid values for this (no zeros allowed!).
• void Reduce()
o This method should reduce the contents of the fraction. The resultant fraction should be an equivalent, improper fraction in lowest terms (no mixed numbers).
• Fraction operator+(Fraction addMe)
o This method implements behavior for the addition operator. Your driver should clearly demonstrate the use of this operator.
• Fraction operator-(Fraction subtractMe)
o This method implements behavior for the subtraction operator. Your driver should clearly demonstrate the use of this operator.
• Fraction operator*(Fraction multiplyMe)
o This method implements the behavior for the subtraction operator. Your driver should clearly demonstrate the use of this operator.
• Fraction operator/(Fraction divideMe)
o This method implements behavior for the division operator. Your driver should clearly demonstrate the use of this operator.
Hints & Considerations
• Remember to call your reduction method every time you perform an operator on a fraction. This will ensure that the resultant Fraction is in lowest terms.
• Remember to sanitize inputs for your Fractions. A denominator of zero should never be allowed at anytime. If the client code attempts to load a zero into a denominator, you should print a message informing the user that this has occurred. Additionally, you should make a decision about the action the program takes in terms of picking a value to assign to the denominator when such an errors occurs. The best course of action is probably to change nothing and just display a message to the user.
Sample Driver
#include <iostream>
#include "fraction.h"
using namespace std;
int main() {
// make some Fractions
Fraction frac1;
Fraction frac2;
Fraction result;
// set values
frac1.setNumerator(10);
frac1.setDenominator(5);
frac2.setNumerator(18);
frac2.setDenominator(4);
// test operations
result = frac1 + frac2;
frac1.Print(); cout << " + "; frac2.Print(); cout << " = "; result.Print();
cout << endl;
result = frac1 - frac2;
frac1.Print(); cout << " - "; frac2.Print(); cout << " = "; result.Print();
cout << endl;
result = frac1 * frac2;
frac1.Print(); cout << " * "; frac2.Print(); cout << " = "; result.Print();
cout << endl;
result = frac1 / frac2;
frac1.Print(); cout << " / "; frac2.Print(); cout << " = "; result.Print();
cout << endl;
return 0;
}
Sample Output
(10 / 5) + (18 / 4) = (13 / 2)
(10 / 5) - (18 / 4) = (-5 / 2)
(10 / 5) * (18 / 4) = (9 / 1)
(10 / 5) / (18 / 4) = (4 / 9)
Press any key to continue
Explanation / Answer
#include #include class FLOAT { float no; public: FLOAT(){} void getdata() { coutno; } void putdata() { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.