The following functions are supposed to return a random int or a random double n
ID: 3717083 • 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
int getRand() const
double getRand()
The two methods int getRand() const and double getRand() have same signature exvept that one is const and other is not.
C++ allows member methods to be overloaded on the basis of const type. We can make one function const, that returns a const reference or const pointer, other non-const function, that returns non-const reference or pointer.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.