The C++ namespace: The namespace without the 'using' directive C++ code 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: Printing some data to demonstrate different namespace without using the 'using' directive in C++ programming

To show: How to use the C++ namespace without the 'using' directive for different namespaces




// namespace without 'using' directive

#include <iostream>


namespace NewNsOne


// declare namespace NewNsOne variable

int p = 4;


// declare namespace NewNsOne function

int funct(int q);



namespace NewNsTwo


// declare namespace NewNsTwo variable

int r = 6;


// declare namespace NewNsTwo function

int funct1(int numb);


// declare nested namespace

namespace InNewNsTwo


// declare namespace InNewNsTwo variable

long tst = 20.9456;




int main(void)


// the following four lines of code will generate error because it is not at global scope


// namespace local

// {

// int k;

// }


std::cout<<"NewNsOne::p is "<<(NewNsOne::p)<<"\n";

std::cout<<"NewNsTwo::r is "<<(NewNsTwo::r)<<"\n";

std::cout<<"NewNsTwo::InNewNsTwo::tst is "<<(NewNsTwo::InNewNsTwo::tst)<<"\n";


return 0;



Output example:


NewNsOne::p is 4

NewNsTwo::r is 6

NewNsTwo::InNewNsTwo::tst is 20

Press any key to continue . . .



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