-2

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.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 1
    The term is OOP, not OOPS. Either you misheard, or you shouldn't be working at that place even if they offer you a position. Inline functions in C++ are one case where multiple definitions might be present in different object files, but the linker will merge them together keeping just one and discard others. – Tanveer Badar Aug 26 '21 at 08:26
  • 1
    Either you misrepresent the question or the answer, or the panelist doesn't know what they are talking about. – n. m. could be an AI Aug 26 '21 at 08:28
  • 2
    FYI: [Definitions and ODR (One Definition Rule)](https://en.cppreference.com/w/cpp/language/definition) (for C++). Concerning C, I'm not that sure but found this: [SO: Does C have One Definition Rule like C++?](https://stackoverflow.com/q/34986335/7478597) – Scheff's Cat Aug 26 '21 at 08:32
  • 1
    What is the definition of "_project_", then? I had several projects that produced multiple executables. And then, yes, it is possible to have multiple definitions of a function that is declared in a single header, at most one in any executable. – the busybee Aug 26 '21 at 08:42
  • And, additionally, to resolve the equivocation, you can apply techniques to rename function definitions that have equal names in their sources. – the busybee Aug 26 '21 at 08:44
  • 1
    In C++ any derived or templated class can provide an own definition of the single declaration of a method. – the busybee Aug 26 '21 at 08:47
  • @thebusybee ok i get your idea. You mean by function overriding right?. – Mark Dsylva Aug 26 '21 at 08:55
  • 1
    In C++, yes. That's why C++ compilers "mangle" the names by augmenting them with argument and return types. In the end, when the linker shall resolve the references, all identifiers must be unique. (With some exceptions, like multiple definitions of the exactly same object, given that the linker is capable of doing so.) For example, the static method `int method(float);` will have the identifier `_Z6methodf` when compiled with MinGW's GCC 8.1.0. Member methods will be augmented by their class name. – the busybee Aug 26 '21 at 09:16
  • 1
    There are several ways a function declared in a header can have multiple definitions in a project. It can have a different name each time it is included, by way of preprocessor features, or be in a different name space each time, again by preprocessor features or by being inside different `namespace` bodies each time it is included, or by being inside an anonymous namespace or inside different class declarations. It can be defined with `inline`. It can be in a header that is included in different programs in the project but only in one per program. – Eric Postpischil Aug 26 '21 at 11:50
  • 2
    Re “I said the compiler will throw an error stating multiple declarations for the function”: Generally it is the linker that complains, unless the function is defined multiple times in a single translation unit. – Eric Postpischil Aug 26 '21 at 11:51
  • 1
    If the question used "a function", you should first ask whether that means a "single function", or whether the definition could declare multiple functions. In the latter case, yes (using static). In the first case, no. A single function can only be defined one way. – Johannes Schaub - litb Sep 05 '21 at 10:04
  • Another interviewer showing off. You were wrong to be as specific as stating it was the *conpiler* that will *throw* an error, when it is the *linker* that will *give* an error, but if he has some theory about OOPS baing able to overcome the one-definition rule it is incumbent upon him to exhibit it to you, and also not to ask fringe-area questions that are only designed to show off his theory rather than your actual suitabilty for the actual position. [Exceptions are thrown: compiler and linker errors are *printed*, and inhibit all further processing. It isn't the same thing.] – user207421 Sep 05 '21 at 10:43

1 Answers1

-2

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!)