AppDividend
Latest Code Tutorials

Namespace in C++ Tutorial | C++ Namespace Example

0

Namespace in C++ Tutorial | C++ Namespace Example is today’s topic. First of all, we have to know what the Namespace is?. Why are they important? How can we use them? And many more. So, Namespace gives us the key to group named entities that are objects, classes, and functions under its name. Or we can say build to define the scope of the entity and prevents the chances of occurring name collision.

Namespace in C++

Namespace maps a series of names to their associated object. In this way, we can divide our Global scope into various scopes.  Two member functions and variables can have the same name with the help of namespace. A namespace is defined only at the global scope and can be declared in another namespace and no need of semicolon after the ending of the scope of the namespace.

A namespace is the feature added in C++ and not present in C. The namespace is the declarative region that provides a scope to the identifiers (names of the types, function, variables etc) inside it.

Multiple namespace blocks with a same name are allowed. All the declarations within those blocks are declared in the named scope.

#Syntax

namespace identifier
{
  Body // body contains functions,variable.
}

Namespace declarations appear only at global scope. Namespace declarations can be nested within the another namespace.

Namespace declarations don’t have to access the specifiers. (Public or private). No need to give the semicolon after the closing brace of the definition of a namespace. We can split a definition of namespace over several units.

See the following example.

namespace My_Namespace
{
  int i,j; // i,j are the normal variables defined in My_Namespace scope
}

If we want to access these entities inside their namespace, we usually can do it. But to access them from outside we have to use:

#Syntax

My_Namespace:: i;
My_Namespace:: 

:: is known as scope operator.

The primary and most exciting thing in the namespace is that multiple namespaces can have same name.No error is thrown.

This feature makes C++ better than C.

#Namespace Nickname

Alternate name can also be given to our namespace by using the following syntax.

#Syntax

namespace new_name = current_name;
using namespace std;

Now let’s clear the topic with the help of the program.

See the following example.

#include <iostream>
using namespace std;
namespace My_Namespace
{
    class AppDividend
    {

        public:
           void show()
           {
                cout <<"\nApp Dividend\n";
           }
    };
}

int main()
{
    My_Namespace::AppDividend app;
    app.show();
}

See the following example.

 

Namespace in C++ Tutorial

#Namespace std

Whole files in the C++ library declare all of its classes, objects, functions within the std namespace. That is why we generally include the ‘using namespace std;’ statement in our programs that uses any of the entity present in the iostream.

#include <iostream>
using namespace std;
namespace My_Namespace
{
        class App;
}
  class My_Namespace::App
{
    public:
        void show( )
        {
            cout<<"\nHello, Welcome to AppDividend\n";
        }

};
int main()
{
  My_Namespace:: App app;
  app.show( );
}

See the following output.

 

using Directive

In the above example, we have used ‘using namespace std’ at the start of the program and in this example, the special thing is that we have declared a class in the namespace but have defined it outside the scope of namespace.

So, it tells us that we can declare classes in the namespace but can define it outside the namespace also.

Similarly, we can also define methods outside of the namespace while declaring them in the namespace.

#Inline Namespace

Members of the inline namespace are treated as a member of parent namespace. It makes us declare some special features in our parent namespace. We can also use the inline namespace to see the changes to public interfaces. The namespace that has the most previous version is called inline.

The inline keyword must be used to declare that namespace.

#Unnamed namespace

When we don’t give the name to our namespace and wants to make a declaration of the variable, then unnamed namespace comes in the role. The code present can see identifiers in the unnamed namespace in that file, but from outside of that file it is invisible.

#Syntax

namespace
{
    int show( );
}

This is the unnamed namespace means namespace have no space.

Now the most important thing is using a directive. Let’s see its use.

#using Directive

It allows accessing the entities in the namespace to be used without using namespace-name.

Keyword using has been used to introduce the entity from a namespace into the current region.

See the following program.

#include <iostream>
using namespace std;
namespace app
{
  int a = 1;
  int b = 2;
}
namespace dividend
{
  double a = 1.011;
  double b = 2.3333;
}
int main ( ) {
  using app::a;
  using dividend::b;
  cout<<a<< endl;
  cout<<b<< endl;
}

See the output.

 

Namespace std

So we have accessed it by using keyword.

#Nested Namespaces

Namespaces can be nested where you can define a namespace inside another namespace. See the following syntax.

namespace namespace_name1 {
   // code declarations
   namespace namespace_name2 {
      // code declarations
   }
}

You can access members of the nested namespace by using resolution operators. See the following code.

// to access members of namespace_name2
using namespace namespace_name1::namespace_name2;

// to access members of namespace:name1
using namespace namespace_name1;

#Discontiguous Namespaces

A namespace can be defined in the several parts and so a namespace is made up of the sum of its separately defined parts.

The separate parts of the namespace can be spread over the multiple files.

So, if one part of the namespace requires the name defined in another file, that name must still be declared. Writing the following namespace definition either defines a new namespace or adds new elements to an existing one. See the following code.

namespace namespace_name {
   // code declarations
}

Finally, Namespace in C++ Tutorial | C++ Namespace Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.