Namespace and Using Flashcards

1
Q

What is a Namespace?

A

A place to store and provide scope to variables, functions, classes, etc. to provide obfuscation and avoid namespace collision.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Namespace and Using

How do you create a namespace?

A

namespace namespace_name {
…Code in namespace…
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Namespace and Using

How do you reference a namespace?

A
  • directly by name (namespace_name::var/function/class)
  • via the ‘using’ keyword. (using namespace_name)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Namespace and Using

If two files are imported with the same namespace, what happens?

A

The namespaces are merged into a single namespace.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Namespace and Using

What is the default namespace?

A

The Global namespace

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Namespace and Using

What is the benefit of the global namespace?

A

Things in the global namespace can be accessed without ‘using’ or ‘::’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the ‘using namsepace’ keyword do in a .cpp file?

A

It brings the mentioned namespace into the global namespace.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the ‘using namsepace’ keyword do in a .h file?

A

It pulls the headerfile’s material in the mentioned space.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why should the ‘using’ keyword be avoided in .h files?

A
  • It can create subnamespaces if namespaces are created in the .h file.
  • It pulls the .h file into the “using” namespace rather than the global namespace.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Namespace and Using

What is a subnamespace?

A

A namespace that can only be accessed from the namespace it was declared in.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Namespace and Using

What does a directive allow you to do?

A

Specify single elements from a namespace that you can include in your global namespace.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you declare a directive?

A

using namespace::element;

ex: using std::cout;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the error “____ is ambiguous” signify?

A

That the namespace of an object / method needs to be specified as there is namespace collision.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the ‘friend’ keyword?

A

It Grants access to viewing and interacting with the private member variables / methods of the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the rules to the ‘friend’ keyword?

A
  • Only offered, not taken.
  • Not bidirectional.
  • Friendship is not inherited.
  • Inheritence is not friendship.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What can you friend?

A
  • Specific methods / functions
  • entire classes
17
Q

What is the most common usage of friend?

A

The &laquo_space;output operator overload.

18
Q

Where is friendship declared?

A

Inside of the class that is granting access.

19
Q

What are the rules to friending a function?

A
  • The function must be in the same namespace
  • implementing them must be done in the same environment.
20
Q

Given Class MyClass and Struct MyStruct what is the syntax to friend each of these?

A
  • friend class MyClass
  • friend struct MyStruct
21
Q

Namespace and Using

Why would you use an alias longer than the command name?

A

If the type of certain variables needs to be updated, it allows you to update only 1 variable, rather than all of them.

22
Q

Namespace and Using

How do you create an Alias in C++?

A

using alias_name = command;

ex: using ll = long long;