In this lecture you will learn about the using namespaces.
So namespaces gives you a mechanism to modularize 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 cplusplus
arguments, dependent lookup mechanism for overloaded functions where the compiler will choose the function
that has the best fit according to the arguments passed to the function.
So defining namespace is simple.
For example, let's define a namespace here.
Namespace you will use the namespace keyword.
You decorate the types, functions and global variables with the namespace keyword here and the name
you give it to it.
So let's create a utilities namespace.
Here.
And.
Here.
So let's create a function in this namespace pool.
Pool Data.
Pool data.
Here.
Let's make this, for example, just the comment, the code here, and then let's add another function
here.
Integer Get data.
Get data.
Let's add another comment here because we will fill these codes in later lectures.
Code.
Here.
So as you can see here, we created a code, for example.
Example code two and example code.
Example Code one.
And then you don't use the semicolon after declaring an ending.
Namespaces here.
So now you use this 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 function.
So in which case the actual functions will have to be defined elsewhere.
So and you will need to use the qualified name here.
Uh, let's.
Uh, make it here.
Um.
You can do this like this.
For example.
Namespace.
My professional.
Your utilities.
Usually it's not best practice to make your variable names that long, but that's okay.
This is just an explanatory code here.
Then.
And then let's create a declare the functions declaring the functions.
Here, Bull.
Uh, pool data.
Pull 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 bool here.
For example, let's define the bool.
Here.
Bull utilities or not here.
You can define this code later.
Defining the functions later.
Here.
Defining the functions here.
So bool utilities here.
As you can see, we adhere to double dots here and pull data.
And then you can write the.
Code that returns bool returns a bool from this function.
And we can create the get data after as soon as you can see here, this is the integer.
So integer ops, we have a problem here.
Let's see, what is this?
Can you define or declare pull data here?
Because name does not includes namespace utilities.
As you can see, we can not 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 rule here.
Not this is an integer.
I'm sorry, integer.
Um, okay.
My professional utilities get data.
And code that returns an integer 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 this technically bug.
So.
But some colors will use it and depend on it.
When you update your code to fix the bug, you may decide to love to love your colors.
The option to use the old version so that your code does not break.
So you can do this with a 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 to our namespace version two here.
Okay.
Namespace my profession Utilities.
And then let's create a version two in this namespace.
So.
Namespace namespace v2.
And then you can declare your functions again.
And here.
Uh, pool data.
And the integer get data.
Here.
Integer knife feature.
New feature.
Here.
So now, here.
Now colors will want to specify.
It's actually make our new with version two program.
So this colors who want to specify version can call the fully qualified names for example colors could
use here my professional functions.
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 newer version and utilities pulled 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 utilities just then the first version.
So as you can see, we are using pool data now and let's use the pool data.
So there is a different functions in 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, new feature here, functions calls, get data, it will be here.
My professional.
A utilities here.
The V2.
Need to get data.
Get data.
Yes.
And that is the cult here.
So.
So it's important to note that to declare a nested namespace, you have to do the nesting manually as
shown here.
So let's create an example to write the first version of the code that will call it using the namespace
utilities.
So C plus plus 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.
Okay.
Inline namespace.
We won.
We?
Yeah, we won version one.
And.
Here.
Pull.
Pool data.
Pool data and integer.
Get data here.
And as you can see, we have namespace v2.
Also, let's actually add before we v2.
So we can it's more readable.
So now to call the version of get data here, as you can see, we got an error here.
Is ambiguous.
So now to call the first version of get data, you can use the utilities get data or utilities.
We one.
Full data.
As you can see here, it's fixed.
V1V1.
Get data.
And utilities.
We want pool data so fully qualified names can make the code 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.
Um using statement to indicate the symbols declared in the specified namespace.
The namespace can be used with a fully qualified name.
So for example, you can use the using a namespace utilities.
My professional.
My professional utilities.
And like that.