operator [] method

int operator [](
  1. int index
)

Access the whole part, the numerator or the denominator of the fraction via index. In particular:

  • 0 refers to the whole part;
  • 1 refers to the numerator;
  • 2 refers to the denominator.

Any other value which is not 0, 1 or 2 will throw an exception of MixedFractionException type.

Implementation

int operator [](int index) {
  switch (index) {
    case 0:
      return whole;
    case 1:
      return numerator;
    case 2:
      return denominator;
    default:
      throw MixedFractionException(
        'The index ($index) is not valid: it must either be 0, 1 or 2.',
      );
  }
}