using Namespace directive In C++

The using namespace directive can also be used to avoid namespace prepending. This directive instructs the compiler to utilize names from the specified namespace in subsequent code. As a result, the namespace for the following code is presumed.

Now, Let’s see an example:

#include <iostream>
using namespace std;

namespace nameSpace_1
{
    void func()
    {
        cout << "nameSpace_1 is Call." << endl;
    }
}

namespace nameSpace_2
{
    void func()
    {
        cout << "nameSpace_2 is Call." << endl;
    }
}

using namespace nameSpace_2;

int main()
{
    func();

    return 0;
}

if you compile and run the above code, this would produce the following result −

Output:

nameSpace_2 is Call.

The ‘using’ directive can also be used to refer to a specific namespace object. For example, if you only want to utilise cout from the std namespace, you can refer to it as follows:

using std::cout;

Following code can refer to cout without specifying the namespace, but other things in the std namespace must be specified explicitly, as seen below.

#include <iostream>
using std::cout;

int main () {
   cout << "std::endl is used with std!" << std::endl;
   
   return 0;
}

If we compile and run the code above, we get the following result:

std::endl is used with std!

Join Our Community

Join our WhatsApp Group To know more about Programming Language tips, tricks and knowledge about and how to start learning any programming language.

Here we specify the stream ostream using std. So, when we use this inside the code only count works but when using cin not work because istream does not specify here.

Now, see the below code:

#include <iostream>
using std::cout;

int main()
{
    int a;
    cin >> a;
    cout << a;
    return 0;
}

Here cin is not declared inside std and shows the following error:

using_stand_namespace.cpp:8:5: error: 'cin' was not declared in this scope
     cin >> a;
     ^~~
using_stand_namespace.cpp:8:5: note: suggested alternative:
In file included from using_stand_namespace.cpp:1:0:
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\iostream:60:18: note:   'std::cin'
   extern istream cin;  /// Linked to standard input

When I am going to use std::cin then the error removes to see the below code.

#include <iostream>
using std::cout;

int main()
{
    int a;
    std::cin >> a;
    cout << a;
    return 0;
}

Output:

std::endl is used with std!" << std::endl;
22

Let’s see another example:

#include <iostream>
using namespace std;

namespace area_rect_using_int
{
    void area_func(int l, int b)
    {
        cout << "Area of recctangle: " << l * b << endl;
    }
}

namespace area_rect_using_float
{
    void area_func(float l, float b)
    {
        cout << "Area of rectangle: " << l * b << endl;
    }
}

using namespace area_rect_using_float;
int main()
{
    area_func(2.0, 3.1);

    return 0;
}

Output:

Area of recctangle: 6.1

Related Articles:

What’s namespace?

nested namespace in C++

Default image
Lingaraj Senapati
Hey There! I am Lingaraj Senapati, the Co-founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.
Articles: 228

Newsletter Updates

Enter your email address below to subscribe to our newsletter