Header and Namespace

HEADER AND NAMESPACE

Look at this short code  first , in order to know what are Headers and why Namespace is used..


include <iostream>

using namespace std;

int main()

{

cout << “Sample Program\n”;

return 0;

}


The two lines that begins the program are directives. The first is a preprocessor directive, and the second is a using directive.

They occupy a sort of gray area: They’re not part of the basic C++ language, but they’re necessary anyway.

PREPROCESSOR DIRECTIVES

The first line of the FIRST program

#include <iostream.h>

Might look like a program statement, but it’s not. It isn’t part of a function body and doesn’t end with a semicolon, as program statements must. Instead, it starts with a number sign (#). It’s called a preprocessor directive. Recall that program statements are instructions to the computer to do something, such as adding two numbers or printing a sentence. A pre-processor directive, on the other hand, is an instruction to the compiler.

A part of the compiler calls the pre-processor deals with these directives before it begins the real compilation process.

The preprocessor directive #include tells the compiler to insert another file into your source file. In effect, the #include directive is replaced by the contents of the file indicated. Using an #include directive to insert another file into your source file is similar to pasting a block of text into a document with your word processor. The type file usually included by #include is called a header file.

HEADER FILES

The preprocessor directive #include tells the compiler to add the source file IOSTREAM to the firstProgram.CPP source file before compiling. Why do this? IOSTREAM is an example of a header file (sometimes called an include file). It’s concerned with basic input/output operations, and contains declarations that are needed by the cout identifier and the << (insertion Operation) operator. Without these declarations, the compiler won’t recognize cout and will think << is being used incorrectly. The newer Standard C++ header files don’t have a file extension, but some older header files, left over from the days of the C language, have the extension .H.

The using Directive

A C++ program can be divided into different namespaces. A namespace is a part of the program in which certain names are recognized; outside of the namespace they’re unknown. The directive:

using namespace std;

says that all the program statements that follow are within the std namespace. Various program components such as cout are declared within this namespace. If we didn’t use the using directive,

we would need to add the std name to many program elements.

For example:Iin the FIRST program we’d need to say std::cout << “Every age has a language of its own.”;

To avoid adding std:: dozens of times in programs we use the using directive instead.

When writing a class library, programmers would prefer to use short and common names for non-member functions and classes, like add() and book. However, short and common names may turn out to be the same names selected by the creators of another library or by an application that uses the library. This can lead to “name clashes” and generate multiple definition errors from your compiler.

Defining a Namespace

A namespace is a section of a file that is given a name. The following code defines a namespace geo with some declarations inside it:


namespace geo

{

const double PI = 3.14159;

double circumf(double radius)

{

 return 2 * PI * radius;

}

} //end namespace geo


Braces delimit the namespace. Variables and other program elements declared within the braces are called namespace members. Notice that there is no semicolon following the closing brace, as there is with classes.

Accessing Namespace Members

Code outside a namespace cannot access the elements within it, at least not in the normal way.

The namespace makes them invisible:



namespace geo

{

const double PI = 3.14159;

double circumf(double radius)

{

 return 2 * PI * radius;

}

} //end namespace geo

double c = circumf(10); //won’t work here



To make the elements visible outside the namespace you must invoke the namespace name when referring to them.
There are two ways to do this.

  • First, you can precede each element’s name with the namespace name and the scope resolution operator::

double c = geo::circumf(10); //OK

  • Or you can use the using directive:

using namespace geo;

double c = circumf(10); //OK

The using directive ordinarily causes the namespace to be visible from that point onward. However, you can restrict the region where the using directive is in effect to a particular block, such as a function:


void seriousCalcs()

{

using namespace geo;

//other code here

double c = circumf(r); //OK

}

double c = circumf(r); //not OK

Here the members of the namespace are visible only within the function body.


Namespaces in Header Files

Namespaces are most commonly used in header files containing library classes or functions. Each such library can have its own namespace. By this time you are familiar with the namespace std, whose members constitute the Standard C++ Library.

Multiple Namespace Definitions


namespace geo

{

const double PI = 3.14159;

} // end namespace geo

//(some other code here)

namespace geo

{

double circumf(double radius)

{ return 2 * PI * radius; }

} //end namespace geo


This looks like a redefinition, but it’s really just a continuation of the same definition. It allows a namespace to be used in several header files, which can then all be included in a source file.

In the Standard C++ Library, dozens of header files use the namespace std.


//fileA.h

namespace alpha

{

void funcA();

}

//fileB.h

namespace alpha

{

void funcB();

}

fileMain.cpp

#include “fileA.h”

#include “fileB.h”

using namespace alpha;

funcA();

funcB();


You can place declarations outside a namespace that behave as if they were inside it. All you need is the scope resolution operator and the namespace name:


namespace beta

{

int uno;

}

int beta::dos;

Here, both uno and dos are declared in the namespce beta.


 Unnamed Namespaces

You can create a namespace without a name. Doing so creates a namespace that is automatically visible throughout the file in which it’s defined, but not visible from other files. The compiler gives an unnamed namespace an internal name unique to the file. Elements declared in the unnamed namespace can be accessed from anywhere in the file. In the following listing,

funcA() and funcB() can access the gloVar variable in their respective files.


//fileA.cpp

namespace //unnamed namespace unique to fileA.cpp

{

int gloVar = 111;

}

funcA()

{ cout << gloVar; } //displays 111

//fileB.cpp

namespace //unnamed namespace unique to fileB.cpp

{

int gloVar = 222;

}

funcB()

{ cout << gloVar; } //displays 222


In this example both files contain a variable named gloVar, but there’s no conflict because each variables is declared in an unnamed namespace unique to its file and is invisible everywhere else.

This approach provides an alternative to the use of static for restricting the scope of global variables to their own file. In fact, the namespace approach is now considered preferable to making elements static.

Leave a Comment