C++ Tutorial #17: Using Namespaces

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Today I’m going to show you how to create your very own namespaces. Namespaces are useful, and it helps avoid name collisions with variables. Here’s an example: You have several functions named getValue(), you can use namespaces to categorize or organize these functions. Here’s another example: highScore::getValue() and playerScore::getValue(). Think of them like a folder for your functions and classes (we’ll discuss classes in another tutorial). But before we get started on namespaces, let’s talk about scopes. You will need to learn the different types of scopes before you write your own namespaces.

 

Scopes

 

In previous tutorials, I have told you to avoid giving variables the same name. because it will reset the value of that variable. However, that’s not true in all cases, you can have two or more variables with the same name on different scopes.

 

Variables that are outside any block has a global scope, these variables are called global variables. Variables that are inside a block has a block scope, and these variables are called local variables.

 

Take a look at the image below: There are two variables named int x. The first one is a global variable with the value of 100. The second variable is a local variable with the value of 200. The program will print the local variable first because the first time the cout is used is in the block scope. After that, the program will print the global variable. While the cout command is in a block scope, there wasn’t a variable declared in that block scope so the program printed out the global variable instead.

 

If there was a variable declared in that scope it would have printed out that variable instead of the global variable. This means that you can have multiple local variables inside of different block scopes, but you can have only one global variable. You’re welcome to try out this code shown in the image. When you’re ready to move on to namespaces, you can continue reading this post.

 

Global and Local Variable Example

 

 

Namespaces

 

 

Create a blank .cpp file and let’s get started. Copy this code:

 

 

 

Your program should look like this:

 

vertices: cube = 8, cylinder = 0
faces: cube = 6, cylinder = 2
edges: cube = 12, cylinder = 0

 

Let me explain the code:

 

We created two namespaces: cube and cylinder. Inside each of these namespaces we have three functions: vertices(), faces(), and edges(). In these functions, we made the program return the correct number for each function.

 

 

 

In our main function, we use cout to print out the value of each function from both namespaces.

 

 

 

I know what you’re thinking: “Why can we just use variables alone to print out the values?” The answer is you could, and that’s more efficient. I’m showing you how to accomplish the same thing using other things such as namespaces, functions, and classes. When working on large projects namespaces are used to avoid name collisions.

 

We have used two namespaces in the past tutorials: std and sf. In the Standard Namespace (std), we can use commands such as cout, cin, and endl. Then we have our SFML Namespace (sf) that have functions we use to draw images, handle controls etc. But what if these namespaces have the same function names or the same classes? Again, that’s why we have namespaces, to avoid name collisions. If however, you know that there won’t be any conflict with any namespaces, you can write out “using namespace std; for example, that makes it so you don’t have to write out: std::cout, std::cin, and std::endl all the time.

 

I hope you have enjoyed this tutorial. Next time, we will work on classes and work on projects with separate files. Thanks again for reading!

 

Facebooktwittergoogle_plusredditpinterestlinkedinmail

About Justin

Computer programmer and owner of kauergames.com.
Bookmark the permalink.

One Comment

  1. I have noticed you don’t monetize your page, don’t waste your traffic, you can earn extra bucks every month because you’ve got high quality content.
    If you want to know how to make extra money, search for:
    Boorfe’s tips best adsense alternative

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.