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

Viewed 1423

I Have a Fraction Class.

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

  1. Multiply two Fraction objects. e.g F1*F2
  2. Multiply a Fraction object by an integer. For ex. F1*3
  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.

3 Answers

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.

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.

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
Related