Deck 11: Separate Compilation and Namespaces
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/35
Play
Full screen (f)
Deck 11: Separate Compilation and Namespaces
1
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
2
You can use the static qualifier with a global function,class or variable to get the same effect as an unnamed namespace.
False
3
In C++,a compilation unit is a class or function.
False
4
Namespaces may not be nested.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
5
During name resolution,nested namespaces behave like nested blocks.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
6
Any global declarations or definitions can be placed in a namespace.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
7
The scope of a using directive or using declaration is from its position in the block to the end of the program.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
8
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.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
9
A namespace grouping requires a semicolon after the closing curly brace.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
10
You can use #define to define a name for a C++ variable.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
11
Namespaces can be defined in pieces across several files.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
12
If I have already written the #include header,I can overload operator<< for class A as follows:
std::ostream& operator<< (std::ostream& out,const A& obj);
std::ostream& operator<< (std::ostream& out,const A& obj);
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
13
A namespace is just a class with all the folderol stripped off.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
14
If I just write code,it is not in any namespace.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
15
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.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
16
A function defined inside an unnamed namespace requires qualification.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
17
The include statement,#include looks in the system defined directory for the file,file.h.(Windows PC and Macintosh users sometimes use "folder" for what I call "directory". )
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
18
The include statement,#include "file.h" looks first in the system defined directory for file.h then,if the file is not found,it looks in the user's current directory.(Windows PC and Macintosh users sometimes use "folder" for what I call "directory". )
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
19
You can have a name spelled the same in two different namespaces with no conflict in your program.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
20
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.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
21
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
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;
}
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
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;
}
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
22
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
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: ____________________
#include
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: ____________________
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
23
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;
}
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;
}
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
24
Carefully distinguish the using directive
using namespace std;
from the using directive
using std::cout;
using namespace std;
from the using directive
using std::cout;
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
25
The output from the following code is
"function".
(The code will compile and run. )
#include
using namespace std;
namespace
{
char c_string[ 10 ] = "namespace";
}
void output()
{
char c_string[ 10 ] = "function";
cout << c_string << endl;// which c_string?
//other code
}
int main()
{
output();
}
"function".
(The code will compile and run. )
#include
using namespace std;
namespace
{
char c_string[ 10 ] = "namespace";
}
void output()
{
char c_string[ 10 ] = "function";
cout << c_string << endl;// which c_string?
//other code
}
int main()
{
output();
}
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
26
Given the namespace groupings that contain class definitions:
namespace Savitch
{
class String
{
public:
char* fetchString( );
void storeString(char[]);
private:
char str[ 100 ];
int length;
};
}
namespace Teague
{
class String
{
public:
char* fetchString( );
void storeString(char[]);
private:
char str[ 100 ];
int length;
};
}
Create an object of class String,s1,from namespace Savitch and an object,s2,of class String,s1,from namespace Teague.Use a member function to give to each object a C-string message that tells of which namespace the
class is a member.
namespace Savitch
{
class String
{
public:
char* fetchString( );
void storeString(char[]);
private:
char str[ 100 ];
int length;
};
}
namespace Teague
{
class String
{
public:
char* fetchString( );
void storeString(char[]);
private:
char str[ 100 ];
int length;
};
}
Create an object of class String,s1,from namespace Savitch and an object,s2,of class String,s1,from namespace Teague.Use a member function to give to each object a C-string message that tells of which namespace the
class is a member.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
27
You can declare several names from a namespace in one declaration:
#include
using std::cout,std::cin,std::endl;
#include
using std::cout,std::cin,std::endl;
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
28
What is the problem that the C++ namespace facility solves?
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
29
You want to use only one name,funct1,from name space MyNamespace.The directive #include "MyNamespace" has been places at the top of the file.You will call this function a large number of times in a block.Which of the following will make only the name funct1 available only in that block (not outside the block)?
A)Place using namespace std;
Just after your #include directives.
B)Place using namespace MyNamespace;
Just after your other #include directives.
C)Place using namespace MyNamespace;
Just inside the block where you want to use funct1.
D)Place using MyNamespace::func1;
Just inside the block where you want to use funct1.
E)Place #include "MyNamespace" at the top of the file where you are using the name func1.
A)Place using namespace std;
Just after your #include directives.
B)Place using namespace MyNamespace;
Just after your other #include directives.
C)Place using namespace MyNamespace;
Just inside the block where you want to use funct1.
D)Place using MyNamespace::func1;
Just inside the block where you want to use funct1.
E)Place #include "MyNamespace" at the top of the file where you are using the name func1.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
30
Consider the iostream library,in particular,cout and endl.Assume that the #include 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.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
31
Suppose we have the following namespace:
namespace A
{
void f(int);
//...
}
using namespace A;
// In the global namespace
void g()
{
f('a');// calls A::f(int)
}
In the global namespace,the function g( )calls f('a').There is no problem here,f('a')is unambiguously A::f(int).Now suppose at some later time,the following namespace grouping and using directive is inserted prior to the call to function f.
namespace B
{
void f(char);
//...
}
using namespace B;
For convenience,all these are listed again.
namespace A
{
void f(int);
//...
}
using namespace A;
namespace B
{
void f(char);
//...
}
using namespace B;
// In the global namespace
void g()
{
f('a');
}
In g(),to what does the call,f('a')resolve? How could the code be modified so that the call still resolves to A::f(int)?
namespace A
{
void f(int);
//...
}
using namespace A;
// In the global namespace
void g()
{
f('a');// calls A::f(int)
}
In the global namespace,the function g( )calls f('a').There is no problem here,f('a')is unambiguously A::f(int).Now suppose at some later time,the following namespace grouping and using directive is inserted prior to the call to function f.
namespace B
{
void f(char);
//...
}
using namespace B;
For convenience,all these are listed again.
namespace A
{
void f(int);
//...
}
using namespace A;
namespace B
{
void f(char);
//...
}
using namespace B;
// In the global namespace
void g()
{
f('a');
}
In g(),to what does the call,f('a')resolve? How could the code be modified so that the call still resolves to A::f(int)?
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
32
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.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
33
Use a namespace grouping to insert this declaration,void greeting();,in namespace Problem2.Then,in another,separate namespace grouping,add the definition of greeting()in namespace Problem2.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
34
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
{
void g()
{
f();//Does this call A::f()? Or the global f()?
}
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.
void f();//global
namespace A
{
void g()
{
f();//Does this call A::f()? Or the global f()?
}
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.
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck
35
Why you would use unnamed namespaces in your programs?
Unlock Deck
Unlock for access to all 35 flashcards in this deck.
Unlock Deck
k this deck