Overloaded Arithmetic Operations on fractions
 
  
Hey, I'm 100% new to Overloading Operators, I don't fully get the syntax.
The goal here is trying to divide fractions. Fractions are stored within a class.
My main question is, how do I need to fix my syntax to make it work? I have a lot of red lines...
Resource File:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 
 | Mixed Mixed::operator/ (const Mixed mixed1, const Mixed mixed2)
{
	Mixed fraction;
	mixed1.ToFraction();
	mixed2.ToFraction();
	fraction.numerator = (mixed1.numerator * mixed2.denominator);
	fraction.denominator = (mixed2.numerator * mixed1.denominator);
	fraction.Simplify();
	return fraction;
}
void Mixed::ToFraction()
{
	if((numerator==0)&&(denominator==0))
		cout << integer << '/' << '1' << endl;
	else
	{
		if(integer<0)
			numerator = (integer*denominator) - numerator;
		else
			numerator = (integer*denominator) + numerator;
		integer = 0;
		cout << numerator << '/' << denominator << endl;
	}
}
void Mixed::Simplify()
{
	int greatestCommonDenominator = gcd(numerator,denominator);
	cout << "This is the LCD: " << greatestCommonDenominator << endl;
	if(greatestCommonDenominator==0)
		cout << integer << endl;
	else if((denominator/greatestCommonDenominator != 0)&&(numerator/greatestCommonDenominator != 0))
	{	
		numerator=numerator/greatestCommonDenominator;
		denominator=denominator/greatestCommonDenominator;
		if(integer != 0)
			cout << integer << ' ';
		cout << numerator << "/" << denominator << endl << endl;
	}
}
int Mixed::gcd(int a, int b)
{
	if(b == 0)
		return a;
	else
		return gcd(b, a%b);
}
 |  | 
Header File:
(It's within Public, should it be a friend?)
| 12
 
 |  
	Mixed operator/ (const Mixed mixed1, const Mixed mixed2);
 |  | 
Testing Code:
| 12
 
 | 	  Mixed  m2(-1,2,4),m8(0,12,24);
	  cout << "(m2 / m8) = " << (m2/m8) << '\n';
 |  | 
Any and all advice or resources would be appreciated! 
Last edited on 
 
 
Topic archived. No new replies allowed.