The C++ namespace: the 'using declaration' 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: Using the 'using' declaration in order to provide more control on the namespaces in C++ programming

To show: How to use the 'using declaration' in C++ programming to provide more control on the use of namespaces in C++ programming

 

// using declaration, funct2() function defined in two different namespaces

#include <iostream>

using namespace std;

 

namespace One

{

double funct1(double q)

{

return q;

}

 

// first funct2() definition

void funct2()

{

cout<<"funct2() function, One namespace..."<<endl;

}

}

 

namespace Two

{

// second funct2() definition

void funct2()

{

cout<<"funct2() function, Two namespace..."<<endl;

}

}

 

int main(void)

{

// the 'using' declaration identifies the desired version of funct2()

using One::funct1; // becomes qualified identifier

using Two::funct2; // becomes qualified identifier

 

double p = 4.556; // local declaration, takes precedence

 

cout<<"First p value, local = "<<p<<endl;

p = funct1(3.422);

cout<<"Second p value, by function call = "<<p<<endl;

funct2();

 

return 0;

}

 

Output example:

 

First p value, local = 4.556

Second p value, by function call = 3.422

funct2() function, Two namespace...

Press any key to continue . . .

 

 

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