Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ --------------- Question 2 Create a class to handle fractions, using multipl

ID: 3709181 • Letter: C

Question

c++ ---------------

Question 2 Create a class to handle fractions, using multiple cpp files and appropriate.h file. Both the numerator and denominator are integers, positive or negative. Your class must have a default constructor that uses a constructor list to initialize the values. The fraction must default to 1/1 Overload operators ,-, +,-so they work with your Fraction class. You will most likely need to create a private method to convert fractions to a common denominator so that, for example, the class can work with 1/2 and -4/-8, and another private method to reduce the fraction to lowest form (4/8 would print as 1/2). All output should show the fraction in lowest form You will need a method to display the fraction as a fraction. If the fraction has numerator 4 and denominator 7 then it should display 4/7 Bonus points (1 ea) for overloading *. =.

Explanation / Answer

// File Name: FractionOperatorOverloading.cpp

#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
//Class Fraction definition
class Fraction
{
//Data member for numerator and denominator
int numerator;
int denominator;
public:
// Default constructor
Fraction()
{
numerator = denominator = 1;
}// End of default constructor

// Input redirection operator overloaded
friend ostream& operator<<(ostream &os, const Fraction &f)
{
// Stores numerator, operator symbol (/) and denominator in os stream
os<<f.numerator<<" / " <<f.denominator;
// Returns output stream
return os;
}// End of function

// Output redirection operator overloaded
friend istream& operator >> (istream &is, Fraction &f)
{
// To store symbol
char ch;
// Stores numerator in is stream
is>>f.numerator;
// To discard fraction symbol
is>>ch;
// Stores denominator in is stream
is>>f.denominator;
// Returns input stream
return is;
}// End of function

// Overloading + operator
// Addition operator overloaded
Fraction operator + (Fraction f)
{
// Creates a temporary object of Fraction class
Fraction t;
// Multiplies the implicit object numerator with parameter object denominator
// and multiplies the parameter object numerator with implicit object denominator
// Add both the result and store it in the temporary object numerator
t.numerator = numerator * f.denominator + f.numerator * denominator;
// Multiply implicit object denominator with parameter object denominator and store it in the temporary object denominator
t.denominator = denominator * f.denominator;
// Return the result object
return t;
}// End of function

// Overloading - operator
// Subtraction operator overloaded
Fraction operator - (Fraction f)
{
// Creates a temporary object of Fraction class
Fraction t;
// Multiplies the implicit object numerator with parameter object denominator
// and multiplies the parameter object numerator with implicit object denominator
// Subtracts both the result and store it in the temporary object numerator
t.numerator = numerator * f.denominator - f.numerator * denominator;
// Multiply implicit object denominator with parameter object denominator and store it in the temporary object denominator
t.denominator = denominator * f.denominator;
// Return the result object
return t;
}// End of function

// Overloading * operator
// Function to perform multiplication operation and return the result object
Fraction operator * (Fraction f)
{
// Creates a temporary object of Fraction class
Fraction t;
// Multiplies the implicit object numerator with parameter object denominator and stores it in temporary object numerator
t.numerator = numerator * f.numerator;
// Multiply implicit object denominator with parameter object denominator and store it in the temporary object denominator
t.denominator = denominator * f.denominator;
// Return the result object
return t;
}// End of function

// Overloading / operator
// Function to return the object after division
Fraction operator / (Fraction f)
{
// Creates a temporary object of Fraction class
Fraction t;
// Checks for divide by zero error
if(f.denominator == 0 || denominator == 0)
{
cout<<" Cannot divide by zero";
exit(0);
}// End of if condition
// Otherwise denominator is not zero
else
{
// Multiply implicit object numerator with parameter object denominator and store it in the temporary object numerator
t.numerator = numerator * f.denominator;
// Multiply parameter object numerator with implicit object denominator and store it in the temporary object denominator
t.denominator = f.numerator * denominator;
// Return the result object
return t;
}// End of else
}// End of function

// Overloading > operator
//Returns true if both numerator and denominator cross multiplication is greater
bool operator > (Fraction f)
{
return ((numerator / denominator) > (f.denominator / f.numerator));
}// End of function

// Overloading < operator
//Returns true if both numerator and denominator cross multiplication is less
bool operator < (Fraction f)
{
return ((numerator / denominator) < (f.denominator / f.numerator));
}// End of function

// Overloading == operator
// Returns true if both numerator and denominator cross multiplication is equal
bool operator ==(Fraction f)
{
return (numerator * f.denominator == denominator * f.numerator);
}// End of function

// Overloading = operator
// Returns the object after assignment
void operator =(const Fraction &f)
{
numerator = f.numerator;
denominator = f.denominator;
}// End of function

// Function to minimize the fraction and returns it
Fraction minimize()
{
// Loops till denominator
for(int c = 2; c < denominator; c++)
{
// Checks if both the numerator and denominator is divisible by the counter value
if((numerator % c == 0) && (denominator % c == 0))
{
// Divide the numerator with counter value and store the quotient
numerator = numerator / c;
// Divide the denominator with counter value and store the quotient
denominator = denominator / c;
}// End of if condition
}// End of for loop
// Returns the current object
return *this;
}// End of function
};//End of class

//Displays the menu for Operator
void selectOperator()
{
cout<<" + for Addition - for Subtraction * for Multiplication / for Division
> - for greater than < for less than == for check equals = for assignment $ - for exit.";
}// End of function

// Main method
int main()
{
// To store operator symbol
string operatorSymbol;
// Creates two Fraction class object
Fraction f1, f2;
// Creates an object to store result
Fraction t;
// Loops till operator symbol is not "$"
do
{
// Displays the menu for operator symbol
selectOperator();

// Accepts the symbol
cout<<" Enter the expression symbol: ";
cin>>operatorSymbol;
// Checks if the operator symbol is "$" stop the program
if(operatorSymbol.compare("$") == 0)
exit(0);
// Checks if the symbol is '='
if(operatorSymbol.compare("=") == 0)
{
cout<<" Example First fraction 2/3 + second fraction 3/2 ";
// Accepts the fraction
cout<<"Enter an First Fraction: ";
cin>>f1;
// Assigns the object using = operator overloading
t = f1;
cout<<" After assignment: ";
//Displays the result
cout<<" First Fraction Number: "<<f1<<" Second Fraction Number: "<<t;
}// End of if condition
// Otherwise binary operator
else
{
cout<<" Example First fraction 2/3 + second fraction 3/2 ";

// Accepts first and second fraction
cout<<"Enter an First Fraction: ";
cin>>f1;

cout<<" Enter an Second Fraction: ";
cin>>f2;

// Checks the symbol and use the appropriate operator with object
if(operatorSymbol.compare("+") == 0)
{
t = f1 + f2;
//Displays the result
cout<<f1<<" "<<operatorSymbol<<" "<<f2<<" = "<<t;
cout<<" Minimized Fraction -> "<<t.minimize();
}
else if(operatorSymbol.compare("-") == 0)
{
t = f1 - f2;
//Displays the result
cout<<f1<<" "<<operatorSymbol<<" "<<f2<<" = "<<t;
cout<<" Minimized Fraction -> "<<t.minimize();
}
else if(operatorSymbol.compare("*") == 0)
{
t = f1 * f2;
//Displays the result
cout<<f1<<" "<<operatorSymbol<<" "<<f2<<" = "<<t;
cout<<" Minimized Fraction -> "<<t.minimize();
}
else if(operatorSymbol.compare("/") == 0)
{
t = f1 / f2;
//Displays the result
cout<<f1<<" "<<operatorSymbol<<" "<<f2<<" = "<<t;
cout<<" Minimized Fraction -> "<<t.minimize();
}
else if(operatorSymbol.compare(">") == 0)
{
if(f1 > f2)
//Displays the result
cout<<f1<<" "<<operatorSymbol<<" "<<f2<<" true";
else
cout<<f1<<" "<<operatorSymbol<<" "<<f2<<" false";
}
else if(operatorSymbol.compare("<") == 0)
{
if(f1 < f2)
//Displays the result
cout<<f1<<" "<<operatorSymbol<<" "<<f2<<" true";
else
cout<<f1<<" "<<operatorSymbol<<" "<<f2<<" false"; }
else if(operatorSymbol.compare("==") == 0)
{
if(f1 == f2)
//Displays the result
cout<<f1<<" "<<operatorSymbol<<" "<<f2<<" true";
else
cout<<f1<<" "<<operatorSymbol<<" "<<f2<<" false";
}
else
cout<<" Invalid symbol";
}
}while(1);
}//End of main

Sample Output:

+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
= for assignment
$ - for exit.
Enter the expression symbol: +

Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 4/2

Enter an Second Fraction: 2/3
4 / 2 + 2 / 3 = 16 / 6
Minimized Fraction -> 8 / 3
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
= for assignment
$ - for exit.
Enter the expression symbol: -

Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: -12/2

Enter an Second Fraction: 1/2
-12 / 2 - 1 / 2 = -26 / 4
Minimized Fraction -> -13 / 2
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
= for assignment
$ - for exit.
Enter the expression symbol: *

Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 2/3

Enter an Second Fraction: 4/3
2 / 3 * 4 / 3 = 8 / 9
Minimized Fraction -> 8 / 9
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
= for assignment
$ - for exit.
Enter the expression symbol: /

Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 44/2

Enter an Second Fraction: 4/3
44 / 2 / 4 / 3 = 132 / 8
Minimized Fraction -> 66 / 4
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
= for assignment
$ - for exit.
Enter the expression symbol: >

Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 12/2

Enter an Second Fraction: 1/2
12 / 2 > 1 / 2 true
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
= for assignment
$ - for exit.
Enter the expression symbol: <

Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 2/3

Enter an Second Fraction: 8/9
2 / 3 < 8 / 9 true
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
= for assignment
$ - for exit.
Enter the expression symbol: ==

Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 1/2

Enter an Second Fraction: 3/2
1 / 2 == 3 / 2 false
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
= for assignment
$ - for exit.
Enter the expression symbol: =

Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 12/9

After assignment:

First Fraction Number: 12 / 9
Second Fraction Number: 12 / 9
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
= for assignment
$ - for exit.
Enter the expression symbol: $

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote