The C++ namespace: the namespace with 'using' directive C++ program example


Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Displaying some data to demonstrate how to use the 'using' directive for namespace in C++ programming

To show: How to use the namespace with the 'using' directive in C++ programming




// namespace with 'using' directive.

#include <iostream>

using namespace std;


namespace SampleOne


double p = 10.34;



namespace SampleTwo


using namespace SampleOne;

double q = 77.12;


namespace InSampleTwo


double r = 34.725;




int main(void)


// this directive gives you everything declared in SampleTwo

using namespace SampleTwo;

// this directive gives you only InSampleTwo

using namespace SampleTwo::InSampleTwo;

// local declaration, take precedence

double p = 23.11;


cout<<"p = "<<p<<endl;

cout<<"q = "<<q<<endl;

cout<<"r = "<<r<<endl;


return 0;



Output example:


p = 23.11

q = 77.12

r = 34.725

Press any key to continue . . .



C and C++ Programming Resources | C & C++ Code Example Index