Namespace-in-C++ .

In this session i have discussed namespace in C++ .


The namespace is a group of entities like class , function , variable , object etc . You can call the entities using :: scope resolution operator .

It is allow to same name in multiple namespace . That means remove the problem of naming collision .


Namespace syntax

    namespace namespace_name
    {
        int a, b;
    }
    

Example : Write a program that demonstrate the use of namepace in C++ .

    #include<iostream>
    using namespace std; 
    namespace abc
    {
        int x=20;
    }
    int main()
    {
        cout<<abc::x;

       return 0;
    }
    OUTPUT : 20
    

Example : Write a program that demonstrate the use of namepace in C++ .

    #include<iostream>
    using namespace std; 
    namespace abc
    {
        class A
        {
          public :
             void show()
             {
                 cout<<"\n This show()";
             }

        };
    }
    namespace xyz
    {
        class A
        {
            public :
                void disp()
                {
                    cout<<"\n This is disp()";
                }

        };
    }
    int main()
    {
        abc::A obj;
        obj.show();
        xyz::A obj1;
        obj1.disp();

       return 0;
    }
    OUTPUT : This is show()
             This is disp()
    

Welcome to AndroidATPoint.

back image                     next image