So simple problem. I need to have a division by zero validation in my constructor in this class. It is painfully simple, but I am just starting to learn classes and I just cannot figure out what I have to do.
Would it be easier to just put the validation in my RationalNumber constructor, or a constructor for the validation separate??
Any help would be greatly appreciated.
#ifndef RATIONALNUMBER_H
#define RATIONALNUMBER_H
// The FeetInches class holds distances or measurements
// expressed in feet and inches.
class RationalNumber
{
private:
int numerator; // To hold a number of feet
int denominator; // To hold a number of inches
void reduce(); // Defined in FeetInches.cpp
public:
// Display function
void display();
// Constructor
RationalNumber(int n = 0, int d = 1)
{
if (d == 0)
{
denominator = 1;
}
numerator = n;
denominator = d;
reduce();
}
// Mutator functions
void setNumerator(int n)
{ numerator = n; }
void setDenominator(int d)
{ denominator = d;
reduce(); }
// Accessor functions
int getNumerator() const
{ return numerator; }
int getDenominator() const
{ return denominator;}
};
#endif