1 2 3 4 5 6 7 8 9 10 11 12

Then the two commands:
f1.create(1, 2);
f1.print();
sets f1.numerator and f2.denominator, and prints 0.5

As with all functions in C++, it is possible and useful to initialize the parameters to the member for example:

class Fraction {
public:
void create (int num = 0, int denom = 1);
void print();
private:
int numerator;
int denominator;
}
;
then we could write
Fraction F1;
F1.create (5,3);// set numerator == 5 and denominator == 3 F1.create (5);// sets numerator == 5 and denominator == 1 F1.create ();// set numerator == 0 and denominator == 1 F1.create (,3);// how about this one?

Here we have been using the initializerfunction "create" to set the values of the Fraction F1. Of course we could have built constructors to do the same thing at object creation time. For example:
Fraction F1 (5,3);
Fraction F2 (5);
Fraction F3 ();

Fraction :: Fraction (int num = 0, int denom = 1) {
Numerator = num;
Denominator = denom;
}

March 11, 1998

Page 3

C201/TAM