C++ Header in Namespace
Writing a C++ Application – Part 4
Forward: In this part of the series, I explain why and how a header should be included in a namespace.
By: Chrysanthus Date Published: 7 Feb 2013
Introduction
Header and Scope
Whether you are dealing with a long or large application, the header is typically in the global scope; even if the header resides in a file (header.hh) as it should be. That is fine.
Reason For Namespace
The global scope is a convenient place to declare entities. However, it is not very convenient because of name conflict (ambiguity). The namespace came to consolidate scopes and so minimize name conflict. So, it is good to place a header in a namespace. The header we have so far in this series can be placed in a namespace as follows:
namespace alpha
{
const double pi = 3.141592;
#define myConstStr "I love you."
enum DayNumber {today, tomorrow, afterTomorrow, theDayAfterTomorrow};
class Calculator
{
public:
int num1;
int num2;
Calculator(int ident1, int ident2);
int add();
};
static Calculator::Calculator myObject(2,3);
void fn();
}
The header is the same as before, but this time it has been enclosed in a namespace called, alpha. You will have to try this:
Go to the c:\dir1 directory and change the content of the header to what you have above. For this new header scheme to work, you have to make some changes in the mainFile.cpp and sampleEntities.cpp files.
Go to the mainFile.cpp file in the working directory and add the line,
using namespace alpha;
just below the line,
#include "c:\dir1\demo.hh"
to have the corresponding pairs of lines:
#include "c:\dir1\demo.hh"
using namespace alpha;
So, after the inclusion, everything below uses the namespace alpha. The header is now in the namespace, alpha. Do the same thing in the file, sampleEntities.cpp of the c:\dir1 directory. The story does not end there. A function declared in the header of a namespace has to be modified a bit at the definition. In the sampleEntities.cpp file, the beginning of the function, fn() is:
void fn()
{
Replace this beginning with:
void alpha::fn()
{
to precise that the function is declared in the namespace, alpha. Note that the namespace name has been placed after the return type specifier (void). Also note the use of the scope resolution operator. That is all.
Now, at the command line of the working directory prompt, type the following (or according to your compiler) and press Enter to compile the application:
g++ c:\dir1\sampleEntities.cpp mainFile.cpp -o mainFile.exe
The application should compile without any error message.
To execute the application, type the following at the working directory prompt and press Enter:
mainFile.exe
You should see the result as before (1st and 2nd parts of series); no problem.
That is how you use a header in a namespace.
C++ has a library called the standard library. This library is provided with every C++ installation. This library is very large, broken down into smaller libraries or components. One of the components is called the iostream class. The synopsis of this class is declared inside the namespace called the std namespace, where std means standard. The cout object, which is used to output data to the console, is declared in the iostream class, inside the std namespace. That is, the iostream class synopsis and the cout object declaration are inside the std namespace. This means the iostream header is inside the std namespace. So the following two lines are typically seen together:
#include <iostream>
using namespace std;
The first line says include the iostream header, while the second line says, use the std namespace. You cannot really use the first line without the second line. We have a similar thing for the demo header and alpha namespace above.
And that is it for this part of the series, we stop here and continue in the next part.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT