Categories: C++

Namespaces in C++

What are Namespaces in C++?

Namespaces are logically separated sections of a program. They are required if you want multiple functions with the same name. These functions can be declared in two different namespaces and called by referencing the corresponding namespace.

It is similar to referring to the second names of two people who share the same first name. So namespace instructs the compiler to use one of the two identically named functions.

Assume you have two functions named calculate(), each of which performs a different task. One calculate() function performs multiplication, while another performs addition. To avoid ambiguity, you will declare both functions in separate namespaces in this case. These namespaces will distinguish both functions and provide information about both functions.

Defining a Namespace:

As shown below, a namespace definition begins with the keyword namespace, followed by the namespace name −

namespace namespace_name {
   // code declarations
}
int main() 
{
    // main function
}
Namespace creation rules:
  • The namespace must be defined at the global level or nested within another namespace.
  • The namespace definition doesn’t terminate with a semicolon like in the class definition.
  • For convenience, you can use an alias name for your namespace name.
  • Namespace instances cannot be created.
  • Unnamed namespaces are also possible. Each translation unit has its unnamed namespace. They behave identically to named namespaces.
  • A namespace definition can be carried over and extended across multiple files; it is not redefined or overridden.
Ways to use a namespace:

There are three ways to use a namespace in the program,

  • Scope resolution operator (::)
  • The using directive
  • The using declaration
Example: Scope resolution operator
#include <iostream>
using namespace std;

// first name space
namespace first_space {
   void func() {
      cout << "Inside first space" << endl;
   }
}

// second name space
namespace second_space {
   void func() {
      cout << "Inside second space" << endl;
   }
}

int main () {
   // Calls function from first name space.
   first_space::func();
   
   // Calls function from second name space.
   second_space::func(); 

   return 0;
}
Output:
Inside first_space
Inside second_space
Example: The using directive

You can use using keyword to import an entire namespace into your program with a global scope. It can import a namespace into another namespace or any program.

#include <iostream>
using namespace std;
// first name space
namespace first_space
{
void func()
{
 cout << "Inside first_space" << endl;
}
}

// second name space
namespace second_space
{
void func()
{
 cout << "Inside second_space" << endl;
}
}
using namespace first_space;
int main ()
{
// This calls function from first name space.
func();
return 0;
}
Output:
Inside first_space
Example: The using declaration

When we use a directive, we import all the names in the namespace and make them available throughout the program, implying that they have a global scope.

However, when we use declaration, we import one specific name at a time, which is only available within the current scope.

#include <iostream>
using namespace std;
// first name space
namespace first_space
{
void func()
{
 cout << "Inside first_space" << endl;
}
}

// second name space
namespace second_space
{
void func()
{
 cout << "Inside second_space" << endl;
}
}
using namespace first_space;
int main ()
{
 using namespace first_space;  // using directive
    using second_space::func; // using declaration
    func();    // calls func() of second_space namespace
    first_space::func(); // class func() of first_space namespace
return 0;
}
Output:
Inside second_space
Inside first_space

Note: also read about Multithreading in C++

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago