The Rational Class sample driver (main)

This sample driver shows how the Rational class is used.

The sample driver file   --   the whole thing: ratdriver.cpp

Notice first that the rat.h file must be included here as well as in the .cpp file. That is because the identifiers are used here and the compiler must know their definition.

The first thing in main are instantiations of four objects:
   Rational x(-2,6), y(-14,-16), z, w(4);
The object   x   will hold the fraction -1/3 in memory after the constructor calls reduce to reduce it to lowest terms. The object   y   will hold 7/8. The object   z   will hold 0/1, using both default values in the constructor. The object   w   will be 4/1, using only the second default value of one for the denominator.

The code using the objects is similar to Java. For example, this code prints the object   x   and   y  , adds   y   to   x   putting the sum in   z  , and prints   z  . It then prints   z   as a float.
   x.printRational();  cout << " + ";  y.printRational();
   z = x.add(y);
   cout << " = ";  z.printRational();  cout << endl;
   z.printRational();
   cout << " = ";  z.printRationalAsFloat();  cout << endl << endl;
The dot operator is used in an identical way in C++.