In an internet class I am following, we had to make a "fraction" class, that is, a class to handle rational numbers. To do this, I created the three following constructors:
Rational::Rational(): m_numerateur(0), m_denominateur(1)
{
}
Rational::Rational(int numerateur): m_numerateur(numerateur), m_denominateur(1)
{
}
Rational::Rational(int numerateur, int denominateur): m_numerateur(numerateur), m_denominateur(denominateur)
{
if(denominateur == 0) // We check that the denominator is different than 0.
{
std::cout << "Erreur: le dénominateur doit être un entier différent de 0!" << std::endl;
}
else
{
if(denominateur < 0)
{
m_denominateur = -m_denominateur;
m_numerateur = -m_numerateur;
}
else
{
// The fraction is already either of the form a(x,y), x,y>0 or of the form a(x,y), x<0, y>0.
}
}
}
After the homework was completed, the teacher wrote that those three constructors could be simplified into one constructor.