Tutorial Study Image

C++ Namespaces


June 13, 2023, Learn eTutorial
221

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.

What exactly is a Namespace?

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.

Namespace creation

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.


namespace MyNamespace
{
    // 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.

Namespace Declaration Features

The following guidelines must be followed before using namespaces in C++:

  • The global scope or a nested namespace must be used to declare a namespace.
  • For our namespace names, we can utilize aliases (alternative names).
  • Namespaces may not have names. Unnamed namespace members are considered as though they were a part of the parent namespace.
  • A namespace definition can be applied to several files. They are not altered or replaced.
  • Public and private access specifiers are absent from namespace declarations.

How to Use a Namespace in C++?

In C++, there are three ways in order to use namespaces:

  • Using Scope Resolution Operator
  • Using Directive
  • Using Declaration
  1. Using Scope Resolution Operator (::)

    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.

     

    
    #include <iostream>
    using namespace std;
    
    namespace learnEtutorials1
    {
        void greet() 
        { 
            cout << "Hello from namespace learnEtutorials1." << endl; 
        }
    }
    
    namespace learnEtutorials2
    {
        void greet()
        {
            cout << "Hello from namespace learnEtutorials2." << endl;
        }
        
        int var = 20;
        
        int func()
        {
            return var * 2;
        }
    }
    
    int main()
    {
     // 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
     learnEtutorials2::greet();
     
     return 0;
    }
    
     
    

    Output:

    
    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

  2. Using Directive

    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":

    
    // namespace_learnEtutorial1.h
    namespace School
    {
        int school_capacity = 600;
        
        class Students
        {    
            int no_of_students = 450;
        };
    }
     
    

    Let's add the above file to the main.cpp C++ program:

    
    // main.cpp
    
    // including the header file
    #include " namespace_learnEtutorial1.h"
    
    void school()
    {
        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.
    Students 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.

  3. Using Declaration

    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

    
    using namespace_name::member_name;
     
    

How C++ Uses Namespaces

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.

Benefits of Namespaces

  • Ease of usage of libraries: Namespaces allow us to utilize many libraries in one program and differentiate variable names using the scope resolution operator.
  • Reusable variable names: In a single C++ program, we can utilize the same variable names more than once.
  • Improved code readability: Improved code readability can be achieved by utilizing namespaces to specify similar code across several files and libraries.

Drawbacks of Namespace's

  • Confusing function calls - If functions are defined in numerous places, the namespace may lead them to be called ambiguously.
  • Utilizing the using directive incorrectly can result in unexpected program behavior.

Conclusion

  • We can bundle things like variables, classes, objects, as well as functions under a single name by using namespaces.
  • Namespaces may or may not contain names.
  • A namespace can be defined in several files and sections. Each component is added together to create the final namespace.
  • There are three ways to use namespaces in C++: using declarations, utilizing directives, and the scope resolution operator.