The C++ namespace: C++ program example without the 'using namespace std;'

 

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: A simple C++ program example which does not using the C++ standard namespace, using fully qualified namespace instead

To show: How to use the fully qualified namespace instead of the standard namespace in C++ programming

 

 

// demonstrates the use of function prototypes without the 'using namespace std;' but fully qualified standard namespace

#include <iostream>

 

// another method simplifying type identifier

typedef unsigned short USHORT;

 

// function prototype

USHORT FindTheArea(USHORT length, USHORT width);

 

int main(void)

{

USHORT lengthOfYard;

USHORT widthOfYard;

USHORT areaOfYard;

 

// without using namespace std globally, you have to explicitly use the std for every occurrences of the standard C++

// class namespace. Also apply to other non-standard namespace which need to use fully qualified namespace

std::cout<< "\nThe wide of your yard(meter)? ";

std::cin>> widthOfYard;

std::cout<< "\nThe long of your yard(meter)? ";

std::cin>> lengthOfYard;

areaOfYard = FindTheArea(lengthOfYard, widthOfYard);

std::cout<< "\nYour yard is ";

std::cout<< areaOfYard;

std::cout<< " square meter\n\n";

 

return 0;

}

 

USHORT FindTheArea(USHORT l, USHORT w)

{

return (l * w);

}

 

Output example:

 

The wide of your yard(meter)? 20

The long of your yard(meter)? 20

Your yard is 400 square meter

Press any key to continue . . .

 

 

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