Broad Network


Namespace scope in C++

Scopes in C++ - Part 4

Forward: In this part of the series, I explain namespace scope in C++.

By: Chrysanthus Date Published: 25 Aug 2012

Introduction

This is part 4 of my series, Scopes in C++. In this part of the series, I explain namespace scope in C++. As I said in the previous part of the series, scoping in C++ is focused on the C++ block, which consists of statements within curly brackets.

A Namespace
A namespace is a block with identifier names. The aim of a namespace is to prevent ambiguity of names in a C++ program coded by a programmer. A namespace does not have identifiers standing alone. The identifiers are in the form of declarations. In theory, a namespace would take any definition or declaration. It can take the definition of a whole function. The point of interest is the names of the identifiers of the objects, classes, functions and structures.

In the example namespace below, I have included the complete definition of a function, for the purpose of illustration. In practice, you should have function prototypes in the place of function definitions, in a namespace. In practice, a namespace should have just simple short definitions and declarations. In practice, avoid description of classes and functions. If the description of a class is not important, then, in a line, just type the reserved word, class, followed by the name you want for the class, and then a semicolon. For functions and member functions, type only the prototypes. In the example below, I have not followed this advice because I have certain things to point out. In practice, the description of a class or function should go into a different file or into a library, while the namespace goes into a header (file).

Example
The following program has a namespace called, myName:

#include <iostream>
using namespace std;


namespace myName
    {

        int identi1;
        float identi2;


        class Calculator
            {
                public:
                int num1;
                int num2;

                Calculator(int ident1, int ident2)
                    {
                        num1 = ident1;
                        num2 = ident2;
                    }

                int add ();
            };


        void fntn(int id1, float id2)
            {
                int ide1 = id1;
                float ide2 = id2;
                int ide3 = identi1;
                cout << ide1 << '\n';
            }

    }


int main()
    {
        myName::identi1 = 3;
        cout << myName::identi1 << '\n';
        myName::identi2 = 2.5;
        cout << myName::identi2 << '\n';

        myName::fntn(8,9.5);


        return 0;
    }

The namespace defines an int object and a float object. The namespace goes on to give a partial description of a class and then a full definition (description) of a function. Inside the class description, you have two properties, a constructor function and a member function (method) prototype.

Scoping in Namespace
Remember, a block inside a block is a nested block. A nested block can have its own nested block. In this article, I refer to nested blocks as levels. The content directly in the outermost block are referred to, here, as first level content. Second level and third level contents are similarly described (referred to), in this article. Scoping in a namespace is the same as in a class. That is, an identifier in the first level of a namespace can be seen in all the inner (nested) levels of the namespace. An identifier in the second level can be seen in the third level; third level in fourth level, and so on. It does not work the other way round.

Now, the inner levels or inner blocks do not occur arbitrarily. In the above code, the class, forms an inner level to the namespace (as a whole). The constructor function forms an inner level to the class. As you can see, the inner blocks are not formed arbitrarily.

An identifier of an inner block can be seen in its own inner block and in nested blocks; not the other way round.

Accessing Namespace Identifiers
You can access the namespace identifiers (objects) using the scope resolution operator (::). Normally, you will access only the first level identifiers.

The main function in the above code shows how the first level identifiers are accessed. Note in particular, the accessing (calling) of the function, fntn() from the namespace, in main. Also note the use of the scope resolution operator.

To use an identifier without the preceding, namespace name and scope resolution operator, you have to precede the statement with the following directive:

    using namespace namespaceName;

So to access the ident1 identifier in the above namespace without using, “myName::” you would type:

    using namespace myName;
    identi1 = 3;

There can be statements between the using directive and the identi1 assignment.

File Scope
C++ does not really have the vocabulary, File Scope. However, any identifier that is not declared in a block, can be considered as having the file scope. It would be seen in all the blocks and nested blocks.

Global Scope
The include directives include files to the file of interest. At the end of the day, all the included files and the file of interest form a resulting long file. So, the identifiers of file scope in each of the file in the resulting long file, can be considered as identifiers of global scope. Note that C++ has a limit to the length of the resulting long file.

By now, you know that a typical C++ program is made up of separate files. The question you may ask now is that, how are the files connected (related). The files are connected by the included files, but if you do that bluntly, you will quickly reach the limit of the length of the resulting file. Your included files should actually be headers (short files), which consist of namespaces having short declarations. The main blocks or descriptions (definitions) of the declarations, should be placed in the separate files or in libraries. In that way, you will not suffer from the limited length of the resulting file.

End of Series
That is it for namespace scope; that is it for Scopes in C++. I hope you appreciated the series. There is still more to learn. You will get that in the next series.

I assume you have completed this series. It means you already had the professional skills in C++. Now you are in an advanced course of C++. You may already be working. If you ever have too much work, you can subcontract (outsource) some or all to someone in the site below.

See you later.

Chrys

Programming Solutions

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem

Comments

Become the Writer's Fan
Send the Writer a Message