1. Posts/

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

···
Programming C/C++

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <iostream&gt;
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.