|< C++ Namespace 1 | Main | C++ Implementation In Java >| Site Index | Download |


 

 

 

 

 

 

MODULE 23a - C++ NAMESPACES 2

When the space becomes bigger and bigger

You have to define your own space!

 

 

 

 

My Training Period: xx hours

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

23.2.3  Explicit Element Access

  • This access method we use the namespace identifier together with the scope resolution operator (::) followed by the element name.

  • Using this method, we can qualify each member of a name space.  It also can resolve the ambiguity.

  • No matter which namespace (except anonymous/unnamed namespace) is being used in your subsystem or program, you can apply the scope operator, :: to access identifiers in any namespace (including a namespace already being used in the local scope) or the global namespace.

  • using directive cannot be used inside a class but the using declaration is allowed.

  • A program example:

// explicit access, namespace within a class

#include <iostream>

using namespace std;

 

class One

{

       public:

           void funct1(char chs)

           {cout<<"character = "<<chs<<endl;}

};

 

class Two:public One

{

       public:

           //The using directive is not allowed in class using namespace One;

           void funct1(char *str)

           {cout<<"String = "<<str<<endl;}      

           // using declaration is OK in class

           using One::funct1;  // overload Two::funct1()

};

 

int main()

{

   Two Sample;

   // calling One::funct1()

   Sample.funct1('P');

   // calling Two::funct1()

   Sample.funct1("This is string");

   return 0;

}

 

Output:

 

C++ Namespace explicit access

 

23.3  Namespace std

  1. std::cout – explicitly

  2. using std::cout – using declaration

  3. using namespace std – using directive

// namespace std example, notice the omitted .h header files

#include <iostream>

 

void main()

{

   std::cout<<"Demonstrating ";

   using namespace std;

   cout<<"the std namespace."<<endl;

}

 

Output:

 

C++ Namespace std

 

using namespace std;

 

23.4  Standard C++ Library

 

<algorithm>

<bitset>

<complex>

<deque>

<exception>

<fstream>

<functional>

<hash_map>

<hash_set>

<iomanip>

<ios>

<iosfwd>

<iostream>

<istream>

<iterator>

<limits>

<list>

<locale>

<map>

<memory>

<new>

<numeric>

<ostream>

<queue>

<set>

<sstream>

<stack>

<stdexcept>

<streambuf>

<string>

<strstream>

<utility>

<valarrary>

<vector>

 

 

 

Table 23.1:  C++ Standard header

 

<cassert>

<cctype>

<cerrno>

<cfloat>

<ciso646>

<climits>

<clocale>

<cmath>

<csetjmp>

<csignal>

<cstdarg>

<cstddef>

<cstdio>

<cstdlib>

<cstring>

<ctime>

 

 

 

Table 23.2:  C++ wrapper – Using C library in C++ codes.  Note that there is no .h extension

23.5  Using C++ Library Headers

#include <iostream>
#include <cstdlib>
  1. To assuredly declare an external name in namespace std that is traditionally declared in <stdlib.h>, for example, include the header <cstdlib>. Knowing that the name might also be declared in the global namespace.

  2. To assuredly declare in the global namespace an external name that is declared in <stdlib.h>, include the header <stdlib.h> directly. Knowing that the name might also be declared in namespace std.

  • Normally programmers use the using declaration:

using namespace std;
  • This brings all library names into the current namespace. If you write this declaration immediately after all the include preprocessor directives, you hoist the names into the global namespace.

  • You can subsequently ignore namespace considerations in the remainder of the translation unit. You also avoid most dialect differences across different translation environments.

  • Unless specifically indicated otherwise, you may not define names in the std namespace, or in a namespace nested within the std namespace.

  • The term translation unit refers to a source code file together with any included files, but less any source lines omitted by conditional preprocessor directives. Syntactically, a translation unit is defined as a sequence of external declarations:

 

external-declaration

translation-unit  external-declaration

 

external-declaration

 

function-definition

declaration

 

Some Working Program Examples and Experiments

 

/* a simple mathematics calculation */

// this program is from module 1, C program.

// header files used is the C++ wrapper, no .h anymore.

// the stdlib.h for system("pause") also has been removed

#include <cstdio>

 

// the main() function

int main( )

{

       // variables declaration and initialization

       int    x, y, z;

       x = 20;

       y = 2;

      

       printf("\nGiven x = 20, y = 2\n");

       printf("\nx / y = %d", x / y);

       x = x * y;

       y = y + y;

       printf("\nx = x * y");

       printf("\ny = y + y");

       printf("\nNew value for x / y = %d", x / y);

       z = 20 * y / x;

       printf("\nz = 20 * (y / x )= %d\n", z);

      

       return 0;

}

 

Output:

 

C++ Namespace C++ wrapper

 

// demonstrates the use of function prototypes C++ program, no .h anymore

#include <iostream>

// but has to explicitly use the 'using namespace std'

using namespace std;

 

// another method simplifying type identifier

typedef unsigned short USHORT;

// function prototype

USHORT FindTheArea(USHORT length, USHORT width); 

 

int main()

{

       USHORT lengthOfYard;

       USHORT widthOfYard;

       USHORT areaOfYard; 

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

       cin>> widthOfYard;

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

       cin>> lengthOfYard;

       areaOfYard = FindTheArea(lengthOfYard, widthOfYard);

       cout<< "\nYour yard is ";

       cout<< areaOfYard;

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

       return 0;

}

 

USHORT FindTheArea(USHORT l, USHORT w)

{

   return (l * w);

}

 

Output:

 

C++ Namespace wrappers

 

// demonstrates the use of function prototypes

// variation of the C++ program, no .h anymore

// without the 'using namespace std;'

#include <iostream>

 

// another method simplifying type identifier

typedef unsigned short USHORT;

// a function prototype

USHORT FindTheArea(USHORT length, USHORT width); 

 

int main()

{

       USHORT lengthOfYard;

       USHORT widthOfYard;

       USHORT areaOfYard; 

       // without using namespace std globally, you have to

       // explicitly use the std for every occurrences of the...

       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:

 

C++ Namespace

 

 

// ***************namespace.cpp**************/

// demonstrates the use of function prototypes

// variation of the C++ program, no .h anymore

// without the 'using namespace std;'

#include <iostream>

 

// another method simplifying type identifier

typedef unsigned short USHORT;

// a function prototype

USHORT FindTheArea(USHORT length, USHORT width); 

 

int main()

{

       USHORT lengthOfYard;

       USHORT widthOfYard;

       USHORT areaOfYard;

 

       // without using namespace std globally, you have to

       // explicitly use the std for every occurrences of the...

       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);

}

 

[bodo@bakawali ~]$ g++ namespace.cpp -o namespace

[bodo@bakawali ~]$ ./namespace

 

The wide of your yard(meter)? 200

 

The long of your yard(meter)? 10

 

Your yard is 2000 square meter

 

tenouk fundamental of C++ namespaces

 

 

 

 

 

 

 

 

 

 

 

 

 

Further C++ related reading:

 

  1. Source code for the program examples is available in C++ Namespace source codes.

  2. Check the best selling C / C++, STL and UML books at Amazon.com.

  3. A complete C & C++ Standard Libraries documentation.

  4. Standards:  The C/C++ standards references (ISO/IEC is covering ANSI and is more general):

  1. ISO/IEC 9899 (ISO/IEC 9899:1999) - C Programming languages.

  2. ISO/IEC 9945:2002 POSIX standard.

  3. ISO/IEC 14882:1998 on the programming language C++.

  4. ISO/IEC 9945:2003, The Single UNIX Specification, Version 3.

  5. Get the GNU C library information here.

  6. Read online the GNU C library here.

 

 

 

 

 

|< C++ Namespace 1 | Main | C++ Implementation In Java >| Site Index | Download |


C++ Namespace:  Part 1 | Part 2