Namespaces and new headers
The programs that we've seen so far are not wrong but they can't be called as 100% C++ programs either. The reason is because we've been making use of header files (such as iostream.h etc...). Making use of these .h files is more similar to C programming. Of course it isn't wrong to use them in C++ but there is a better method provided in C++. This method makes use of namespaces.
Instead of # include <iostream.h> we shall use the following two lines:
#include <iostream>
using namespace std;
Firstly take note of the fact that there is no ‘.h’ in the include statement. This is the format of the new style headers used in C++. They do not use .h files. Old C++ compilers may not support namespaces. In case you are using such a compiler then you have to stick to the old method of #include with the ‘.h’ extension. But all the latest C++ compilers will support namespaces and they will also support the old style headers.
When C++ was created it used header files like those used in C programming. But then changes were made. Though it still supports the old header files, they are not recommended. You know that iostream.h is a file. C++ created a new style of its own. In this method we do not specify filenames. Instead it is a method that makes sure that appropriate definitions required by the C++ library have been declared. The new style headers are not filenames and hence the .h extension is left out.
The tabulation below lists out the various headers we have used and their equivalent new style headers.
Old style header name | Equivalent new style header | Purpose |
iostream.h | <iostream> | General purpose I/O |
fstream.h | <fstream> | File I/O operations |
iomanip.h | <iomanip> | Manipulators |
string.h | <cstring> | character array string operations |
math.h | <cmath> | Mathematical Functions |
stdlib.h | <cstdlib> | Memory allocation, numeric and system functions |
- | <string> (supports the string class). | String objects |
If you notice in the tabulation above, some of the new style headers make use of the prefix ‘c’ in the header file. For example:
stdlib.h
is now:
<cstdlib>
These headers were actually part of Standard C and since C++ supports C these headers were retained. Though these headers are supported they could be eliminated in the future.
When using a new style header you can make use of the namespace statement:
using namespace std;
A namespace is a region of declaration. The purpose of namespace is to avoid collision of identifiers (i.e. to avoid collision of variables or class names or function names etc.). Elements declared in one namespace are different from those declared in another. Always make use of C++ new style headers whenever you write a C++ program.
The first.cpp program using namespaces:
You can make use of new style headers in all the other programs that we have seen so far.
Thus the first program you wrote will become:
# include <iostream>
using namespace std;
int main( )
{
char letter;
cout << "Enter any letter" ;
cin >>letter;
cout << "The letter you entered is : " <<letter;
return 0;
}
A closer look into Namespaces Why we need Namespaces?
Namespaces were introduced to avoid name collisions. When
many people started to create libraries for different purposes, each person made
use of global variables. These variables are visible to the entire program and
can be accessed by any function. Now, when a programmer made use of two or more
libraries, there is a good chance that some variable names might coincide or
some identifier used by the programmer could clash with that used by the library
creator. This can lead to serious problems.
The problem can even be extended to function names( ). You could end up having a program that has two functions performing different tasks, having the same name and arguments. For example, both libraries may have the function draw( ). But both may have a different meaning to the function. So when you use the function draw ( ), the compiler would have a problem in deciding which library function it should use. Namespaces were created to solve these problems.
What namespace does?
Namespace localizes the visibility of names declared within it (the names are only visible within that particular namespace). So when you refer to some name, you can specify the namespace to which it belongs to avoid conflicts.
Syntax:
General form for creating a namespace is:
namespace name
{
// declarations
}
Anything defined within a namespace is only within the scope of the namespace and not available outside.
Example:
#include <iostream>
using namespace std;
namespace bact //A namespace called bact
{
class bacteria //class belonging to namespace bact
{
private:
int step;
public:
bacteria( )
{
cout<<"\nCreated Bacteria";
}
};
namespace vir //Another namespace ‘vir’
{
The output would be:
Created Bacteria
Created virus
The bacteria has a life : 1
The virus has a life : 0
Two namespaces ‘bact’ and ‘vir’ have been created in the program. Both contain a variable using the same identifier (‘life’) and both have a class within them. Within the main ( ) function, when we want to refer to the variable ‘life’ remember the following:
Similarly when creating an object belonging to the class ‘bacteria’, remember:
But once the object has been created, you needn't keep specifying the namespace (i.e. for calling member functions you needn’t specify the namespace again).
The ‘using’ Keyword
In the previous section, you must have noticed that each time
we refer to a namespace variable you have to make use of scope resolution
operator. You also need to specify the namespace. Doing this repeatedly is a
tedious job, especially if you are going to make use of a particular namespace
frequently. Imagine how a simple program cluttered with the name of the
namespace and the scope resolution operator everywhere would look.
The best solution is to use the keyword using.
What about namespace std?
The std namespace is the area in which the entire Standard C++ library is
declared. Remember how we start our programs:
#include <iostream>
using std namespace;
int main ( )
{
}
The ‘using’ statement informs the compiler that you want to use the ‘std’
namespace. The keywords cout, cin etc. are all present in the std namespace.
You can also make use of the following method for writing programs:
#include <iostream>
int main ( )
{
int x ;
std:: cout<< "Enter a number : ";
std:: cin>>x;
std :: cout<<x;
return 0;
}
In this case we haven’t specified the statement:
using namespace std;
Generally we don't use this method because we will use ‘cout’ and ‘cin’ very
frequently in our coding. By the above method you will have to use std :: each
time you use ‘cout’ or ‘cin’.
It is very convenient to make use of
using std namespace;
before the main ( ) function to tell the compiler that by default use the ‘std’ namespace.
There are two methods to make use of the using keyword:
using namespace name; //This is called as the Using Declaration
Example: using namespace bact;
or
using name :: member; //This is called as the Using Directive
Example: using bact :: life;
In the first case, you don't need to make use of scope
resolution when you are referring to any variable or function belonging to
namespace ‘bact’.
In the second case, only the variable ‘life’ belonging to the ‘bact’ namespace
is made visible. Check out the example below:
#include <iostream> //we’ve not specified using namespace std;
namespace bact
{
int life;
class bacteria
{
private:
int step;
};
}
namespace vir
{
int life;
class virus
{
private:
int step;
};
}
int main( )
{
using namespace bact; //the compiler uses bact namespace
life=1; //this refers to ‘life’ of ‘bact’
vir::life=0;
bacteria fever;
vir::virus cholera;
std::cout<<"\nThe bacteria has a life : "<<life;
std::cout<<"\nThe virus has a life : "<<vir::life;
return 0;
}
In the main ( ) function, we have said
using namespace bact;
‘life’ is a variable belonging to ‘bact’ namespace and the
class ‘bacteria’ is a class belonging to the ‘bact’ namespace. Since you’ve
asked the compiler to use the ‘bact’ namespace, you don't need the scope
resolution operator whenever you are referring to something from ‘bact’
namespace. Suppose you want to refer to the ‘life’ variable of the namespace ‘vir’,
then you have to explicitly say so (using scope resolution).
Note that in the above program we are making use of
std::cout
because we haven’t mentioned:
using namespace std;
So, we need to explicitly state the namespace to which ‘cout’ belongs to (otherwise your compiler will say that it doesn’t know what ‘cout’ is).
What if we declare a variable outside the namespaces? In C++, if you declare a variable outside the main ( ) function and outside all other functions, then the variable becomes a global variable. If you declare a variable outside all namespaces, then the variable is now present in the global namespace. Check out the example below:
#include <iostream>
int life=0; //life in the global namespace
namespace bact
{
int life; //life in bact namespace
class bacteria
{
private:
int step;
};
}
int main( )
{
bact::life=1;
::life=0;
std::cout<<"\nThe bacteria has a life : "<<bact::life;
std::cout<<"\nThe organism has a life : "<<::life;
return 0;
}
The output would be:
The bacteria has a life : 1
The organism has a life : 0
When we say:
::life
it refers to the ‘life’ present in the global namespace.
Beware: You cannot create namespaces within functions. Namespaces can be nested (one within the other).
Go back to the Contents Page 2
Copyright © 2004 Sethu Subramanian All rights reserved.