Hey guys! I'm on a new topic and I'm finding it to be a bit frustrating. And basically the way I am learning classes and ADT's is by playing with them. So, I started and I think I understand the basic concept.
I am going to post the code below that I am playing with. The part I am looking at now is how to write the Multiply function. I think once I get the concept of how to manipulate things in the implementation file, I'll be okay. The Client Code was given and is not supposed to be changed. The specification and implementation files are the ones I'm tinkering with. I was given a sort of blueprint of what to do. I've already written my default constructors but now I'm stuck at how to manipulate things. If anyone can "break it down":\$ for me, I'd really appreciate it.

Specification file:

``````// Header file fraction.h declares class Fraction.
class Fraction
// Class Fraction represents the numerator and
// denominator of a fraction.
{
public:

// Constructors
Fraction();
// Post: Numerator and denominator have been set to zero
Fraction(int initNumerator, int initDenominator);
// Post: Numerator has been set to initNumerator;
//       denominator has been set to initDenominator.
// Post: self + frac1 is returned.
Fraction Subtract(Fraction frac1) const;
// Post: self - frac1 is returned.
Fraction Multiply(Fraction frac1) const;
// Post: self * frac1 is returned.
Fraction Divided(Fraction  frac1) const;
// Post: self / frac1 is returned.
int print() const;
// Post: print Numerator and Denominator of frac1.
private:
int numerator;
int denominator;
};``````

Implementation file:

``````//*******************************************************

// Implementation file fraction.cpp implements the member
// functions of class Fraction.

#include "fraction.h"
#include<iostream>
using namespace std;

Fraction::Fraction()
{
// FILL IN Code for default constructor.
numerator=0;
denominator=1;
}

Fraction::Fraction(int initNumerator, int initDenominator)
{
// FILL IN Code for default constructor.
numerator=initNumerator;
denominator=initDenominator;
}

// Pre:  frac1 and self have been initialized.
// Post: frac1 + self is returned in reduced form.

{
// FILL IN Code.
}

Fraction Fraction::Subtract(Fraction  frac1) const
// Pre:  frac1 and self have been initialized.
// Post: self - frac1 is returned in reduced form.are
{
// FILL IN Code.
}

[B]Fraction Fraction::Multiply(Fraction  frac1) const
// Pre:  frac1 and self have been initialized.
// Post: self - frac1 is returned in reduced form.are

{
// FILL IN Code.

}    [/B]

Fraction Fraction::Divided(Fraction  frac1) const
// Pre:  frac1 and self have been initialized.
// Post: self - frac1 is returned in reduced form.are

{
// FILL IN Code.
}

int Fraction::print() const
{
// FILL IN Code.
cout<<numerator<<"/"<<denominator;
}``````

How in the world am I supposed to "get" the first fraction to multiply it with the second one???

Client Code:

``````#include <iostream>
#include "fraction.h"
using namespace std;

int main()
{
Fraction f1(9,8);
Fraction f2(2,3);
Fraction result;

cout << "The result starts off at ";
result.print();
cout << endl;

cout << "The product of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.Multiply(f2);
result.print();
cout << endl;

cout << "The quotient of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.Divided(f2);
result.print();
cout << endl;

cout << "The sum of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result.print();
cout << endl;

cout << "The difference of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.Subtract(f2);
result.print();
cout << endl;

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

system("pause");
}``````

Any pointers or tips? I know I am missing something in my understanding of classes...

3
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by NitaB

Okay, I changed the bolded part to this:

``````int Fraction::Multiply(Fraction frac1) const
// Pre:  frac1 and self have been initialized.
// Post: self - frac1 is returned in reduced form.are

{
// FILL IN Code.
Multiply.frac1=Fraction.initNumerator*Fraction.numerator;
}``````

but all i get are errors...insufficient contextual information to determine type

All I want to do is multiply one fraction by the second.
I tried other things that made more sense but gave even more errors so I assume they were wrong. Help please

You need to provide an implementation for all the arithmetic functions , something like this

``````Fraction Fraction::Multiply(Fraction  frac1) const
// Pre:  frac1 and self have been initialized.
// Post: self - frac1 is returned in reduced form.are

{
// FILL IN Code.

//multiply numerators from 2 fractions
int num = this->numerator * frac1.numerator;
//multiply denominators from 2 fractions
int den = this->denominator * frac1.denominator;

//create a new Fraction made up of the new numerator & denominator
Fraction f (num,den);
//return the new Fraction
return f;
}``````

I do see that 'numerator' and 'denominator' are private members and hence you would have to provide public accessors to those to be able to do this.

Edited by Agni: java hangover..this.numerator !!

How in the world am I supposed to "get" the first fraction to multiply it with the second one???

You have it. Read up in your text about the *this pointer. You've formed this object and now it has a reference to itself:
this->numerator and this->denominator are the values for the current object (since the object and any other objects of the class can "see" its private members). So in your frac1s that you are passing in, the numerator and denominator will be visible (but invisible to other classes since they are private.

So for the addition operator for example, declare a new fraction local to that block, Fraction fracresult; or something:
fracresult.numerator = (this->numerator x denominator of frac1) + (numerator of frac1xthis->denominator); (standard formula for adding two fractions - now the denominator will be the product of the denominators and set equal to fracresult.numerator)
The reason you need the -> is that the object has a reference to *this, so you need to dereference and . to access the method and arrow is a shortcut for that.

EDIT: Well there's an outpouring for you. What he said definitely works too (with using the constructor) -- though you shouldn't need an accessor (getter) between two objects of the same class, but it couldn't hurt to add one (though I think you said you are limited by the assigned header file) for other classes to interact with.

Edited by jonsca: n/a

my bad, thanks for correcting :) ...

Thanks Agni! Thanks jonsca!
Actually, I kinda figured it out doing something similar to what jonsca said(you were right about me being limited by the header file).
Here is how I did it:

``````Fraction Fraction::Multiply(Fraction frac1) const
// Pre:  frac1 and self have been initialized.
// Post: self - frac1 is returned in reduced form.are

{
Fraction Temp;
Temp.numerator=numerator*frac1.numerator;
Temp.denominator=denominator*frac1.denominator;

return Temp;
}``````

The others kinda followed suite.

Now I am trying to come up with an algorithm to simplify the fractions. I have this:

``````Fraction Fraction::Multiply(Fraction frac1) const
// Pre:  frac1 and self have been initialized.
// Post: self - frac1 is returned in reduced form.are

{
// FILL IN Code.
Fraction Temp;
Temp.numerator=numerator*frac1.numerator;
Temp.denominator=denominator*frac1.denominator;

int x=Temp.numerator;
while(Temp.denominator%x!=0&&Temp.numerator%x!=0)
{
x--;
}
Temp.numerator=Temp.numerator/x;
Temp.denominator=Temp.denominator/x;

return Temp;
}``````

...buutttt, it's not exactly working correctly and I don't know why.

Ha! Nevermind, I figured it out. I love when that happens. lol!
I made an if statement inside a for loop and kinda sorta used what i had but moved the Temp.numerator=Temp.numerator/x; blah blah inside the if statement. Voila!
Thanks again guys!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.