Question:
Another basic C++ question?
Eugene
2011-07-28 06:34:08 UTC
There is more than one version of "pow" in the math library.
Two versions are (prototypes shown):

double pow(double base, double exponent);
double pow(double base, int exponent);

a. What term describes the C++ feature that allows multiple
versions of function pow?

b. *Briefly* say how the compiler will know which version
to use if function pow is called in your code?
Three answers:
2011-07-28 06:38:34 UTC
a. This is known as overloading functions.



b. The signature of the function tells the compiler which version of pow is called. If you pass 2 doubles, it uses the first version. If you pass a double and an int, it uses the second version.
2011-07-28 13:36:54 UTC
this is called function overloading....

same function name but different parameters...



compiler successfully calls the version you want to call because of the parameter types.

The compiler know about the parameters being passed to call the function, on the basis of

those parameters, compiler can call the appropriate version of the function...



even you can also overload functions based on const..



int foo(int a);

int foo(const int a);



so if a const variable will be passed, const version will be called, and if simple variable is passed then normal version will be called... and this really comes handy with const objects, which allow you to call only const function.
Lithium
2011-07-28 13:44:16 UTC
its called function overloading.. hope that helps


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...