Polymorphism  «Prev 

Overloading a function


Objective: Add a new print() function to the rational class that takes an integer argument representing the number of decimal places of precision to print.
The new function will have the following prototype:
void rational::print (int);

Function Exercise Hint

The default value for floating-point precision is 6. For example, the number 1234.5678 prints as 1234.57. To change the way this value prints, you could use the ios_base member function:

class ios_base{
public:
....
streamsize precision (streamsize n); 
};

A call of precision() affects all floating-point I/O operations until the next call of precision().
For example:

cout.precision(8);
cout << 8765.4321 << ' ' << 9876.54321 << ' '
  << 98765  << endl;
cout.precision(4);
cout << 8765.4321 << ' ' << 9876.54321 << ' '
  << 98765  << endl;

produces:
8765.4321 9876.5432 98765
8765 9877 98765

Instructions

Write an overloaded print() function that calculates the decimal equivalent of the rational number and then prints the decimal equivalent with the precision specified by the function's integer argument.
Using the hint given above, set the output precision to be equal to number of digits up to the decimal point plus the number of desired decimal places of precision.

Paste your code below and click the Submit button when you are ready to submit this exercise.