// namespace-main-15-16.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program illustrates the "using declarations" for namespaces and program scopes. // It is possible to make the member of a namespace visible so that it can be // referred to outside the namespace directly with its name (no need // for namespace_name:: before the variable). That is possible if the member // is declared with a using declarations. // // As with any other declaration, a name introduced by a using declaration has // these characteristics: // 1) It must be unique in its scope // 2) It hides the same name introduced by a declaration in an enclosing scope // 3) It is hidden by a declaration of the same name in a nested scope // Key points: 1) Using Declaration & scope #include "namespace16.h" void fun1() { cout << "Starting call to fun1()" << endl; using region4::dup; using region3::Greeting; Greeting(); region3::Calc1 c1(200); dup.setValue(c1); cout << "The value of object \"dup\" has been set to : " << dup.getValue() << endl; cout << "Ending call to fun1()" << endl; } int main() { // The following using declarations are introduced inside the main() function; as a // result they are only in effect inside this function. using region3::Calc1; using region3::dup; using region3::region31::message1; using region3::region31::region32::message2; using region3::region31::region32::region33::message3; using region3::Greeting; using region4::Calc2; // The following can NOT be done since it will cause multiple declaration // in the same scope. Un-comment-out the following line and compile for details. // using region4::dup; Greeting(); Calc1 c1(30), c11(45);; c1.setValue(32); Calc2 c2; c2.setValue(c1); cout << "The value of object \"c2\" has been set to : " << c2.getValue() << endl; // use the global object - set it to the same value as c1 region4::dup.setValue(c1); cout << "The value of object \"dup\" has been set to : " << region4::dup.getValue() << endl; region3::dup(c1, c11); cout << "The value of object \"c1\" has been set to : " << c1.getValue() << endl; cout << "\n\n==============================================\n"; message1(); message2(); message3(); cout << "==============================================\n"; //start a scope { cout << "Inside Main() / Scope" << endl; using region4::dup; Greeting(); Calc1 c1(5000); dup.setValue(c1); cout << "The value of object \"dup\" has been set to : " << dup.getValue() << endl; cout << "Leaving Main() / Scope" << endl; } fun1(); return 0; } // The result of running the program /* Hello from region 3. The value of object "c2" has been set to : 32 The value of object "dup" has been set to : 32 The value of object "c1" has been set to : 45 ============================================== Hello...there...Is that really much better? ============================================== Inside Main() / Scope Hello from region 3. The value of object "dup" has been set to : 5000 Leaving Main() / Scope Starting call to fun1() Hello from region 3. The value of object "dup" has been set to : 200 Ending call to fun1() */