In addition to functions, classes, and enumerations, C ++ provides namespaces as a mechanism for controlling and controlling the repeated names of functions and classes. For example, if you name some of your functions with names that intersect with the function names in the standard library, then you get a name conflict, that is, a function override from the standard library, which can lead to ambiguity and errors in the program. Thanks to the namespace, you can avoid this problem by simply placing functions in namespace .
namespace My_code { class complex { /* ... */ }; complex sqrt(complex); // ... int main(); } int My_code::main() { complex z {1,2}; auto z2 = sqrt(z); std::cout << '{' << z2.real() << ',' << z2.imag() << "}\n"; // ... }; int main() { return My_code::main(); }
In this case, the My_code space allows you to write a separate implementation of the main() function, which will be called in the main() function, so you need to call the main function, through the namespace My_code .
My_code::main();
Similarly, functions are called from the standard library, for example:
std::max(3, 5);
In order to make life easier and not to write std every time, you can use the using directive.
using namespace std;