Will the code compile now? If so, predict the output and explain.

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
```
// 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;
}
```


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

(Alphabetizing Strings) Use the string-comparison functions discussed in Section 22.10 and the techniques for sorting arrays developed in Chapter 7 to write a program that alphabetizes a list of strings. Use the names of 10 towns in your area as data for your program.

What will be an ideal response?

Computer Science & Information Technology

A column is also known as a record.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

Pressing Ctrl + Home moves the insertion point to ________

A) the beginning of the document B) the beginning of the current line C) the end of the current line D) the end of the document

Computer Science & Information Technology

Labels are numeric data that can be used in calculations. _________________________

Answer the following statement true (T) or false (F)

Computer Science & Information Technology