C++ Namespaces with examples

The C++ programming language offers various useful and efficient features and functionalities to programmers. It also supports object-oriented programming concepts. In C++, Namespaces are one of the most useful concepts.

What are Namespaces in C++?

With the help of Namespaces in C++, it is easy to handle applications by organizing too many classes. It is more like a container for identifiers. It is mainly used to put the names of its members in a different space to prevent name collisions which will occur when your program includes multiple libraries.

The global namespace is also known as the root namespace in C++. You have to use namespace_name::class_name to access the class of a namespace.

Defining a namespace in C++

In C++, you have to use the namespace keyword followed by the namespace as below:-

namespace mynamespace{
// declarations
}
int main(){
// statements
}

Rules to follow before defining a namespace

In C++, there are certain rules which you have to follow before defining a namespace.

  • You should always define a namespace at global scope or nested inside another namespace.
  • There is no need to put a semicolon at the end of the namespace definition.
  • For ease of use, you can put an alias name for your namespace name like below:-
    namespace TechVidvan{
    void tutorial();
    class Do{
    // class definition
    };
    }
    namespace Tech = TechVidvan;
  • There are no access specifiers in namespace declarations.
  • You can also split the namespace into several units.
    #include <iostream>
    using namespace std;
    namespace first{
    void fun() {
    cout << "C++ Namespaces!" << endl;
    }
    }
    namespace second{
    void fun() {
    cout << "Namespaces in C++!" << endl;
    }
    }
     
    int main () {
    first::fun();
    second::fun();
    return 0;
    }
    

Output:-

C++ Namespaces!
Namespaces in C++!

Using Namespaces in C++

In three ways, you can use a namespace in C++:-

1. Using Scope Resolution Operator

Using the namespace name, you can explicitly specify any name declared in a namespace and the scope resolution ‘::’ operator with the identifier.

namespace Tech
{
class A
{
static int a;
public:
void fun();
};
class B;    
}
int Tech::A::a=23; 	 
class Tech::B
{
int b;
public:
int val()
{
cout << b;
}
B();   
}
Tech::B::B()   
{
  b=0;
}

2. Using the directive

You can import an entire namespace into your program with the help of using directive. Let’s say we created a header file named Namespace1.h:-

namespace Tech
{
int x;
class PleaseCheck
{
int i;
};
}

Below, we included the above namespace header file in Namespace2.h file:-

include "Namespace1.h";

namespace Tech1
{
  using namespace Tech;
  PleaseCheck obj;
  int y;
}

Above, we imported the namespace Tech into namespace Tech1 and the class PleaseCheck is now accessible in the namespace Tech1.

Below, we created the following program in a different file named test.cpp:-

#include "Namespace2.h";

void test()
{
using Namespace Tech1;
PleaseCheck obj2;
}

You can use namespace wherever you want with the help of using directive.

3. With Using declaration

With the help of using declaration, you can import one specific name at a time and it is available only inside the current scope.

NOTE:- You have to be careful of using the using declaration because it can override the name imported with using directive.

Let’s say we created a file named Namespace.h:-

namespace Tech
{
void fun1()
{
cout << "Just testing Function1 of namespace Tech!\n";
}
void fun2()
{
cout << "Just testing Function2 of namespace Tech!\n";
}
}

namespace Tech1
{
void fun1()
{
cout << "Just testing Function1 of namespace Tech1!\n";
}
void fun2()
{
cout << "Just testing Function2 of namespace Tech1!\n";
}
}

Below, we created a program named test.cpp and included the above header file into it.

#include "Namespace.h";

void testing()
{
using namespace Tech;  
using Tech1::fun1;
fun1();    
Tech::fun1();
}

Output:-

Just testing Function1 of namespace Tech1!
Just testing Function1 of namespace Tech!

Nested Namespace in C++

You can also define a namespace inside another namespace in C++.
Example:-

namespace namespace_1{
  // statements
   namespace namespace_2{
   	// statements
   }
}

Discontinuous Namespace in C++

It is mainly used to define a new namespace or you can add new elements to an existing namespace through it.
Example:-

namespace namespace_name{
   // code
}

Need for namespaces in C++

Let’s say that you are in a situation where you have a function named abc() in your program and there is a predefined library available that has the same function abc(). Now, when we compile the program then the compiler will get confused between these two different abc() functions.

So, to prevent this, the namespace is used as additional information to differentiate similar functions, variables, classes, etc. with the same name available in different libraries. It is used widely in several programs.

Summary

In this tutorial, we discussed namespaces in C++and how to define them. We talked over some rules which you should follow before working with the namespaces. Then we saw three ways in which you can use namespaces in C++. We talked over the discontinuous and nested namespace in C++. We discussed the need for namespaces in C++.