|< C/C++ Typecasting 2 | Main | C++ Namespace 2 >| Site Index | Download |


 

 

 

 

 

MODULE 23 - C++ NAMESPACES TUTORIAL 1

When the space becomes bigger and bigger

You have to define your own space!

 

 

 

 

 

My Training Period: xx hours

 

Note: Tested using Visual C++ 6.0 and VC++ .Net, Win32 empty console mode application.  g++ (GNU C++) example is given at the end of this Module. Source code for the program examples is available in C++ Namespace source codes.

 

The C++ programming abilities that should be acquired:

 

 

 

 

 

 

 

 

 

 

 

 

 

23.1    The Namespace

  • Real applications or programs consist of many source files. These files can be authored and maintained by more than one developer or programmer. Eventually, the separate files are organized and linked to produce the final application.

  • Traditionally, the file organization requires that all names that aren't encapsulated within a defined namespace (such as in a function or class body, or translation unit) must share the same global namespace. Hence, multiple definitions of names or name clashes will be encountered while linking the separate modules.

  • Namespace mechanism in C++ overcomes the problem of name clashes in the global scope.  The namespace mechanism allows an application to be partitioned into number of subsystems. Each subsystem can define and operate within its own scope.

  • The namespaces declaration identifies and assigned a unique name to a user declared namespace.  This will be used to solve the name collision or conflict in large program and libraries development where there are many programmers or developer working for different program portions.

  • To use C++ namespaces, there are two steps involved:

  1. To uniquely identify a namespace with the keyword namespace.

  2. To access the elements of an identified namespace by applying the using keyword.

  •  General form of namespace is:

namespace indentifier

{ namespace body }

  • For example:

namespace  NewOne

{

      int p;

      long q

}

  • p and q are normal variables but integrated within the NewOne namespace.  In order to access this variables from the outside the namespace, we have to use the scope operator ::.  From previous example:

NewOne::p;

NewOne::q;

  • A namespace definition can be nested within another namespace definition.  Every namespace definition must appear either at file scope or immediately within another namespace definition.  For example:

// a namespace with using directive

#include <iostream>

using namespace std;

 

namespace SampleOne

{

       float p = 10.34;

}

 

namespace SampleTwo

{

       using namespace SampleOne;

       float q = 77.12;

       namespace InSampleTwo

       {

              float r = 34.725;

       }

}

 

int main()

{

       // this directive gives you everything declared in SampleTwo

       using namespace SampleTwo;

       // this directive gives you only InSampleTwo

       using namespace SampleTwo::InSampleTwo;

       // local declaration, take precedence

       float p = 23.11;

 

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

       cout<<"q = "<<q<<endl;

       cout<<"r = "<<r<<endl;

       return 0;

}

 

Output:

 

C++ Namespace using directive

// a namespace without using directive

#include <iostream>

using namespace std;

 

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

{

      // the following four lines of code will generate error

      // because it is not at global scope...

      // namespace local

      // {

      //       int k;

      // }

 

      cout<<"NewNsOne::p is "<<(NewNsOne::p)<<endl;

      cout<<"NewNsTwo::r is "<<(NewNsTwo::r)<<endl;

      cout<<"NewNsTwo::InNewNsTwo::tst is"<<(NewNsTwo::InNewNsTwo::tst)<<endl;

      return 0;

}

 

Output:

 

C++ Namespace without using directive

 

23.1.1  Namespace Alias

// namespace alias

#include <iostream>

using namespace std;

 

namespace TheFirstLongNamespaceSample

{

     float p = 23.44;

     namespace TheNestedFirstLongNamespaceSample

     {    int q = 100;    }

}

 

// an alias namespace

namespace First = TheFirstLongNamespaceSample;

// use access qualifier to alias a nested namespace

namespace Second = TheFirstLongNamespaceSample::TheNestedFirstLongNamespaceSample;

 

int main()

{

       using namespace First;

       using namespace Second;

 

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

       cout<<"q = "<<q<<endl;

       return 0;

}

 

Output:

 

C++ Namespace alias

 

23.1.2  Namespace Extension

  • The definition of a namespace can be split over several parts of a single translation unit.

  • If you have declared a namespace, you can extend the original namespace by adding new declarations.

  • Any extensions that are made to a namespace after a using declaration will not be known at the point at which the using declaration occurs.

  • For example:

 

// a namespace extension

// cannot be compiled, no output, just sample code

// original namespace

namespace One

{

       // declare namespace One variable

       int p;

       int q;

}

 

namespace Two

{

       float r;

       int s;

}

 

// namespace extension of the One

namespace One

{

       // declare namespace One function

       void funct1(void);

       int funct2(int p);

}

 

int main()

{       

    return 0;

}

            // no output

// namespace extension

#include <iostream>

using namespace std;

 

struct SampleOne

{    };

struct SampleTwo

{    };

 

// original namespace

namespace NsOne

{

       // original function...

       void FunctOne(struct SampleOne)

       {    cout<<"Processing the struct argument..."<<endl;    }

}

 

using NsOne::FunctOne;  // using-declaration...

 

// extending the NsOne namespace

namespace NsOne

{

       // overloaded function...

       void FunctOne(SampleTwo&)

       {    cout<<"Processing the function argument..."<<endl;    }

}

 

int main()

{

      SampleOne TestStruct;

      SampleTwo TestClass;

 

      FunctOne(TestStruct);

      // the following function call fails because there are

      // no overloaded version for this one

      // FunctOne(TestClass);

       return 0;

}

 

Output:

 

C++ Namespace extension

 

23.1.3  Unnamed/anonymous Namespace

namespace { namespace_body }

namespace unique { namespace_body}

using namespace unique;

// anonymous or unnamed namespace

#include <iostream>

using namespace std;

 

// anonymous namespace

namespace

{

    int p = 1;  // unique::p

}

 

void funct1()

{

       ++p;  // unique::++p

}

 

namespace One

{

       // nested anonymous namespace

       namespace

       {

              int p;         // One::unique::p

              int q = 3;  // One::unique::q

       }

}

 

// using-declaration

using namespace One; 

 

void testing()

{

       // ++p;               // error, unique::p or One::unique::p?

       // One::++p;      // error, One::p is undefined

       cout<<"++q = "<<++q<<endl;

}

 

int main()

{

       testing();

       return 0;

}

 

Output:

 

C++ Namespace anonymous un-named

 

23.2  Accessing Namespace Elements

  1. Using explicit access qualification

  2. Using directive

  3. Using declaration

23.2.1  using Directive

// using directive

#include <iostream>

using namespace std;

 

namespace One

{    float p = 3.1234;    }

 

namespace Two

{

       using namespace One;

       float q = 4.5678;

       namespace InTwo

       {    float r = 5.1234;    }

}

 

int main()

{

       // this using directive gives you all declared in Two

       using namespace Two;

       // this using directive gives you only the InTwo namespace

       using namespace Two::InTwo;

       // this is local declaration, it takes precedence

       // comment this code and re run this program, see the different...

       float p = 6.12345;

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

       cout<<"q = "<<q<<endl;

       cout<<"r = "<<r<<endl;

       return 0;

}

 

Output:

 

C++ Namespace using directive access element

 

23.2.2  using Declaration

using::unqualified-identifier;

// using declaration

// function funct2() defined in two different namespaces

#include <iostream>

using namespace std;

 

namespace One

{

       float funct1(float 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()

{

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

       using One::funct1;      // becomes qualified identifier

       using Two::funct2;      // becomes qualified identifier

       float 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:

 

C++ Namespace using declaration

 

tenouk fundamental of C++ object oriented tutorial

 

 

 

 

 

 

 

 

 

 

 

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/C++ Typecasting 2 | Main | C++ Namespace 2 >| Site Index | Download |


C++ Namespace:  Part 1 | Part 2