#Concept #Study #CPP
Namespaces are used to prevent naming conflicts, e.g. if 2 modules use functions that are named the same but have different purposes, C++ will not know which function it should use.
A Namespace is resoluted with the scope resolution operator `::`
Example, using cout function of Namespace `std` without using the namespace, namespace needs to be defined before each call of `cout`:
```c++
#include <iostream>
int main(){
std::cout << "Hello world!";
std::cout << "My name is Karl";
return 0;
}
```
## Using Namespaces
Example of using the namespace `std`, we no longer need resolute `std` each time:
```c++
#include <iostream>
using namespace std; // using the std namespace
int main(){
cout << "Hello world!";
cout << "My name is Karl";
return 0;
}
```
### using single names from a Namespace
Alternatively, to no use the complete namespace in our code, to reduce footprint and prevent other conflicts in larger code, we can choose to use the single names of a namespace:
```c++
#include <iostream>
using std::cout; // using a single name from std namespace
int main(){
cout << "Hello world!";
cout << "My name is Karl, tell me your name";
std::cin >> name;
return 0;
}
```
## 🔗Resources