Implement the class called Fraction for performing arithmetic functions with fraction. The interface (.h) has been separated from the implementation (.cpp). You have been provided with a driver/client program that will use this class. You have also been provided a utilities.h file which contains the gcd, simplify and lcm (least common multiple) functions.
Fractions have the form Numerator / Denominator and will always be stored/displayed in the reduced form
Implement the Fraction class.
Fraction.h - Class declaration (provided for you)
Fraction.cpp - Class definition
Use int variables to represent the private data of the class. There are only two private members: an int to represent the numerator and an int to represent the denominator.
(a) Provide the following constructors:
default constructor - give initial values of 0 for the numerator and 1 for the denominator parameterized constructor - give initial values for the numerator and denominator from the constructor parameters. If the denominator is 0, use the default values for the numerator and denominator. Simplify the fraction.
copy constructor - initial values from an object that already exists
conversion constructor - converts a vector of two ints into a Function. Initial value for numerator will be the first int in the vector and initial value for denominator is second int in the vector.
(b) Provide a destructor
(c) Provide the following overloaded operators: +, -, *, /, =, ==, <, >.
Im currently attempting to implement this function
Fraction Fraction::operator+(const Fraction rhs) {
int sum = 0;
// If the denominators are the same
if (rhs.denominator == denominator) {
// Add the numerators
sum = rhs.numerator + numerator;
// Create a new Fraction constructor should simplify
return Fraction(sum, denominator);
} else {
// Find the LCM of the denominators
int lcmValue = lcm(denominator, rhs.denominator);
// Calculate the new numerator
sum = numerator * (lcmValue / denominator) + rhs.numerator * (lcmValue / rhs.denominator);
// Create a new Fraction; constructor will simplify
return Fraction(sum, lcmValue);
}
}
and all the test have pass for far for what ive implemented up until
bool testoperatorPlus() {
Fraction num1;
num1.setFraction(4,5);
Fraction num2;
num2.setFraction(2,3);
Fraction test;
cout << "adding 4/5 to 2/3, results should be 22/15\n";
test = num1 + num2;
cout << "The + operator returned correct values." << endl;
return true;
}
else {
cout << "The + operation did not return the correct values." << endl;
cout << "getNumerator() returned " << test.getNumerator() << " should have returned 22"<<endl;
cout << "getDenominator() returned " << test.getDenominator() << "should have returned 15"<<endl;
Assistant
code
return false;
}
}
it says
UNIT TEST - operator+
adding 4/5 to 2/3, results should be 22/15
The + operation did not return the correct values.
getNumerator() returned 0 should have returned 22
getDenominator() returned 1should have returned 15
You DID NOT PASS this unit test
Here are the rest of the classes being used
/*
testing.cpp
Created on: July 7, 2024
code
Author: Professor Kartchner
*/
#include
#include
#include
#include
#include "Fraction.h"
using namespace std;
while (getline(resultStream, str, delimiter)) {
result.push_back(str);
}
return result;
}
bool testDefaultConstructor() {
Fraction* testNode = new Fraction();
cout << "Creating a Fraction with the default constructor\n";
cout << "Should have values of 0 and 1\n";
if ((testNode->getNumerator() == 0) &&
(testNode->getDenominator() == 1)) {
code
cout << "Default constructor correctly initialized node and accessors return the correct values." << endl;
delete testNode;
return true;
}
else {
cout << "Default constructor incorrectly intialized node and/or accessors did not return the correct values." << endl;
cout << "getNumerator() returned " << testNode->getNumerator() << endl;
cout << "getDenominator() returned " << testNode->getDenominator() << endl;
delete testNode;
return false;
}
}
bool testParameterizedConstructor() {
Fraction* testNode = new Fraction(5,3);
cout << "Creating a fraction with the values 5 and 3\n";
if ((testNode->getNumerator() == 5) &&
(testNode->getDenominator() == 3)) {
else {
cout << "Conversion constructor incorrectly intialized to the correct values." << endl;
cout << "getNumerator returned " << test->getNumerator() << endl;
cout << "getDenominator() returned " << test->getDenominator() << endl;
delete test;
return false;
}
}
bool testConversionConstructorANDreduce() {
vector number;
number.push_back(6);
number.push_back(4);
Fraction* test = new Fraction(number);
cout << "Creating a fraction with values 6 and 4 in a vector\n";
cout << "Should reduce to 3 and 2\n";
if ((test->getNumerator() == 3) &&
(test->getDenominator() == 2)) {
cout << "The + operator returned correct values." << endl;
return true;
}
else {
cout << "The + operation did not return the correct values." << endl;
cout << "getNumerator() returned " << test.getNumerator() << " should have returned 22"<<endl;
cout << "getDenominator() returned " << test.getDenominator() << "should have returned 15"<<endl;
cout << "adding 3/4 to 6/8, results should be 3/2\n";
cout << "I used a .setFraction to set the results to 6 8, this has been written for you"
<< "and doesn't reduce the fraction automatically\n";
test = num1 + num2;
cout << "The + operator returned correct values." << endl;
return true;
}
else {
cout << "The + operation did not return the correct values." << endl;
cout << "getNumerator() returned " << test.getNumerator() << " should have returned 3"<<endl;
cout << "getDenominator() returned " << test.getDenominator() << "should have returned 2"<<endl;
cout << "The - operator returned correct values." << endl;
return true;
}
else {
cout << "The - operation did not return the correct values." << endl;
cout << "getNumerator() returned " << test.getNumerator() << " should have returned 2"<<endl;
cout << "getDenominator() returned " << test.getDenominator() << "should have returned 15"<<endl;
cout << "The - operator returned correct values." << endl;
return true;
}
else {
cout << "The - operation did not return the correct values." << endl;
cout << "getNumerator() returned " << test.getNumerator() << " should have returned 3"<<endl;
cout << "getDenominator() returned " << test.getDenominator() << "should have returned 5"<<endl;
cout << "The * operator returned correct values." << endl;
return true;
}
else {
cout << "The * operation did not return the correct values." << endl;
cout << "getNumerator() returned " << test.getNumerator() << " should have returned 8"<<endl;
cout << "getDenominator() returned " << test.getDenominator() << "should have returned 15"<<endl;
cout << "The * operator returned correct values." << endl;
return true;
}
else {
cout << "The * operation did not return the correct values." << endl;
cout << "getNumerator() returned " << test.getNumerator() << " should have returned 4"<<endl;
cout << "getDenominator() returned " << test.getDenominator() << "should have returned 3"<<endl;
code
return false;
}
}
bool testoperatorLessThan() {
Fraction num1;
num1.setFraction(4,5);
Fraction num2;
num2.setFraction(2,3);
Fraction test;
//test for catching umimplemented < operator
if(!(num1<num2) && !(num2<num1)){
cout << "< operator is not implemented";
return false;
}
cout << "Creating fractions of 4/5 and 2/3, 4/5 < 2/3 is FALSE\n";
if(num1 < num2){
cout << "Incorrectly returned true\n";
return false;
}
else {
cout << "Correctly returned false\n";
return true;
}
}
bool testoperatorGreaterThan() {
Fraction num1;
num1.setFraction(4,5);
Fraction num2;
num2.setFraction(2,3);
Fraction test;
//test for catching umimplemented < operator
if(!(num1<num2) && !(num2<num1)){
cout << "< operator is not implemented";
return false;
}
cout << "Creating fractions of 4/5 and 2/3, 4/5 > 2/3 is TRUE\n";
if(num2 < num1){
cout << "Correctly returned true\n";
return true;
}
else {
cout << "Incorrectly returned false\n";
return false;
}
}
cout << "The / operator returned correct values." << endl;
return true;
}
else {
cout << "The / operation did not return the correct values." << endl;
cout << "getNumerator() returned " << test.getNumerator() << " should have returned 6"<<endl;
cout << "getDenominator() returned " << test.getDenominator() << "should have returned 5"<<endl;
cout << "The = operator returned correct values." << endl;
return true;
}
else {
cout << "The = operation did not return the correct values." << endl;
cout << "num1.getNumerator() returned " << num1.getNumerator() << " should have returned 2"<<endl;
cout << "num1.getDenominator() returned " << num1.getDenominator() << "should have returned 3"<<endl;
cout << "num2.getNumerator() returned " << num2.getNumerator() << " should have returned 2"<<endl;
cout << "num2.getDenominator() returned " << num2.getDenominator() << "should have returned 3"<<endl;
code
return false;
}
}
int main() {
bool passed;
int score = 0;
cout << "UNIT TEST - Default Constructor" << endl;
passed = testDefaultConstructor();
if(passed){cout << "You PASSED this unit test\n\n";score+=3;} else cout << "You DID NOT PASS this unit test\n\n";
code
cout << "UNIT TEST - Parameterized Constructor" << endl;
passed = testParameterizedConstructor();
if(passed){cout << "You PASSED this unit test\n\n";score+=3;} else cout << "You DID NOT PASS this unit test\n\n";
cout << "UNIT TEST - Parameterized Constructor AND Reduce" << endl;
passed = testParameterizedConstructorAndReduce();
if(passed) {cout << "You PASSED this unit test\n\n";score +=4;} else cout << "You DID NOT PASS this unit test\n\n";
cout << "UNIT TEST - Copy Constructor" << endl;
passed = testCopyConstructor();
if(passed){cout << "You PASSED this unit test\n\n";score+=4;} else cout << "You DID NOT PASS this unit test\n\n";
cout << "UNIT TEST - Conversion Constructor" << endl;
passed = testConversionConstructor();
if(passed){cout << "You PASSED this unit test\n\n";score+=3;} else cout << "You DID NOT PASS this unit test\n\n";
cout << "UNIT TEST - Conversion Constructor AND reduce" << endl;
passed = testConversionConstructorANDreduce();
if(passed){cout << "You PASSED this unit test\n\n";score+=3;} else cout << "You DID NOT PASS this unit test\n\n";
cout << "UNIT TEST - operator+" << endl;
passed = testoperatorPlus();
if(passed){cout << "You PASSED this unit test\n\n";score+=5;} else cout << "You DID NOT PASS this unit test\n\n";
cout << "UNIT TEST - operator+ AND reduce" << endl;
passed = testoperatorPlusANDreduce();
if(passed){cout << "You PASSED this unit test\n\n";score+=5;} else cout << "You DID NOT PASS this unit test\n\n";
cout << "UNIT TEST - operator-" << endl;
passed = testoperatorMinus();
if(passed){cout << "You PASSED this unit test\n\n";score+=5;} else cout << "You DID NOT PASS this unit test\n\n";
cout << "UNIT TEST - operator- AND reduce" << endl;
passed = testoperatorMinusANDreduce();
if(passed){cout << "You PASSED this unit test\n\n";score+=5;} else cout << "You DID NOT PASS this unit test\n\n";
cout << "UNIT TEST - operator*" << endl;
passed = testoperatorMult();
if(passed){cout << "You PASSED this unit test\n\n";score+=5;} else cout << "You DID NOT PASS this unit test\n\n";
cout << "UNIT TEST - operator* AND reduce" << endl;
passed = testoperatorMultANDreduce();
if(passed){cout << "You PASSED this unit test\n\n";score+=5;} else cout << "You DID NOT PASS this unit test\n\n";
cout << "UNIT TEST - operator<" << endl;
passed = testoperatorLessThan();
if(passed){cout << "You PASSED this unit test\n\n";score+=6;} else cout << "You DID NOT PASS this unit test\n\n";
cout << "UNIT TEST - operator>" << endl;
passed = testoperatorGreaterThan();
if(passed){cout << "You PASSED this unit test\n\n";score+=6;} else cout << "You DID NOT PASS this unit test\n\n";
cout << "UNIT TEST - operator/" << endl;
passed = testoperatorDivide();
if(passed){cout << "You PASSED this unit test\n\n";score += 6;} else cout << "You DID NOT PASS this unit test\n\n";
cout << "UNIT TEST - operator==" << endl;
passed = testoperatorEquivalent();
if(passed){cout << "You PASSED this unit test\n\n";score+=6;} else cout << "You DID NOT PASS this unit test\n\n";
cout << "UNIT TEST - operator=" << endl;
passed = testoperatorAssign();
if(passed){cout << "You PASSED this unit test\n\n";score +=6;} else cout << "You DID NOT PASS this unit test\n\n";
cout << "You received "<<score<<" out of 80 points";
return 0;
}
/*
Fraction.cpp
Created on: June 29, 2024
code
Author: Professor Kartchner
*/
#include "Fraction.h"
#include "utilities.h"
#include
#include
using namespace std;
//Operators
Fraction Fraction::operator+(const Fraction rhs) {
int sum = 0;
// If the denominators are the same
if (rhs.denominator == denominator) {
// Add the numerators
sum = rhs.numerator + numerator;
// Create a new Fraction constructor should simplify
return Fraction(sum, denominator);
} else {
// Find the LCM of the denominators
int lcmValue = lcm(denominator, rhs.denominator);
// Calculate the new numerator
sum = numerator * (lcmValue / denominator) + rhs.numerator * (lcmValue / rhs.denominator);
// Create a new Fraction; constructor will simplify
return Fraction(sum, lcmValue);
}
}
istream& operator>>(istream& in, Fraction& number) {
return in;
}
#ifndef UTILITIES_H
#define UTILITIES_H
#include
using namespace std;
//returns the greatest common demoninator
int gcd(int a, int b) {
if (a < 0) a = -a;
if (b < 0) b = -b;
code
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
//returns the least common multiple
int lcm(int a, int b) {
return ((a * b) / gcd(a, b));
}
// returns the numerator and denominator for the reduced fraction
vector simplify(int numerator, int denominator) {
vector result;
int factor = gcd(numerator, denominator);
result.push_back(numerator/factor);
result.push_back(denominator/factor);
return result;
}
#endif
#include
#include
#include
#include "Fraction.h"
using namespace std;
// Output menu option, prompt users for valid selection
while (menuOp != 'e') {
cout << "MENU" << endl;
cout << "For menu items a-b, you must have your extraction operator working" << endl;
cout << "a. Input fraction A" << endl;
cout << "b. Input fraction B" << endl << endl;
cout << "For menu items 1-9, you must have entered a fraction A and B and have your insertion operator working" << endl << endl;
cout << "1. A + B" << endl;
cout << "2. A - B" << endl;
cout << "3. B - A" << endl;
cout << "4. A * B" << endl;
cout << "5. A < B" << endl;
cout << "6. A > B" << endl;
cout << "7. A == B" << endl;
cout << "8. A = B" << endl;
cout << "9. A / B" << endl;
cout << "p - Print A and B" << endl;
cout << "e - Exit" << endl << endl;
cout << "Choose an option: ";
cin >> menuOp;
if (menuOp != 'e') {
while (menuOp != 'a' && menuOp != 'b' && menuOp != '1' &&
menuOp != '2' && menuOp != '3' && menuOp != '4' && menuOp != '5' &&
menuOp != '6' && menuOp != '7' && menuOp != '8' && menuOp != '9' &&
menuOp != 'e' && menuOp != 'p') {
cout << "Choose an option: ";
cin >> menuOp;
}
switch (menuOp) {
case 'a':
cin.ignore();
cout << "Enter the numerator followed by the denominator, separated by a" <<
" space for Fraction A" << endl;
cin >> num1;
cout << endl;
break;
case 'b':
// Output playlist messaging
cin.ignore();
cout << "Enter the numerator followed by the denominator, separated by a" <<
" space for Fraction B" << endl;
cin >> num2;
cout << endl;
break;
case '1':
cin.ignore();
if (!(num1.isDefault() || num2.isDefault())) {
result = num1 + num2;
cout << endl << endl << num1 << " + " << num2 << " = " << result << endl << endl;
}
else cout << "You have not entered the two fractions A and B." << endl;
break;
case '2':
cin.ignore();
if (!(num1.isDefault() || num2.isDefault())) {
result = num1 - num2;
cout << endl << endl << num1 << " - " << num2 << " = " << result << endl << endl;
}
else cout << "You have not entered the two fractions A and B." << endl;
break;
case '3':
cin.ignore();
if (!(num1.isDefault() || num2.isDefault())) {
result = num2 - num1;
cout << endl << endl << num2 << " - " << num1 << " = " << result << endl << endl;
}
else cout << "You have not entered the two fractions A and B." << endl;
break;
case '4':
cin.ignore();
if (!(num1.isDefault() || num2.isDefault())) {
result = num1 * num2;
cout << endl << endl << num1 << " * " << num2 << " = " << result << endl << endl;
}
else cout << "You have not entered the two fractions A and B." << endl;
break;
case '5':
cin.ignore();
if (!(num1.isDefault() || num2.isDefault())) {
cout << endl << endl << "A=" << num1 << " B=" << num2 << endl;
cout << "A < B is: ";
if (num1 < num2) cout << "true" << endl << endl;
else cout << "false" << endl << endl;
}
else cout << "You have not entered the two fractions A and B." << endl;
break;
case '6':
cin.ignore();
if (!(num1.isDefault() || num2.isDefault())) {
cout << endl << endl << "A=" << num1 << " B=" << num2 << endl;
cout << "A > B is: ";
if (num2 < num1) cout << "true"<< endl << endl;
else cout << "false" << endl << endl;
}
else cout << "You have not entered the two fractions A and B." << endl;
break;
case '7':
cin.ignore();
if (!(num1.isDefault() || num2.isDefault())) {
cout << endl << endl << "A=" << num1 << " B=" << num2 << endl;
cout << "A == B is: ";
if(num1 == num2) cout << "true"<< endl << endl;
else cout << "false" << endl << endl;
}
else cout << "You have not entered the two fractions A and B." << endl;
break;
case '8':
cin.ignore();
if (!(num1.isDefault() || num2.isDefault())) {
cout << endl << endl << "A=" << num1 << " B=" << num2 << endl;
num1 = num2;
cout << "A = B: A=" << num1 << " B=" << num2 << endl << endl;
}
else cout << "You have not entered the two fractions A and B." << endl;
break;
case '9':
cin.ignore();
if (!(num1.isDefault() || num2.isDefault())) {
cout << endl << endl << "A=" << num1 << " B=" << num2 << endl;
result = num1/num2;
cout << endl << endl << num1 << " / " << num2 << " = " << result << endl << endl;
}
else cout << "You have not entered the two fractions A and B." << endl;
break;
case 'p':
cin.ignore();
if (!(num1.isDefault() || num2.isDefault())) {
cout << endl << endl << "A=" << num1 << " B=" << num2 << endl << endl;
}
else cout << "You have not entered the two fractions A and B." << endl;
break;
default:
cout << "You have entered an invalid option" << endl;
}
}
}
}// end of PrintMenu
int main() {
code
// Output play list menu options
PrintMenu();
return 0;
}
I need help figuring out why it's returning 0 and 1
It looks like the issue you're encountering stems from mismatches between the private member variable names in your Fraction class declaration (in Fraction.h) and the names you're using in your Fraction.cpp implementation.
In C++, if you're accessing class member variables, especially when their names are the same as function parameters or local variables, you need to be careful to use this-> to distinguish between them, or choose distinct names.
Given that your Fraction class's private member variables are likely named differently than just numerator and denominator (perhaps they're named _numerator and _denominator, or have some other prefix or suffix), when you're using numerator and denominator in your functions, the code might be referring to local or undeclared variables instead of your class's private members.
Here's how you can fix the issue:
Check your Fraction.h file: Locate the actual names of your private member variables. For example, they might be _numerator and _denominator, or numerator_ and denominator_.
Update your Fraction.cpp file: Wherever you access these member variables, make sure you're using the correct names.
For instance, if your member variables are named _numerator and _denominator, you should use those names in your function implementations.
Use this-> when necessary: To explicitly refer to the class's member variables, especially when there's a naming conflict with function parameters or local variables, use this->. For example:
cpp
// In your operator+ implementation
if (rhs.getDenominator() == this->denominator_) {
// Access member variables using this->memberVariableName
}
Here's the corrected operator+ function:
cpp
Fraction Fraction::operator+(const Fraction& rhs) const {
int sum = 0;
// If the denominators are the same
if (rhs.denominator_ == this->denominator_) {
// Add the numerators
sum = rhs.numerator_ + this->numerator_;
// Create a new Fraction constructor should simplify
return Fraction(sum, this->denominator_);
} else {
// Find the LCM of the denominators
int lcmValue = lcm(this->denominator_, rhs.denominator_);
// Calculate the new numerator
sum = this->numerator_ * (lcmValue / this->denominator_) + rhs.numerator_ * (lcmValue / rhs.denominator_);
// Create a new Fraction; constructor will simplify
return Fraction(sum, lcmValue);
}
}
Additionally, here's advice on a few other points:
Pass by reference: In your operator overloads, you should pass objects as const Fraction& instead of const Fraction. This avoids unnecessary copying and is standard practice.
Mark member functions as const when appropriate: If a member function doesn't modify the object, mark it as const. This applies to operator overloads like operator+.
Simplify your code where possible: If your simplify function and constructors are correctly implemented, you might not need to check if the denominators are the same when adding fractions. The general formula works in all cases.
Finally, ensure that your setFraction function and other member functions follow similar patterns, using this-> and the correct variable names to access the class's private members.
After making these changes, your unit test for the operator+ should pass, and the output should correctly display the numerator and denominator you expect (22 and 15 in this case).