0

I'm trying to convert a object to a float, but it doesn't seems to work the way I want.

int main()
{
  Fraction A(20,6);
  float E;  
  E = A;
  cout << E << endl;
}

I already did a type convert operator inside the class

operator float () const     
    {           
        return static_cast<float>(num/den);  //Being num and den private members, 20 and 6 in this case. 
    }

When I run the program, the result is 3 (with no decimal places). Any help?

David Heffernan
  • 587,191
  • 41
  • 1,025
  • 1,442
Lucas Azevedo
  • 65
  • 1
  • 5

1 Answers1

0

I can't see your code, but based on the behavior you're observing, is it possible that num and den are defined as int data types? If so, num/den is being treated as integer division, and therefore resulting in the result being truncated.

Xirema
  • 19,266
  • 4
  • 30
  • 64