2

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.

secretgenes
  • 1,291
  • 1
  • 19
  • 39

3 Answers3

4

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.

lubgr
  • 37,368
  • 3
  • 66
  • 117
  • what about calling : Fraction operator *(Fraction rhs, int lhs) ? Guess it has to be taken account. – Blood-HaZaRd Nov 12 '18 at 11:12
  • @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. – lubgr Nov 12 '18 at 11:18
  • 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 Nov 12 '18 at 11:21
  • @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. – lubgr Nov 12 '18 at 11:26
3

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.

Gem Taylor
  • 5,381
  • 1
  • 9
  • 27
0

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
secretgenes
  • 1,291
  • 1
  • 19
  • 39