class Fraction
{
public:
Fraction();
Fraction(int numerator, int denominator);
void divide(int& num, int& denom) const;
void multiply(int& num, int& denom) const;
void add(int& num, int& denom) const;
void substract(int& num, int& denom) const;
int getNum() const;
int getDenom() const;

bool operator > (const Fraction& otherFraction) const;
bool operator < (const Fraction& otherFraction) const;
bool operator == (const Fraction& otherFraction) const;

private: 
int num; 
int denom;
int x1;
int y1;
int x2;
int y2;
};
#include "fraction.h"
#include <iostream>

Fraction::Fraction(int,int)
{}

bool Fraction::operator > (const Fraction& otherFraction) const
{
if (num > otherFraction.num)
return true;
else if (num < otherFraction.num)
return false;
else if (denom > otherFraction.denom)
return true;
else if (denom < otherFraction.denom)
return false;
else
return true;
}

bool Fraction::operator < (const Fraction& otherFraction) const
{
if (num < otherFraction.num)
return true;
else if (num > otherFraction.num)
return false;
else if (denom < otherFraction.denom)
return true;
else if (denom > otherFraction.denom)
return false;
else 
return true;
}

bool Fraction::operator == (const Fraction& otherFraction) const
{
return (num==otherFraction.num) && (denom==otherFraction.denom);
}

void Fraction::divide(int& num, int& denom) const
{
num = (x1 * y2);
denom = (y1 * x2);
}

void Fraction::multiply(int& num, int& denom) const
{
num = (x1 * x2);
denom = (y1 * y2);
}

void Fraction::add(int& num, int& denom) const
{
num = ((x1 * y2) + (y1 * x2));
denom = (y1 * y2);
}

void Fraction::substract(int &num, int &denom) const
{
num = ((x1 * y2) - (y1 * x2));
denom = (y1 * y2);
}

int Fraction::getNum() const
{
return num;
}

int Fraction::getDenom() const
{
return denom;
}
#include <iostream>
#include "fraction.h"
using namespace std;

int main()
{
Fraction fraction1(3,4);
Fraction fraction2(5,6);


if (fraction1 > fraction2)
cout << "Fraction 1 is greater than Fraction 2" << endl;
if (fraction2 < fraction1)
cout << "Fraction 2 is less than Fraction 1" << endl;
if (fraction1 == fraction2)
cout << "Fraction 1 is equal to Fraction 2" << endl;

return 0;
}

This is the direction: Create a new advanced data type called Fraction that saves numerator and denominator. There are functions to Add, Subtract, Divide, Multiply the fractions. There are two other functions to return the numerator and denominator. Finally, an overloaded operator should be able to compare whether two fraction objects are greater than, less than, or equal to one another.

1. Am I on the right track?
2. How do i get the fractions to add, subtract, divide, and multiply?

1. There are functions to Add, Subtract, Divide, Multiply the fractions.
2. There are two other functions to return the numerator and denominator.
3. Finally, an overloaded operator should be able to compare whether two fraction objects are greater than, less than, or equal to one another.

Although I think overloading the +, -, /, and * operators would make more sense, your instructor specifically asks for functions to peform the respective tasks:

class fraction
{
     ...
     ...
     ...
     int get_numerator(){return num};
     int get_denominator(){return denom};
     fraction add(fraction);
     fraction subtract(fraction);
     fraction divide(fraction);
     fraction multiply(fraction);
     ...
     ...
     ...
};

Here is one free of charge:

//Assumptions:
//Assuming a proper, non-mixed fractions only (Ex.  1/2 + 2/3 = 7/6)
//Assuming proper input, non-zero division from the user.

fraction add(fraction x);
{    
     enum{top, bottom};

     int frac_1[2] = {num, denom};
         frac_2[2] = {x.num, x.denom};
         temp[2]= {0,0};
         factor = 0,
         divisor = 0;
 
     fraction myfraction;

     //Get a factor that will make everything the same
     factor = frac_1[bottom] * frac_2[bottom];

     //Make everything the same
     for(int i=0; i<2; i++)
     {
          frac_1[i] *= factor;
          frac_2[i] *= factor;
     }

     //Add the fractions & store ye' result
     for(int i=0; i<2; i++)
     {
          temp[i] = frac_1[i] + frac_2[i];
     }

     //Begin reduction to lowest term
     //Identify a common divisor (if any)
     for(int i=2; i<temp[bottom]/2; i++)
     {
         //if the divisor 'i' can go into both equally...
         if(!temp[top]%i && !temp[bottom]%i)
         { 
               //then divide the numerator and denomiantor by the divisor.
               temp[top] /= i;
               temp[bottom] /= i; 
               //set 'i' back to 2 in case of further possible reduction
               i = 2;
          }
     
          myfraction.num = temp[top];
          myfraction.denom = temp[bottom];

          return myfraction;
}

now you can call it in main like this:

//using constructors
fraction frac1(1,2), frac2(2,3), result;
//using and overloaded assignment operator
result = frac1.add(frac2);

You also ask for overloaded <, >, and == functions. In my opinion, the easiest way to compare two fractions is just to make the division and turn them into decimals, make the needed comparison, and return a true/false indication.

Edited 5 Years Ago by Clinton Portis: n/a

This article has been dead for over six months. Start a new discussion instead.