I had an interview today and the panelist asked me this question. I said the compiler will throw an error stating multiple declarations for the function. He said its possible using OOPS.
Can anyone help me out here.
I had an interview today and the panelist asked me this question. I said the compiler will throw an error stating multiple declarations for the function. He said its possible using OOPS.
Can anyone help me out here.
This is called function overloading. It means having more than one function with the same name but different arguments. It is a concept in OOPS. Let's learn it through an example. Let's assume you need to create an exit()
function. Let's say you need one exit
function which takes an exit message (const char *
) and another which just takes an exit code (int
). In C++, you can do the following:
void my_exit(char *_Msg);
void my_exit(int);
However, ambiguous argument types are not allowed. Consider the following:
void print_decimal(double);
void print_decimal(float);
This will produce an error because a number like 12.5
can be expressed both as a float and a double (this confuses the compiler).
(Let me know if I made a mistake, have a great day!)