1). Simple Example Program For Namespace In C++

Simple Namespace  Example Programs  In C++

Definition

A namespace (sometimes also called a name scope) is an abstract container or environment created to hold a logical grouping of unique identifiers or symbols (i.e., names). An identifier defined in a namespace is associated only with that namespace. The same identifier can be independently defined in multiple namespaces. That is, the meaning associated with an identifier defined in one namespace may or may not have the same meaning as the same identifier defined in another namespace. Languages that support namespaces specify the rules that determine to which namespace an identifier (not its definition) belongs.

The functionality of namespaces is especially useful in the case that there is a possibility that a global object or function uses the same identifier as another one, causing redefinition errors.

Syntax

Declaration:

namespace abc {
 int variable;
}

Adding Namespace:

using namespace abc;

Usage Namespace Member:

abc::variable

Example Program

/*  Example Program For namespace Example In C++
    little drops @ thiyagaraaj.com

    Coded By:THIYAGARAAJ MP             */

#include <iostream>
using namespace std;

//Namespace namespacefirst
namespace namespacefirst
{
  int value = 5;
}

//Namespace namespacesecond
namespace namespacesecond
{
  double value = 3.1416;
}

int main () {
  //Namespace namespacefirst Varibale Usage
  cout << "namespacefirst value : " <<namespacefirst::value << endl;

  //Namespace namespacesecond Varibale Usage
  cout << "namespacesecond value : "<<namespacesecond::value << endl;
  return 0;
}

Sample Output:

namespacefirst value : 5
namespacesecond value : 3.1416