c using namespace std


Namespaces allow to group entities like classes, variables and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name. Some cases where absence of namespaces leads to error  -:

--> if your program defined a function called abs( ), it could override the standard library function
abs( ) because both names would be stored in the global namespace ( global namespace is your complete program ).

--> if your program defines a class call ThreeDCircle and a library used by your program defines a class by the same name, a conflict will arise.

The creation of the namespace keyword was a response to these problems. Because it localizes the visibility of names declared within it, a namespace allows the same name to be used in different contexts without conflicts arising. This means that we can use variables, classes, functions of same names in our program

Syntax

namespace name
{
  // declarations
}

A simple example for namespace is as :

namespace sample
{
  int a, b;
}

Simple program to use it is:

#include <iostream>
using namespace std;    // This line is completely explained at the end of this post
namespace sample
{
  int a, b;
}
int main ()                                                                                                                                                          
{
  sample::a=3;    //See here we have to use a scope operator to access the variables
  sample::b=5;
  cout <<sample::a<< endl;
  cout <<sample::b<< endl;
  return 0;
}

Functionality of namespaces is especially useful in the case that there is a possibility that a global object/variable or function uses the same identifier as another one, causing redefinition errors. This problem is resolved in the code below where the variable var is defined twise in th same global namespace :

#include<conio.h>
#include <iostream>
using namespace std;
namespace samplefirst
{
  int var = 5;
}
namespace samplesecond
{
  double var = 3.1416;
}
int main ()
{
  cout << samplefirst::var << endl;
  cout << samplesecond::var << endl;
  getch();
  return 0;
}

In this case, there are two global variables with the same name: var but now they are not same as they are defined in different namespaces and no ERRORS.

using :

In above example we were using the name of namespace and the scope operator each time we want to use its attributes. The using statement was invented to alleviate this problem. The using statement has these two general forms:

using namespace name;   // the complete namespace is brought into view in the current                
                                               namespace/block

using name::member;     //only a single member of the namespace is introduced in the current namespace/block

Above example with using keyword
#include <iostream>
using namespace std;
namespace sample
{
  int a, b;
}
int main ()
{
  using namespace sample;
  a=3;   
  b=5;
  cout <<a<< endl;
  cout <<b<< endl;
  return 0;
}
Now comes the most awaited topic for which this post has been created :

The std Namespace

Standard C++ defines its entire library in its own namespace called std. This is the reason that most of the programs we see in many books and on internet include the following statement:
using namespace std;

The above statement causes the std namespace to be brought into the current namespace, which gives
you direct access to the names of the functions and classes defined within the library/std namespace

You might see some statements in many books as
std::cout
std::cin

this is nothing but using attributes or functions in the std namespace explicitly 

Here is a simple example in 2 different ways

#include <iostream>
int main()
{
  int val;
  std::cout << "Enter a number: ";
  std::cin >> val;
  std::cout << "This is your number: ";
  std::cout << std::hex << val;
  return 0;
}

And the other one is :

#include <iostream>
using namespace std;
int main()
{
  int val;
  cout << "Enter a number: ";
  cin >> val;
  cout << "This is your number: ";
  cout << hex << val;       //hex is used to print the hexadecimal value of integer
  return 0;
}


About the author

Admin
Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus. Aenean fermentum, eget tincidunt.

2 comments:

  1. Nice Post!!
    1. If you were given a chance to provide a standard definition for "namespace", then what it would be? like we have for class as:
    collection of related data members & member functions...

    2. In the above example where you've shown that both 'var' variables have no redefinition error, but then it seems like namespace is acting like a structure or class and you can access them through accessor methods or constructor or declare them in public section.I'm not able to actually figure out what entity this namespace is ???

    PS:
    BTW Nice Post!

    ReplyDelete
    Replies
    1. Thanxx for your comment n i will surely remove your doubt about namespaces.

      Delete

Template by Clairvo Yance
Copyright © 2012 Enigma of IT and Blogger Themes.