Namespaces

6 minutes
Share the link to this page
Copied
  Completed
Namespaces are named program regions used to limit the scope of variables inside the program. They are used in many programming languages to create a separate region for a group of variables, functions, classes, etc. The usage of namespaces helps to avoid conflict with the existing definitions. Namespaces provide a way of implementing information hidden. Some examples of namespaces in Java are classes and packages. Examples of namespaces in C++ are classes, namespaces, and struct.

Transcript

Hello and welcome to the 26th tutorial in the c++ programming series. And in this call, we'll be looking at namespaces namespaces allow variables and methods to be grouped allow multiple instances with the same name. And to access them you just use the namespace and then you access to a particular variable or method and they will hold their own variable for example, will hold their own data compared to a namespace that is named slightly different but may contain a variable that has the same name. So this is show you how namespacing work to go hand in hand with classes which will be covered later on in this series. But you can use it with our classes as well. So the syntax for a namespace is essentially to do a word name base than the actual name of your namespace, then put numbers.

Then inside here, whether you just put your variables or methods on Have you been in or equal nine, float, equal 8.75 then we're going to have a function with say namespace one to the console with a very simple function for the purpose of this tutorial. So phase one. Now that's the new line. And now what we can do is simply copy and paste this. I'm going to change a few fingers, put the number two here, we're gonna change this to 10 to 1.2 fold. You don't have to put these numbers all the names either there's some numbers that are making up I'm planning here for the purpose of this tutorial is to name phase two.

As you can see, both contain an integer and a float variable with the same name but they are actually separate variables belonging to different basis and to access a namespace is extremely simple as well what you do is numbers or numbers to colon colon, I can even assign the value free to it now what we're going to do STD C and number so colon colon or STD and line will create a value for it. As you can see we've accessed the one point colon corner very similar to the STD before to access CO and lines Do you know anything else part of the standard namespace because I would have said it's the standard namespace. And we'd have our own namespace now which is called numbers. So if we were to just access it from numbers to and we're gonna get a value of 10 because even though the variables have this same name, they are essentially different variables, they belong to separate namespaces.

So it's a very easy way to actually access it, instead of having just a one variable name, which doesn't obviously doesn't doesn't allow you to have multiple variables the same name with the same uppercase or lowercase system. Therefore, you have to have different names, even though they made essentially the same thing. So you can assign different namespaces. Now what we're gonna do is numbers, column column is going to use the function in the same way as you would use for a variable. Obviously put the brackets of the airflow compiler notice a function, colon, colon for namespace to maybe run this in namespace, one and get namespace. Two, you can put using namespace then the namespace name, and then the namespace doesn't need to specify but be careful as in this example, the variables and method it's automated based on them.

Same. Let me just show you what I mean. So if I do using the names, B, then you put your namespace names on putting numbers. So the notification going off on every single device. There you go, obviously, cuz he hadn't been declared yet. It was saying that there's no namespace code numbers.

So if I were to just access function on its own dialect has been running, it runs the first namespacing function. And this is simply because we've done using the using namespace number, so anything in here, we don't actually have to specify nice, there's nothing wrong with it. We can still do it. As you can see, it works right there for this slide here. But one thing you got to be careful if we weren't Copy and paste this and put a two here I'm on your file, because it says quote a function is ambiguous. Because both of these now don't require the namespace you could put a keyword function to call this method.

We could also call this method so the compiler not sure which method you're trying to call, so usually don't want to be using ever made basically to have variables and methods that are named the same. But if you do an easy way to access a separate namespaces variable or method in literally just put today's date in front of it, then put colon colon and that is it really for namespaces. That only has to do in the next tutorial, we'll be covering arrays. If you have any questions, feel free to message us at support at solar systems coda UK, the email will be in the description you can comment in this video just directly messages via YouTube, wherever you feel comfortable with. And as usual, thanks for watching and I hope you have a nice day.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.