Exam 11: Separate Compilation and Namespaces

arrow
  • Select Tags
search iconSearch Question
flashcardsStudy Flashcards
  • Select Tags

In C++,a compilation unit is a class or function.

Free
(True/False)
4.8/5
(29)
Correct Answer:
Verified

False

Consider the iostream library,in particular,cout and endl.Assume that the #include <iostream> has been executed.At this point,there are three ways to specify cout and endl so the compiler will be able to find these names when you output say "Hello World".Give all three methods.

Free
(Essay)
4.8/5
(34)
Correct Answer:
Verified

a)Place a using directive in you code,either globally or in the block where you will use it.Example:
using namespace std;
cout << "Hello World" << endl;
b)Place a using definition,in you code,either globally or in the block where you will use it.Example:
using std::cout;using std:endl;
cout << "Hello World" << endl;
c)Qualify each name you will use with the namespace name and the scope resolution operator.Example:
std::cout << "Hello World" << std::endl;

A function defined inside an unnamed namespace requires qualification.

Free
(True/False)
4.8/5
(32)
Correct Answer:
Verified

False

Why you would use unnamed namespaces in your programs?

(Essay)
4.8/5
(32)

If I just write code,it is not in any namespace.

(True/False)
4.9/5
(28)

Namespaces may not be nested.

(True/False)
4.8/5
(34)

Carefully distinguish the using directive using namespace std; from the using directive using std::cout;

(Essay)
4.8/5
(44)

An unnamed namespace provides a facility for collecting names that are local to the file where the unnamed namespace is located.Such names are available in that file,and are unavailable in any other file.

(True/False)
4.9/5
(34)

Any global declarations or definitions can be placed in a namespace.

(True/False)
4.7/5
(30)

You have a helping function that is not a member of your class.You want your class member functions to be able to see it and use it but no other file in your program may see it.You can place it in an unnamed namespace to make it invisible from outside the file.

(True/False)
4.8/5
(39)

Here is some code.There are only two outputs,one a message from the unnamed namespace the other a message from the Savitch namespace.Tell which line generates which message. #include <iostream> using namespace std; namespace { void message(); } namespace Savitch { void message(); } int main() { { message();//a) Savitch::message();//b) using Savitch::message; message();//c) } message();//d) return 0; } namespace Savitch { void message() { cout << "Message from NS Savitch\n"; } } namespace { void message() { cout <<"Message from unnamed NS\n"; } } 1)List the letters of the lines that give the message from the unnamed namespace: ____________________ 2)List the letters of the lines that give the message from the Savitch namespace: ____________________

(Short Answer)
4.9/5
(40)

If I have already written the #include <iostream> header,I can overload operator<< for class A as follows: std::ostream& operator<< (std::ostream& out,const A& obj);

(True/False)
4.8/5
(37)

During name resolution,nested namespaces behave like nested blocks.

(True/False)
4.9/5
(26)

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 <iostream> 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; }

(Essay)
4.7/5
(44)

In a particular file,the names from the global namespace and names from an unnamed namespace defined in the file are accesses the same way: by writing the name.

(True/False)
4.8/5
(33)

A namespace is a collection of name definitions such as class definitions,variable definitions and function definitions used to permit the same name,or names,to be used both in a library and in your own code.

(True/False)
4.9/5
(40)

Suppose the following code is embedded in an otherwise correct and complete program.Answer below the question about what version of f()is called in g(). void f();//global namespace A { \quad void g() \quad { \quad\quad f();//Does this call A::f()? Or the global f()? \quad } \quad void f(); } a)The call is to the global f(); b)The call is to the namespace A version of f(),i.e. ,A::f(); c)There is an error.There is a conflict between the namespace f()and the global f(),so there is no call made at all d)There are other errors that prevent the code from running.

(Essay)
4.9/5
(35)

What is the problem that the C++ namespace facility solves?

(Essay)
4.8/5
(41)

We are interested in providing a class,say class A,with an overloaded operator<< for output.You can't put a using directive or declaration in a function header.There are three ways to make namespace names available in a declaration of an overloaded operator<<.Give two of the three ways to declare an overloaded operator <<.Give the #include,assume that class A has been defined,then define a stand-alone operator<< function for output of a class.Only give the two declarations,not the implementation,of such overloaded functions.

(Essay)
4.8/5
(28)

The following program has been partitioned into two files.The command line command for compiling this is CC B.cpp A.cpp,where CC is the name for your command line compiler.For VC++ this is CL,for Borland,this is BCC32,and for the GNU C++ compiler,this is g++.If you use an IDE (integrated development environment)you would have to place both these files in your project then click the compile button. Predict the output and explain your prediction. // 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 <iostream> 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; }

(Essay)
5.0/5
(29)
Showing 1 - 20 of 35
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)