Ad Hoc Polymorphism  «Prev 

Overloading a function - Results

You entered:

Here is one solution to adding a new print() function to the rational class that takes an integer argument representing the number of decimal places of precision to print.
This code is also available in a file named ovlprint.cpp, which can be found in the compressed course download file available on the Resources page.

void rational::print (int p) {

 double fp_result = static_cast<double>(a/q); 
 int result = a/q;
 int digits, t;
 for (digits =0, t = 1; t <= result; t *= 10, digits++);
 cout.precision(digits + p);
 cout << fp_result;