For this program I am writing a 'Function' class to be used in a program adds and multiplies Fraction objects. It will also create a copy of one using a copy constructor and print the objects as decimals and compare them.
My program executes fine, but when I cast my Fraction objects to the overloaded double operator it always returns 1, and I cannot figure out why - I've even gone through using breakpoints to see the values as the program runs.
Here is the Fraction class declaration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class Fraction
{
private:
int numerator; // integer representing the numerator of the fraction
int denominator; // integer representing the denominator of the fraction
public:
Fraction(int = 1, int = 1); // default constructor
Fraction(const Fraction&); // copy constructor
void setNumerator(int); // prototype for set method for the numerator
int getNumerator(); // "" for the get method for the numerator
void setDenominator(int); // "" for the set method for the denominator
int getDenominator(); // "" for the get method for the denominator
void printFraction(); // method to print the fraction
Fraction operator+(Fraction &); // prototype for overloaded addition operator
Fraction operator*(Fraction &); // " for overloaded multiplication operator
booloperator==(Fraction &); // " for overloaded equals to operator
voidoperator=(Fraction &); // " for overloaded assignment operator
operatordouble(); // conversion operator to double
};
here is the overloaded double operator defined:
1 2 3 4 5 6
Fraction::operatordouble()
{ // conversion
double value; // double representing the fraction's decimal value
value = numerator/denominator; // value is equal to the num divided by the denom
return(value); // return the value
}
Here is the printing:
1 2 3 4 5 6 7 8 9 10 11 12
cout << "\n\n******** Using copy ctor ********";
Fraction fractionCcopy = fractionC;
double a = 0;
a = fractionC;
double b = 0;
b = fractionCcopy;
cout << "\n\nOriginal object using operator double is " << a;
cout << "\nNew object using operator double is " << b;
return fractionCcopy;
a and b always become and output as 1 when I assign them to the fractions.