The C++ namespace: the namespace extension, extending the original namespace 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: Displaying some data to demonstrate how to extend the existing namespace in C++ programming

To show: How to extend the namespace in C++ programming

 

// C++ namespace extension

#include <iostream>

using namespace std;

 

// original namespace

namespace One

{

// declare namespace One variables

int p;

int q;

}

 

namespace Two

{

// declare namespace Two variables

float r;

int s;

}

 

// namespace extension of One

namespace One

{

// declare namespace One function

// void funct1(void);

int funct2(int p);

 

int funct2(int p)

{

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

return 0;

}

}

 

int main(void)

{

One::funct2(200);

 

return 0;

}

 

Output example:

 

p = 200

Press any key to continue . . .

 

 

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