/** @file list4402.cpp */
/** Listing 44-2. Using a Function and Conditional Statements Instead of Conditional Expressions */
/// Construct a rational object from a numerator and a denominator.
/// If the denominator is zero, throw zero_denominator. If the denominator
/// is negative, normalize the value by negating the numerator and denominator.
/// @post denominator_ > 0
/// @throws zero_denominator
rational::rational(int num, int den)
: numerator_(den < 0 ? -num : num), denominator_(init_denominator(den))
{
  reduce();
}

/// Return an initial value for the denominator_ member. This function is used
/// only in a constructor’s initializer list.
int rational::init_denominator(int den)
{
  if (den == 0)
    throw zero_denominator("0 denominator");
  else if (den < 0)
    return -den;
  else
    return den;
}
