I'm reluctant to give the code because you've mentioned that this is classwork.
I'm happy to give some pointers, though.
Let's look at the addition. Let's also assume that the function required is part of the fraction class.
The brief asks you to give a fraction as an argument and return a fraction. It mentions that we don't want to change any of the fractions give, which is a huge hint towards using the
const
keyword.
So, your prototype will look like this:
|
fraction Add( fraction f const ) const;
|
The first const is promising we're not going to change the fraction argument that's been passed in within the scope of the function. The second const promises that we're not going to make any changes to the members of the class we're working on.
I've noticed you've interpreted the formulae you've been given as straightforward equations with divisions and such. They're not. It's telling you how to calculate the numerator and denominator separately for each operation.
Addition: a/b + c/d = (a*d + b*c) / (b*d)
This means that the numerator of the new fraction is (a*d + b*c).
The denominator will be (b*d).
Where a/b is your member fraction and c/d is the fraction you pass into the function.
The skeleton code for your function would be as follows.
Create a temporary fraction within your function.
Set temporary numerator to a*d + b * c.
Set temporary denominator b * d.
Return temporary fraction.
|
As discussed, this will work a lot nicer when you look at operator overloading. At the moment, you'd have to do something like this to get a new fraction:
1 2 3 4 5
|
fraction A, B, C;
// Let's assume that fractions A and B have been populated
C = A.Add( B );
|
When you overload the operators (which is surprisingly easy to do and a stones throw away from your function code), you can implement something like this:
1 2 3 4 5
|
fraction A, B, C;
// Let's assume that fractions A and B have been populated
C = A + B;
|
I would work on getting the functions in place, as operator overloading might be jumping the gun for you a little and it's not part of the given brief. It's inclusion here is merely for demonstration purposes.
Hope this helps.