I make a fraction class, but I want to assign its value to another fraction, usually, I have to do this:
fraction a;
a.setNumerator=(1);
a.setDenominator(2);
b=a; //b is already intialized above
This is long so I made a temporary function like this:
fraction construct (int a, int b)
{
fraction r;
if (b!=0)
r.denominator=b;
r.numerator=a;
return r;
}
But I think it's not a good way to do this so how can I construct objects other than this way?
The solution I tried:
Overload operator =
with float type then I can do something like this
a=1/2;
It works well with this case but with a recursive fraction (i.e 1/3), it will just give me a big approximated fraction(16666667/50000000). My float to fraction function:
fraction (float a)
{
int pow=1;
while (a*pow!=round(a*pow))
{
pow *=10;
}
fraction b((int)(a*pow),pow);
b.simplify();
numerator=b.numerator;
denominator=b.denominator;
}