Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Namespace in C++ and Uses

Last Updated on March 31, 2023 by Prepbytes

Namespaces were added to C++ since they did not exist in C. Namespace is an essential feature of C++ that helps programmers to avoid naming conflicts in their code. It is a way to group identifiers such as variables, functions, and classes into a single logical entity. This article serves as a guide for people interested in learning about namespaces in C++, rules for creating namespaces in C++, the Use of namespace in C++, and the advantages and disadvantages of using namespace in C++. Let’s begin with an overview of namespaces in C++

What is Namespace in C++?

A namespace in C++ is a way to group related classes, functions, and variables together under a unique identifier. It helps to avoid naming conflicts and improves code organization.

For example, let’s say you have two functions with the same name in your code, but they do different things. Without namespaces, the compiler wouldn’t be able to distinguish between them and you would get a naming conflict error.

By putting each of those functions in a different namespace, you can tell the compiler which one you are referring to when you use the function’s name in your code.

To define a namespace in C++, you can use the following syntax:

namespace myNamespace {
    // code goes here

To use a function or variable in a namespace, you can prefix it with the namespace name, like this:


Overall, namespaces in C++ provide a way to organize your code and avoid naming conflicts, making it easier to write and maintain your programs.

Rules for Creating Namespace in C++

Before using a namespace in C++, here are some rules which must be followed in order to create a namespace in C++:

  • Namespace in C++ must be declared at global scope, and can’t be defined inside other namespaces or classes.
  • A namespace declaration starts with the keyword "namespace", followed by the namespace name and a pair of curly braces that enclose the namespace definition.
  • Namespace names must be unique and follow the same rules as C++ identifiers, i.e., they must start with a letter or underscore, and can contain letters, digits, and underscores.
  • Namespace declarations neither have public nor private access specifiers.
  • You can add any C++ entities, such as classes, functions, and variables, inside a namespace, by defining them inside the namespace curly braces.

How to Use Namespace in C++

A namespace in C++ can be used in three ways:

Using Scope Resolution Operator
To call/access any member (variable, method, or class) declared inside a namespace, use the scope resolution operator (::) with the name of the namespace.

Code Implementation:

#include <iostream>
using namespace std;

namespace prepBytes1 {
    void msg() { 
        cout << "Hello from namespace prepBytes1." << endl; 

namespace prepBytes2 {
    void msg() {
        cout << "Hello from namespace prepBytes2." << endl;
    int var = 250;
    int func() {
        return var * 2;

int main() {

    return 0;


Hello from namespace prepBytes1.
Hello from namespace prepBytes2.

Explanation: In the above example, we defined two namespaces – prepBytes1 and prepBytes2. These namespaces had the same function, msg(), inside them. In the main program, we called both these functions using each namespace’s name and the scope resolution operator. Hence, we could avoid any conflict that could have resulted in an error.

Using a Namespace Directive
We can import an entire namespace from another program file into our program file by using the namespace directive. The scope of the imported namespace will be global. This directive can also be used to import a namespace into another namespace or even another program.

For instance, suppose we have a header file called "namespace_learning.h" that contains the namespace described below:

// namespace_learning.h
Namespace PrepBytes {
    int employee_capacity = 240;

    class Students {
        int no_of_students = 2500;

Let us include the above file in a C++ program called main.cpp

// main.cpp

using namespace std;

#include "namespace_learning.h"

void func() {
    // using directive.
    using namespace PrepBytes;  
    Students s1; 

int main() {
   return 0;

NOTE: Using directives is generally discouraged because we have no idea what we have imported. It could be a variable, function, or class. We may possibly import some members which we don’t even need. It is preferable to use the scope resolution operator or the using declaration in our programs.

Using a Namespace Declaration
The using-declaration is slightly different from the using directive. With the help of the scope resolution operator, we only import one namespace member at a time in the using-declaration. The imported member is only available in the current scope.

For example, consider a file named “namespace_learning.h” that contains the following code.

// namespace_learning.h

namespace pb1 {
  void printFunc() {
    cout<<"function is inside namespace pb1"<

Now, let's import this file into our main.cpp file

// main.cpp
using namespace std;

#include "namespace_learning.h";

using namespace pb1; 

using pb2::printFunc;

int main()


    return 0;


function is inside namespace pb2
Holla from namespace pb1

Explanation: In the above program, we used the using directive to import all the members in the namespace pb1. In contrast, we used the using declaration only to import the printFunc function from the namespace pb2. When we called the printFunc() function, the printFunc() function present inside the namespace pb2 was executed. However, when we called the holla() function, the holla() function present in namespace pb1 was executed because we did not import the holla() function from namespace pb2.

Advantages of Namespace in C++

Here are some advantages of using namespaces in C++:

  • Avoiding naming conflicts: Namespace in C++ enables developers to avoid naming conflicts, particularly when multiple libraries or modules are used in the same program. Namespaces allow developers to give unique names to their classes, functions, and variables, avoiding conflicts with names used in other libraries or modules.
  • Organizing code: Namespace in C++ allows developers to organize their code in a more logical and structured way. Developers can group related classes, functions, and variables in a single namespace, making it easier to understand the code.
  • Enhancing readability: By grouping related entities together and giving them a common namespace, developers can improve the readability of their code. This makes it easier for other developers to understand the code, and to locate specific entities when making changes.
  • Encapsulation: Namespace in C++ provides a level of encapsulation by allowing developers to define the scope of their classes, functions, and variables. This means that the entities defined in a namespace can only be accessed from within that namespace, reducing the chances of unintended modifications or misuse.
  • Improved maintainability: Namespace in C++ makes code easier to maintain by providing a clear organizational structure. Developers can quickly find the code they need to modify, reducing the time and effort required to make changes.

Disadvantages of Namespace in C++

Although namespaces in C++ provide many benefits, there are also some potential disadvantages of using a namespace in C++:

  • Name collisions within a namespace: While namespaces can help avoid naming conflicts, it is still possible to have collisions within a namespace if multiple developers define entities with the same name.
  • Increased code complexity: The use of a namespace in C++ can increase the complexity of the code by introducing additional layers of abstraction. This can make the code harder to understand, particularly for developers who are new to the codebase.
  • Overuse of namespaces: Overuse of namespace in C++ can lead to code that is unnecessarily complex and difficult to navigate. Developers need to be careful to use namespaces only when they are needed and to avoid creating too many layers of abstraction.
  • Namespace conflicts with third-party libraries: In some cases, namespaces in a third-party library may conflict with namespaces used in the main program, leading to errors and difficult debugging.

Uses of Namespace in C++

Here are some of the most common uses of namespaces in C++:

  • Helps in avoiding naming conflicts
  • Helps in organizing code
  • Provides a level of encapsulation
  • Separates interface from implementation
  • Provides versioning for libraries or modules

In conclusion, the namespace in C++ is a powerful tool for organizing and managing code, reducing naming conflicts, and improving code maintainability. By grouping related entities under a common name, namespaces provide a way to create a logical hierarchy within a codebase, making it easier for developers to understand and navigate the code. With the ability to create multiple namespaces and use nested namespaces, developers have the flexibility to organize their code in a way that best suits their needs. Overall, the namespace in C++ is an essential feature for creating large, complex software projects.


Here are some frequently asked questions on the namespace in C++.

Q1: Can you have multiple namespace in C++?
Ans: Yes, you can have multiple namespace in C++. Developers can create as many namespaces as they need to group related entities together.

Q2: Can you have a nested namespace in C++?
Ans: Yes, you can have a nested namespace in C++. This allows developers to group related entities together in a hierarchical manner.

Q3: What is a namespace alias in C++?
Ans: A namespace alias in C++ is a way to create an alternative name for a namespace. This can be useful for simplifying code or avoiding naming conflicts.

Q4: Can you use the same name for a class and a namespace in C++?
Ans: No, you cannot use the same name for a class and a namespace in C++. This would result in a compile-time error.

Q5: What is the difference between a class and a namespace in C++?
Ans: A namespace in C++ is a mechanism for grouping related entities under a common name, while a class is a type that defines a blueprint for creating objects.

Q6: Can you define a namespace inside a class in C++?
Ans: Yes, you can define a namespace inside a class in C++. This is known as a "nested namespace" and allows you to organize related entities within a class.

Q7: Can namespace in C++ be used to provide versioning?
Ans: Yes, the namespace in C++ can be used to provide versioning. Developers can include the version number in the namespace name to indicate the version of the library or module being used.

Leave a Reply

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