In this video, we'll briefly go over using c++ name spaces.
C++ name spaces can be a little confusing when you first see them.
As our c++ programs become more and more complex,
our programs become a combination of our own code,
the c++ standard libraries and their code
and libraries from third-party developers and their code.
So as you can imagine, sooner or later
we're going to run into the situation where company x names something
the same as company y.
When we use that name in our program, the c++ compiler
doesn't know which one to use.
So we have something called the naming conflict.
For example, suppose another library that we're using
has defined a name called cout.
Well, that's a big problem, right.
Because now c++ doesn't know which cout to use.
C++ allows developers to use namespaces
as containers to group their code entities into a namespace scope.
That means that i could create a namespace called Frank,
and I could define cout within that namespace.
Now if a programmer wanted to use my version of cout,
all they had to do is say Frank::cout.
That's it. If they wanted to use the one in the standard library,
they would just say standard::cout. It's as simple as that.
Namespaces are used
to reduce the possibility of naming conflicts.
The double colon operator is called the scope resolution operator.
It's used to resolve which name we want to use.
However, many programmers find it tedious to type
std:: all the time when using cin, cout,
endline and so forth.
So c++ provides a few mechanisms to help in that regard.
In this sample code, you can see that we're explicitly telling the compiler
to use cin,
cout and endline from the c++ standard name space using std
followed by the scope resolution operator.
This is how we've written all our code to this point.
Now let's take a look at another way we can do this without so much typing.
In this case, we can use the using namespace directive
followed by the name of the namespace we wish to use.
In this case, the standard namespace.
Notice that I no longer have to use std
followed by the scope resolution operator
when I refer to cin, cout and endline.
The compiler now knows which one to use based on the using
namespace directive.
This is pretty easy to use.
And it's the way I'll do it in this course from this point forward since it reduces code clutter
and it's better from a teaching perspective.
However, this may not be the best solution for large programs.
Using namespace standard not only brings in cin, cout
and endline,
it brings in a bunch of other names that are defined in that namespace.
So the possibility exists that you may still have a naming conflict.
C++ provides another variant of the using namespace directive.
Let's take a look at that. In this case, we have a qualified
using namespace variant
in which we tell the compiler exactly the names I want to use
from a specific namespace.
In this case, you can see I'm using three using directives,
each qualified for a specific name in a specific namespace.
The advantage of this approach
is that we can still use cin, cout and endline in our code
without the name space and scope resolution operator.
And we're not getting any other names from the c++ standard namespace.
For the examples in this course, you can use the namespace standard directive from
the previous slides with no problem.
But keep in mind that it may not be the best practice in larger programs.
That wraps up namespaces.
In the next video, we'll take a quick look at basic IO with cin and cout.