## How to multiply integer constant by Fraction Object in C++

I Have a Fraction Class.

I need to do 3 operations on Fraction Object i.e

- Multiply two Fraction objects. e.g F1*F2
- Multiply a Fraction object by an integer. For ex. F1*3
- Multiply an integer by a Fraction object. For ex. 3*F1.

The first two case can be achieved by overriding the Fraction Class * operator.

Fraction operator*(const Fraction&); Fraction operator*(const int&);

but how to multiply an integer by a fraction Object? The third case

Any Suggestions??

**PS: I don't want to treat integer as a Fraction object e.g (3/1) and then doing the multiplication.**

You need to implement the operator overload as a free function, like this:

Fraction operator *(int lhs, Fraction rhs) { rhs *= lhs; return rhs; }

Note that I have implemented the function in terms of `Fraction::operator*=(int)`

(see here why this is considered good practice). If this function is not present, you might want to pass the second parameter as `const Fraction& rhs`

and provide a different implementation.

Besides, note that an idiomatic way to handle this scenario is to allow an implicit construction of `Fraction`

instances by a single `int`

argument, so your constraint seems a bit awkward to me. Further note that users of your `Fraction`

class might expect all arithmetic operations to be possible (why should there be `operator*`

, but not `operator/`

?). To reduce the amount of manual code to be written in such cases, the boost operator library can be of great help.

**Why does multiplying by a fraction make a number smaller?,** What happens when you multiply a number by a fraction less than 1? C++ Program to Multiply two Numbers In this program, user is asked to enter two numbers (floating point numbers). Then, the product of those two numbers is stored in a variable and displayed on the screen.

Can I make a case for the inplace friend function?

In c++11 you can declare and write your friend function inside the class, which can make it much neater:

class MyNumber { private: Clever c; Clever Multiply (Clever, i) { ... } public: MyNumber operator * (int i)const { return Multiply(c,i) } MyNumber operator * (const MyNumber &i)const { ... } const MyNumber& operator *= (int i) { return c= Multiply(c, i); } // introducing the inline friend (presuming multiply is commutative/symmetric here) friend MyNumber operator (int i, const MyNumber& j) { return j.Multiply(c,i); } };

Note that this friend function is still actually a global function, and has access to the class internals, but its implementation is now tidily inside the class definition.

The neatness of this style is such that I am tempted to use it even when I don't actually need the dirty friend access.

With these math overloading objects, also consider the RValue substitution overloads. An rvalue implementation of binary multiply implemented as mult-assign can show some efficiencies, though perhaps not soo much with only a 2-value fraction class.

**Reasoning about Multiplication,** When you multiply a given fraction by a factor less than 1 The product will? Again, constants are treated just like regular variables except that their values cannot be modified after their definition. Integer Literals. An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.

There are three different ways to overload operators: the member function way, the friend function way, and the normal function way.

In this specific case we need overload the operators in friend function way.

friend Fraction operator*(const Fraction &a, const Fraction &b); //F1*F2 friend Fraction operator*(const Fraction &a, int b); //F1*3 friend Fraction operator*(int a, const Fraction &b); //3*F1

**4. Type Conversions - C in a Nutshell [Book],** dVar *= 3; // Multiply dVar by an integer constant. if ( dVar < 10L ) // Compare dVar with type, or convert a floating-point fraction from type double to the type int , the new type Thus, some long values cannot be stored exactly in a float object. Today, I thought I should try to implement a class in C++ that deals with fractions. This is actually quite easy as I'll show you. First some math Names When you have a fraction \(\frac{a}{b}\) then \(a\) is called numerator and \(b\) is called denominator.

**Multiplication as scaling with fractions (video),** So that is the positive integer that you will multiply by other positive integers to C is also Duration: 5:06
Posted: May 5, 2014 Multiply two Fraction objects. e.g F1*F2; Multiply a Fraction object by an integer. For ex. F1*3; Multiply an integer by a Fraction object. For ex. 3*F1. The first two case can be achieved by overriding the Fraction Class * operator. Fraction operator*(const Fraction&); Fraction operator*(const int&); but how to multiply an integer by a

**1.13. Object-Oriented Programming in C++: Defining Classes ,** We need to be able to add, subtract, multiply, and divide fractions. The notation int num outside the constructor defines the fraction object to have an internal data friend ostream& operator<<(ostream& stream, const Fraction& fraction);. 17. };. To multiply or divide numbers using C# is really similar to multiplying and dividing numbers using other programming languages. In C#, the multiplication symbol used is the asterisk (*), so if you want to multiply a number by another number, you simply need to place the asterisk between them: a = 6; b = 2; Console.WriteLine(a * b);

**1.12. Object-Oriented Programming in C++: Defining Classes ,** We need to be able to add, subtract, multiply, and divide fractions. The notation int num outside the constructor defines the fraction object to have an internal data object friend ostream &operator << (ostream &stream, const Fraction &frac);. Another way to think about this is to rewrite the whole number as a fraction with the denominator of 1 . 5 ⋅ 3 16 = 5 1 ⋅ 3 16 Then multiply numerators and denominators , as per the usual rules for multiplying fractions .

##### Comments

- you may use a templated function and do specialization. Or try function overloading.
- what about calling : Fraction operator *(Fraction rhs, int lhs) ? Guess it has to be taken account.
- @Blood-HaZaRd Right, but the OP already had this signature in his post, so I guess this is already implemented and the question targets an
`int`

left hand side and`Fraction`

right hand side explicitly. - From the given signature in the autor question, it looks like a memeber function, but your's is not (based on the signature). so I guess that it has to be friend and second, there will be a mess one memeber other friend and may be he has to overload too.
- @Blood-HaZaRd Whenever functionality for a class can be implemented as a free function, it should be a free function. I wouldn't consider this a mess, and additionally, the overload the OP is looking for can't be implemented as a member. And sure, if possible, the
`operator*`

overload set should have the minimum number of functions, but note the constraint that`int`

operands shall not be treated as`Fraction`

instances.