⚡C++IntermediateLesson 32 of 50CamboFreelanceJune 19, 2026
C++ Namespaces
Learn C++ namespaces to organize code and prevent name collisions. Create, nest, and use namespaces with the using directive.
Intermediate6 min readLesson 32 of 50
What is a Namespace?
A namespace groups related identifiers to prevent name collisions. This is how std::cout is organized — everything in the C++ standard library lives in the std namespace.
Best Practice: Avoid using namespace std; in header files — it pollutes the global namespace of every file that includes the header. It is fine in .cpp files.
Exercise
Create two namespaces: Imperial (with milesPerHour unit) and Metric (with kmPerHour unit). Write a conversion function in each that converts to the other. Test both.