#include // Here we define three different namespaces, each containing // a function with the same identifier, 'f'. Namespaces allow // us to distinguish between these functions and call each // function separately. // // Any references to companies in this example refer // specifically to the Terminator movie series and not // to any existing company. // // com_cyberdyne // - A namespace for the company with the web address // www.cyberdyne.com. // - Using a reversed domain name is not required, but it is // one way to help ensure that a namespace identifier is // unique. // // com_cyberdyne::skynet // - A namespace for the Skynet project nested inside the // namespace com_cyberdyne. // // com_cyberdyne_skynet // - An alternative namespace identifier for the Skynet // project that does not use a nested namespace. // // Thus, the fully qualified names of the three functions are // // com_cyberdyne::f // com_cyberdyne::skynet::f // com_cyberdyne_skynet::f // // Exercises: // 1. Create a namespace that is associated with you personally. // You could use your uWaterloo User ID, which is guaranteed // to be unique within the University of Waterloo. For example, // if your User ID is 'uwuserid', you could use either // // ca_uwaterloo::uwuserid // // or // // ca_uwaterloo_uwuserid // // as a namespace. // Function declaration int main(); namespace com_cyberdyne { void f(); namespace skynet { void f(); } } namespace com_cyberdyne_skynet { void f(); } // Function definitions int main() { com_cyberdyne::f(); com_cyberdyne::skynet::f(); com_cyberdyne_skynet::f(); return 0; } namespace com_cyberdyne { void f() { std::cout << "In com_cyberdyne" << std::endl; } namespace skynet { void f() { std::cout << "In com_cyberdyne::skynet" << std::endl; } } } namespace com_cyberdyne_skynet { void f() { std::cout << "In com_cyberdyne_skynet" << std::endl; } }