I am working on a c++ assignment for a class and the perimeters were to create a fraction class described below and then test and demonstrate the methods. What i'm confused about is what exactly some of this means in relation to the fraction class - for instance what String toString supposed to do? If someone could clear that up for me, id appreciate it.
thanks
class Fraction
{
int num, den;
public:
Fraction(); // Default construct a Fraction object.
// ie: plug in some default numbers for num,den
Fraction(int,int); // Construct a Fraction based on the given n,d
Fraction(int); // This is a little confusing. Not sure exactly, but I would
// guess the given value is the numerator, and you'd just
// have 1 for the denominator.
Fraction(const Fraction&); // Copy constructor -- create this object from another given object
~Fraction(); // Destructor. Do any cleanup (you won't have any)
staticbool isValidDenom(int); // Return true if the given number would be a valid denominator.
static Fraction create(int,int); // Similar to the constructor, only you create an object and return it.
double toDecimal() const; // Convert the fraction to a decimal (floating point) value and return it
string toString() const; // Convert the fraction to string form and return it. IE
// you'd return a string like "1/2" or something
};
It looks like the function double toDecimal() const; would convert the 'fraction' object into a decimal, and so therefore i believe the toString function would convert the object into its wordy representation.
Example (in conceptual-code):
your object would be something like:
num=3, den=5
and so the toDecimal function should print out " 0.6 "
and the toString function likewise should print "three fifths".
At least that's what makes sense to me.
"String toString" just means "I'm making a new function, calling it 'toString' and it will return a string-type something", if that's your confusion.