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 &lt;iostream&gt;
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 &lt;iostream&gt;
using namespace std;
namespace myAwesomeNamespace {
int a=10, b;
}
namespace myAwesomerNamespace {
int a=20, b;
}
int main() {
cout&lt;&lt;"Awesome a = "&lt;&lt;myAwesomeNamespace::a;
cout&lt;&lt;"Awesomer a = "&lt;&lt;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 &lt;iostream&gt;
using namespace std;
namespace myAwesomeNamespace {
int a=10, b;
}
namespace myAwesomerNamespace {
int a=20, b;
}
int main() {
using namespace myAwesomeNamespace;
cout&lt;&lt;"Awesome a = "&lt;&lt;a;
cout&lt;&lt;"Awesomer a = "&lt;&lt;myAwesomerNamespace::a;
return 0;
}

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

Books you might like: