Constant Objects

I have a couple fraction objects that are declared as constant and they go through a multiplied function and get multiplied correctly. Then the answer goes through a simplify function which simplifies the constant objects correctly. The problem I am having is figuring out how to display the constant objects in their reduced form. For example, 12/8 * 202/303 = 1/1, which is what I am displaying but I need to display it as 3/2 * 2/3 = 1. I am not sure whether There is an issue with the way I am using const or I may need to figure out the correct syntax for another function call to display();

1
2
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
54
55
56
57
58
59
60
61
62

  // In main

	const Fraction f3(12, 8);
	const Fraction f4(202, 303);
	result = f3.multipliedBy(f4);
	cout << "The product of ";
	f3.print();
	cout << " and ";
	f4.print();
	cout << " is ";
	result.print();
	cout << endl;
}


Fraction Fraction::multipliedBy(const Fraction &otherFraction)const
{
	Fraction answer;
	answer.numerator = (numerator * otherFraction.numerator);
	answer.denominator = (denominator * otherFraction.denominator);
	answer.simplify();

	return answer;
}



void Fraction::simplify()
{
	int reducedNumerator = numerator;
	int reducedDenominator = denominator;
	const int prime_SIZE = 26;
	int prime[prime_SIZE] = { 2,3,5,7,11,13,17,19,23,29,31,37,41,43,
					47,53,59,61,67,71,73,79,83,89,97,101 };

	
	for (int i = 0; i < prime_SIZE; i++)
	{
		while (reducedNumerator % prime[i] == 0 && reducedDenominator % prime[i] == 0) {
			reducedNumerator = reducedNumerator / prime[i];
			reducedDenominator = reducedDenominator / prime[i];
			
		}
	}
	numerator = numerator / reducedNumerator;
	denominator = denominator / reducedDenominator;
	if (numerator == denominator) {
		numerator = reducedNumerator;
		denominator = reducedDenominator;

	}
}




void Fraction::print()const
{   // May need function call here. Not sure correct syntax.
	//simplify();
	cout << numerator << "/" << denominator;
}
I see you are calling simplify in multipliedBy but wouldn't it be easier to call simplified in the constructor so that the fraction always is "simplified"? Otherwise you could create local copy and simplify that.

1
2
3
4
Fraction simplifiedFraction = *this;
simplifiedFraction.simplify();

cout << simplifiedFraction.numerator << "/" << simplifiedFraction.denominator;
Last edited on
I like the idea inside the constructor. I tried that and I seem to be getting garbage values.

1
2
3
4
5
6
7
8
9
Fraction::Fraction(int inNumerator, int inDenominator)
{
	Fraction answer;

	answer.numerator = inNumerator;
	answer.denominator = inDenominator;

	answer.simplify();
}


Output:
The product of -858993460/-858993460 and -858993460/-858993460 is 687194768/687194768
In the constructor you want to initialize the variables of the object that is being constructed, not some local variable.
That makes sense. That was actually how I was trying to do that in the print function and I was getting syntax errors so I thought I had to create an object to connect the data. This is my first class project so I am getting used to how data is communicated and organized. I got it working now. I appreciate the guidance.
Topic archived. No new replies allowed.