In this lecture, you will learn about the using namespaces.
So namespaces gives you a mechanism to modularized code.
So a namespace allows you to label your types, functions and variables with unique name so that using
the scope resolution operator, you can give a fully qualified name.
So the advantage is that you know exactly which item will be called.
And the disadvantage is that using a fully qualified name, you are in effect switching of C++ arguments,
dependent lookup mechanism for overloaded functions where the compiler will choose the function that
has the best fit according to the arguments priced to function.
So defining namespace is simple.
For example, let's define a namespace here.
Namespace you use the namespace keyword.
You declare the types, functions and global variables with the namespace keyword here and the name
you give it to it.
So let's create the utilities namespace.
Here.
And.
Here.
So let's create a function in this namespace pool pool data.
Pull data.
Here.
Let's make this, for example, just the comment, the quote here, and then let's add another function
here, integer.
Get data.
Let's add another comment here, because we will fill these courts in later lectures code.
Here.
So as you can see here, we create a code, for example.
Example code two and example for example, code one.
And then you don't use this semicolon after declaring and link namespaces here.
So now you use these symbols here.
For example, if if utilities namespace pool data returns true, then you will get the.
Then you create the integer number.
Utilities get data.
And here, let's add an example.
We will use e.
Variable here.
So then the namespace declaration may just declare the functions, so in which case the actual functions
will have to be defined elsewhere.
So and you will need to use the qualified name here.
Let's.
Make it here.
You can do this like this.
For example, namespace.
My professional.
Utilities.
Usually it's not best practice to make your variable names that long, but it's okay.
Is just an explanatory code here.
Then.
And then let's go declare the functions.
Declaring the functions.
Here, Bull.
Pull data.
For data.
And integer.
Get data.
As you can see, we can declare the same function names.
Because of that, these two utilities doesn't have to do with each other, so they are in the different
namespace so you can declare the same named functions here.
Because they are they they don't have to interact with each other.
So.
Then we can create a pool here.
For example, let's define the pool.
But here.
Bull utilities or not your.
You can define this called letters.
Defining the functions later here, defining the functions here.
So pool utilities, as you can see, we are here to double dots here and pool data.
And then you can write the.
Court that the person who returns a bull from this function.
And we can create the data after that.
As you can see here, this is the integer.
So integer.
Oops, We have a problem here.
Let's see.
What is this?
Cannot define or declare pull data here because the name does not enclose namespace utilities.
As you can see, we can now use the.
Another.
As you can see, there is a different namespace, so we need to add here my professional utilities.
And as you can see here, error is gone.
So let's add another bool here.
Not this is an integer sorry integer OC.
My professional utilities get data.
And code that returns an integer or in this function.
So one use of a namespace is to version your code.
So the first version of your code may have a side effect that is not in your functional specification
and is technically back.
So about some colors will use it and depend on it.
When you update your code to fix the bug, you may decide to love to allow your colors the option to
use the old version so that their code does not break.
So you can do this with the namespace.
But before that.
I want to hear.
Let's use only my professional code.
So I just commented this code and you can use later here.
And.
Let's go within our namespace version two here.
OC namespace My profession Utilities.
And then let's create a version to in this namespace.
So.
Namespace.
V two.
And then you can declare your function again.
And here.
Pull data.
And the integer get data.
Here.
Integer Neave Future new feature.
Yes.
So now here, now colors we want to specify.
It's actually make our new weird aversion to program.
So this callers who want to specify aversion can call the fully qualified names for example colors could
use.
Here my professional functions.
We two and here pull data.
As you can see here, there's no problem.
So we can use this namespace inside the namespace.
So to use the so we can use this and newer version and utilities pull data.
My professional utilities pull data to use the older version so we can use this is the new version version
of our namespace and we can use the professional my.
Officer, we have to end quote here.
My professional utility is just the first version.
So as you can see, we are using pull data now and let's use the pool data.
So there are different functions in the same namespace.
So when an item in a specific namespace calls an item in the same namespace, it does not have to use
qualified name.
So if the new feature here we which are here functions calls get data, it will be here.
My professional.
Utilities here.
V to.
Need to get data.
Get data.
Yes.
And that is the cult.
So.
So it's important to note that to declare a namespace, you have to do the nesting manually as shown
here.
So let's create that an example to write the first version of the code that will call it using the namespace
utilities.
So C++ 11 provides a facility called an inline namespace that allows you to define a nested namespace,
but lost the compiler to treat the items as being in the parent namespace when it performs an argument
dependent lookup.
So let's create an inline namespace here.
K inline namespace.
We won.
We Yes, we won.
Verizon won.
And.
Here.
Pull.
Pull data.
Pull data and integer.
Get data here.
And as you can see, we have namespace V.
Also, let's actually add before we v two.
So we can.
It's more readable.
So now to call the version of data here, as you can see, we got an error here.
Is ambiguous.
So not to call the first version of data you can use the utilities, get data or utilities.
We want.
Full data.
As you can see here, it's fixed.
We won.
We want.
Get data.
And utilities.
We want pool data so fully qualified names can make the record difficult to read, especially if your
code will only use one namespace.
So to help you here you have several options.
You can place a using statement using statement to indicate the symbols declared in the specified namespace.
Same space can be used without a fully qualified name.
So for example, you can use the using a namespace utilities.
My professional.
My professional utilities and like that.