Namespaces were added to C++ since they did not exist in C. In C++, namespaces give scope for identifiers with the same name, such as variables, functions, and classes. The identifiers contained within a namespace's scope can only be accessible outside the scope by using the fully qualified names for each identifier. Namespaces are a fundamental component of C++ since they help us prevent naming conflicts within our programs. Multiple identifiers can share the same name if namespaces are utilized, as long as they are declared in different namespaces.
In this tutorial, we will go over what a namespace is, how to create one in C++, its benefits and drawbacks, and how to use namespaces.
Let us imagine that a classroom contains 2 students with the same name. We must refer to the two students by their first and last names in order to differentiate them. This kind of scenario can also happen in C++. It's possible that a function we wrote with the name
sqrt() already exists in the C++ cmath package. The compiler is unable to distinguish between the two function names in this situation. It will therefore produce an error. Namespaces are employed to prevent this kind of complication.
Namespaces offer a way to keep name conflicts out of a project. In C++, namespaces are used in order to differentiate two or more variables, methods, or classes that have the same name.
In C++, making a namespace is very much similar to making/ creating a class. In C++, a namespace is defined by using the keyword namespace and it is followed by the name of the namespace.
// function, class, and variable declarations
We are allowed to declare user-defined data types (such as classes) and nested namespaces within the namespace's scope.
Declaring every namespace member at once is not required. A namespace can be divided into several parts, and the various identifiers can then be defined according to the scope of each portion. Every separately defined component adds up to form a namespace. The term "discontiguous namespace" refers to a namespace declared in numerous parts. We are able to do this because a namespace in C++ can be divided into various sections. Later on in this essay, we shall talk about discontiguous namespaces.
The following guidelines must be followed before using namespaces in C++:
In C++, there are three ways in order to use namespaces:
To call/access any member (variable, method, or class) which is declared inside a namespace, use the scope resolution operator (::) along with the name of the namespace.
using namespace std;
cout << "Hello from namespace learnEtutorials1." << endl;
cout << "Hello from namespace learnEtutorials2." << endl;
int var = 20;
return var * 2;
// Run the greet() function present in learnEtutorials1
learnEtutorials1::greet(); // used scope resolution operator in order to call this function
// Run the greet() function present in learnEtutorials2
Hello from namespace learnEtutorials1. Hello from namespace learnEtutorials2.
In the abovementioned example, two namespaces—learnEtutorials1 and learnEtutorials2—were defined. The same function, greet(), was contained within both of these namespaces. Both of these functions were called in the main program using the scope resolution operator and the names of the respective namespaces. As a result, we were able to avoid any conflict that might have led to a mistake
We can import a full namespace from another program file into our program file with the aid of the namespace directive. The scope of the imported namespace will be universal. This directive can also be used to import a namespace into yet another namespace or even into another program.
The syntax for using directives.
using namespace namespace_name;
For example: Imagine that the namespace listed below is included in a header file called "namespace_learnEtutorial1.h":
int school_capacity = 600;
int no_of_students = 450;
Let's add the above file to the main.cpp C++ program:
// including the header file
#include " namespace_learnEtutorial1.h"
using namespace School; // using directive
// Now, any variables, functions, or classes found in the School namespace can be utilized directly.
// creating an object with the class Students Students s1 with the name s1.
The using directive allowed us to utilize the class Students in the main.cpp file even though it was created in the namespace learnEtutorial.h file.
When compared to the using directive, the using declaration is slightly different. We only use the scope resolution operator in the using declaration to import a single namespace member at a time. Only the current scope has access to the imported member.
The syntax for using-declaration
In C++, whenever we define a namespace, we can use the name of the namespace and the scope resolution operator to access any variables, functions, or classes that are declared within it.
The scope resolution operator causes the compiler to switch control of the program to the namespace block. The members specified inside a namespace block can be accessed outside the block even if a namespace doesn't have a name; this is possible without the use of the scope resolution operator (just like how global variables are accessed). If a namespace is nested, we can use the scope resolution operator more than once to access it.