Friday, June 22, 2012

c++:: Using namespace std; What does that mean?


Namespaces in C++ are used to define a scope and allows us to group global classes, functions and/or objects into one group.

When you use using namespace std; you are instructing C++ compiler to use the standard C++ library. If you don't give this instruction, then you will have to use std::, each time you use a standard C++ function/entity.

Have a look at the following example, which is very straight forward.

#include <iostream>

using namespace std;

class MyClass {
        public: void sayHello();
};

void MyClass::sayHello() {
     cout<<"Hello World!\n";
}

int main() {
       MyClass myClass;
       myClass.sayHello();
       return 0;
}

If you are not using namespace in the above example, you will have to use std:: everywhere you use cout, like the following.


#include <iostream>

class MyClass {
        public: void sayHello();
};

void MyClass::sayHello() {
        std::cout<<"Hello World!\n";
}

int main() {
       MyClass myClass;
       myClass.sayHello();
       return 0;
}

More about namespaces in C++

Namespaces allow you to group different entities together. You may create your own namespaces for grouping your entities, and access them the same way you did for 'std' namespace.


#include <iostream>

using namespace std;

namespace myAwesomeNamespace {
  int a=10, b;
}
namespace myAwesomerNamespace {
  int a=20, b;
}

int main() {
   cout<<"Awesome a = "<<myAwesomeNamespace::a;
   cout<<"Awesomer a = "<<myAwesomerNamespace::a;
   return 0;
}

You may also use using namespace with your custom namespces.


#include <iostream>

using namespace std;

namespace myAwesomeNamespace {
  int a=10, b;
}
namespace myAwesomerNamespace {
  int a=20, b;
}

int main() {
   using namespace myAwesomeNamespace;
   cout<<"Awesome a = "<<a;
   cout<<"Awesomer a = "<<myAwesomerNamespace::a;
   return 0;
}

Warning: If you use namepaces that have same entities grouped in it, then it will result in ambiguity error.

Related Posts Plugin for WordPress, Blogger...