Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following functions are supposed to return a random int or a random double n

ID: 3716740 • Letter: T

Question

The following functions are supposed to return a random int or a random double number. However the following function overloading is illegal in C++ since the parameters of both functions are the same (they both do not pass any parameters to the function):

int getRand();

double getRand(); //ERROR!!!

One way to fix this error and have two functions to return a random int or double is to have different function names, i.e.:

int getRandInt();

double getRandDouble(); //CORRECT!

What is an alternative way to still do function overloading (having the same name for the both above functions) where one returns a random int and the other returns a random double without any errors?Write the function prototypes and explain briefly in up to 3 sentences.

Explanation / Answer

We can pass different type of argument in function to differentiate two function with same name.

int getRand(int n);

double getRand(double n);

this n can be the upper bound of random number

So, by doing like this, these methods are overloaded now.

getRandom(4); // return random number in range 0 - 4

getDouble(4.0); // return random number in range 0 - 4.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote