// MyHeader.h struct Node { int key; char data[32]; }; // YourHeader.h struct Node { char nodeKey; int iData; double dData; }; // MyProgram.cpp #include "MyHeader.h" #include "YourHeader.h" void main() { Node n; }
// MyHeader.h namespace MyNameSpace { struct Node { int key; char data[32]; }; } // YourHeader.h namespace YourNamespace { struct Node { char nodeKey[5]; int iData; double dData; } } // MyProgram.cpp #include MyHeader.h #include YourHeader.h void main() { MyNameSpace::Node n1; YourNameSpace::Node n2; n1.key = 32; strcpy(n2.nodeKey, "ABCD"); }
Implementation 1 Part of main source file |
Implementation 2 Part of main source file in two places |
Implementation 3 IN separate header and source files |
namespace MyNS { // Function prototypes void function1(); void function2(); void function3(); // Function 1 implementation void function1() { cout << "MyNS::function1\n"; } // Function 2 implementation void function1() { cout << "MyNS::function2\n"; } // Function 3 implementation void function1() { cout << "MyNS::function3\n"; } } // end namespace MyNS void main() { MyNS::function1(); MyNS::function2(); MyNS::function3(); } |
namespace MyNS { // Function prototypes void function1(); void function2(); void function3(); } void main() { MyNS::function1(); MyNS::function2(); MyNS::function3(); } namespace MyNS { // Function 1 implementation void function1() { cout << "MyNS::function1\n"; } // Function 2 implementation void function1() { cout << "MyNS::function2\n"; } // Function 3 implementation void function1() { cout << "MyNS::function3\n"; } } // end namespace MyNS |
// MyNS.h #ifndef MYNS_H #define MYNS_H namespace MyNS { // Function prototypes void function1(); void function2(); void function3(); } #endif // MyNS.cpp namespace MyNS { // Function 1 implementation void function1() { cout << "MyNS::function1\n"; } // Function 2 implementation void function1() { cout << "MyNS::function2\n"; } // Function 3 implementation void function1() { cout << "MyNS::function3\n"; } } // end namespace MyNS |
namespace MyNS { int x = 1; // Declare variable x in namespace MyNS } int x = 0; // Declare variable x in global namespace void main() { using MyNS::x; // Reference x variable from namespace MyNS cout << x; // Value 1 will be printed not 0 cout << ::x; // Explicitly reference global name space to print 0 }
// File: AccessDemo.cpp #include <iostream.h> // Declare namespace 1 with function1() namespace NS1 { void function1() // Function implementation { cout << “NS1::function1() \n”; } } // end NS1 // Declare namespace 2 with function1() namespace NS2 { void function1() // Function implementation { cout << “NS2::function1() \n”; } } // end NS1 // Declare function1() in global namespace void function1() // Function implementation { cout << “NS2::function1() \n”; } //------------------------------------------------------- // main() //------------------------------------------------------- void main() { // Explicit access demonstration cout << “Explicit access\n”; ::function1(); // Call function in global namespace NS1::function1(); // Call function in namespace NS1 NS2::function1(); // Call function in namespace NS2 // using declarationaccess cout << “using declaration access:\n”; using NS1::function1; // Declare use of function1() from NS1 function1(); // Call function in namespace NS1 { // Create a code block with an open brace using NS2::function1; // Declare use of function1() from NS2 function1(); // Call function in namespace NS2 // This use of using only applies within this code block } // End code block with a closing brace function1(); // Now we are outside the above code block // so the using NS2 is out of scope and the // using NS1 is in scope again so call function in namespace NS1 }
// File: AccessDemo2.cpp #includeThe Standard C++ namespace// Declare namespace 1 with function1() namespace NS1 { void function1() // Function implementation { cout << “NS1::function1() \n”; } } // end NS1 // Declare namespace 2 with function1() namespace NS2 { void function1() // Function implementation { cout << “NS2::function1() \n”; } } // end NS1 // Declare function1() in global namespace void function1() // Function implementation { cout << “NS2::function1() \n”; } //------------------------------------------------------- // main() //------------------------------------------------------- void main() { // Using directive giving default access to all of NS2 using namespace NS2; NS1::function1(); // Explicit call to function1() in namespace NS1 ::function1; // Explicit call to function1() in global namespace function1(); // Call function1() in namespace NS2 }