If my goal is to consider f1 and f2 to hold the numerator and denominator of two fractions and multiply f1(9,8) by f2(2,3). i don't understand how to write the MultipliedBy function if I only have two member variables in the class it's self. I feel like i'm just missing something. Does MulitplyedBy get passes f1 and f2? does it need 4 arguments ?

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.MultipliedBy(f2);
    result.print();
    cout << endl;

}

sorry here is my class. I think i'm just mis interpreting what is happeining in the actual code.

class fraction {
    public:
        fraction();
        fraction(int inNumerator,int inDenominator);
        void print()const;
        int fraction::simplify(int Numer,int Denom);
        int fraction::MultipliedBy();
    private:
        int numerator;
        int denominator;
};

Something like this might work for you:

#include <string>
#include <iostream>
#include <sstream>
using namespace std;
class fraction
{

public:
    int numerator;
    int denominator;
    //default constructor
    fraction()
    {
        numerator = 0;
        denominator = 0;
    }
    //constructor with values passed to it
    fraction(int n, int d)
    {
        numerator = n;
        denominator = d;
    }
    //a formatted string to be useful in other places
    string ToString()
    {
        stringstream ss;
        ss << numerator << "/" << denominator;
        return ss.str();
    }
    //A simple multiplier
    fraction MultipliedBy(fraction f)
    {
        fraction result(numerator * f.numerator,denominator * f.denominator);
        return result;
    }

};
//A simple pause screen routine to try and keep the code standard
void Pause()
{
    cin.ignore();
    cin.get();
}
int main()
{
    fraction f1(2,4);
    fraction f2;
    f2.numerator = 1;
    f2.denominator = 3;
    cout << f1.MultipliedBy(f2).ToString();
    Pause();
    return 0;
}

Edited 3 Years Ago by tinstaafl

i mainly just want to understand what is happeining in

result = f1.MultipliedBy(f2);

when f1(9,8) and f2(2,3) have values like that.
I just don't undertstand what is supposed to be passed to the function MultipliedBy.

The code I gave you shows you the function and how to use it. The class I did is very similar to yours but, to pass the fraction to the multipliedby function, I think you need numerator and denominator to be public, because the numerator and the denominator each have to be multiplied separately, so I re did the class a little. Also with the format of the class like this, it should be a little easier to see what the individual functions do.

just in case that is still too complicated for you, here's a break down of the main part of the multipliedby function:

fraction result(numerator * f.numerator,denominator * f.denominator);
the return type is a fraction so there is a temporary variable of type fraction to hold the result. To initialize it the values are included rather than being added separately. numerator is part of f1, and f.numerator is part of f2, the same with the denominators. each set is multiplied together to get the values needed to initialize the result variable.
the same result be achieved by expanding the code a little

fraction MultipliedBy(fraction f)
{
    int n = numerator * f.numerator;
    int d = denominator * f.denominator;
    fraction result(n,d);
    return result;
}

Edited 3 Years Ago by tinstaafl

just to be clear I don't want an example of how to do this problem, I just want to develope a clearer understanding of what is going on

result = f1.MultipliedBy(f2);

when the objects f1 and f2 each have values assigned to them.

do you know how to multiply fractions? the code does exactly the same thing. And since your code only shows the declaration of the function, anybody can only guess the specifics of how your function works. That's why I had to show it to you a complete class so you can see one way of how a function like this can work. And even gave you a fairly detailed explanantion of how it works.

Edited 3 Years Ago by tinstaafl

ok I see that, for some reason i thought my prof. wanted us to make the memeber variables private. So fraction f in this case is what exactly?

sorry about all the questions im new to this and just trying to get a solid understanding of what is happening.

So fraction f in this case is what exactly?

That defines what can be passed to the function. f2 is a fraction and you want to pass it to the function, therefore the function must be declared to accept a variable of type fraction. To be able to use this variable it's given a different name, even though it has the same values as in f2.

Edited 3 Years Ago by tinstaafl

Thanks, i couldn't see your first post completely on my phone, which probubly lead to my confusion. I appreciate the help though.

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