The following program has been partitioned into two files. Before we commented out the keyword namespace and the curly braces that were around func(int) this program compiled and had this output:

```
func(5) = 25
junk(5) = 75
```
Will the code compile now? If so, predict the output and explain.
```
// This goes in file A.cpp
//namespace //
//{
int func(int i)
{
return i*3;
}
//}
int junk (int i)
{
return i*func(i);
}

// This goes in file B.cpp
#include
int func(int i)
{
return i*5;
}
int junk(int i); //from A.cpp
int main()
{
cout <<”func(5) = “ << func(5) << endl;
cout <<”junk(5) = “ << junk(5) << endl;
```
What will be an ideal response?


Compilers compliant with the Standard in this respect will say that func(int) is
multiply defined. Inexplicably, one compiler links to the version of func(int)
from file A.cpp for all calls to func(5).
Explanation: C++ does not allow two definitions for any object to be visible in any
namespace. Both the definition of func(int) in file A.cpp and the definition of
func(int) in B.cpp are both in the global namespace, hence together are illegal. (VC++6.0, which is packed with the text, and the GNU C++ compilers, g++-
2.9.5 and g++-3.0 give the error message: “multiply defined function
func(int)”. The Borland command line compiler BCC 5.5 compiles the program,
ignores the multiply defined function, and links to the version of func(int) from
file A.cpp.)

Computer Science & Information Technology

You might also like to view...

Word provides default tab stops at _____ intervals.

A. 0.5" B. 1" C. 0.75" D. 0.25"

Computer Science & Information Technology

What is Empty (void) element?

What will be an ideal response?

Computer Science & Information Technology

The Android system uses a(n) __________________ as a means of identifying the author of an application and establishing trust relationships between applications.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

In the figure above, letter "B" points to what?

What will be an ideal response?

Computer Science & Information Technology