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...

When data is changed using a form, the changes are automatically saved

Indicate whether the statement is true or false

Computer Science & Information Technology

When the ____________ feature is turned on, the window is displayed only as a tab along one of the edges of the Visual Studio environment.

a. Auto Hide b. Pushpin c. Economy View d. Tab

Computer Science & Information Technology