|
| 23.2.3 Explicit Element Access
// explicit access, namespace within a class #include <iostream> using namespace std;
class One { public: void funct1(char chs) {cout<<"character = "<<chs<<endl;} };
class Two:public One { public: //The using directive is not allowed in class using namespace One; void funct1(char *str) {cout<<"String = "<<str<<endl;} // using declaration is OK in class using One::funct1; // overload Two::funct1() };
int main() { Two Sample; // calling One::funct1() Sample.funct1('P'); // calling Two::funct1() Sample.funct1("This is string"); return 0; }
Output:
|
All the classes, objects and functions of the standard C++ library are defined within namespace std, defined by the ISO/ANSI C++ or a new one, ISO/IEC C++.
Even though compiler that comply with ISO/ANSI C++ standard allow the use of the traditional header files (such as iostream.h, stdlib.h or other than .h extension for implementation dependent etc), actually the standard has specified new names for these header files, using the same name but without the .h (or other extension for implementation dependent) under the namespace std. For example, iostream.h becomes iostream.
All functions, classes and objects will be declared under the std namespace if using the ISO/ANSI C++ compliance compiler. The .h header files have been provided for backward compatibility.
The ISO/ANSI C++ standard requires you to explicitly declare the namespace in the standard library. For example, when using header file iostream.h, you do not have to specify the namespace of cout in one of the following ways (as used throughout this tutorial):
std::cout – explicitly
using std::cout – using declaration
using namespace std – using directive
Keep in mind that in C++ .NET the namespace used intensively.
If you use iostream without the .h extension, then you have to explicitly include either one of those three codes.
The following is a simple program example and make sure your compiler is ISO/ANSI C++ compliance.
// namespace std example, notice the omitted .h header files
#include <iostream>
void main()
{
std::cout<<"Demonstrating ";
using namespace std;
cout<<"the std namespace."<<endl;
}

Then, try comment out the following line, recompile the program. Your compiler should generate error.
using namespace std;
C++ library entities such as functions and classes are declared or/and defined in one or more standard headers. To make use of a library entity in a program, as in C program, we have to include them using the include preprocessor directive, #include.
The Standard C++ library headers as shown in Table 23.1 together with the 16 Standard C headers (C++ wrappers - get the ideas of the wrappersswig.org) shown in Table 23.2, constitute an implementation of the C++ library.
There are several headers that are rarely used are not included here, please check your compiler documentation.
These headers are template based. You will learn Template in next Module (C++ Templates).
<algorithm> | <bitset> | <complex> |
<deque> | <exception> | <fstream> |
<functional> | <hash_map> | <hash_set> |
<iomanip> | <ios> | <iosfwd> |
<iostream> | <istream> | <iterator> |
<limits> | <list> | <locale> |
<map> | <memory> | <new> |
<numeric> | <ostream> | <queue> |
<set> | <sstream> | <stack> |
<stdexcept> | <streambuf> | <string> |
<strstream> | <utility> | <valarrary> |
<vector> |
|
|
Table 23.1: C++ Standard header | ||
<cassert> | <cctype> | <cerrno> |
<cfloat> | <ciso646> | <climits> |
<clocale> | <cmath> | <csetjmp> |
<csignal> | <cstdarg> | <cstddef> |
<cstdio> | <cstdlib> | <cstring> |
<ctime> |
|
|
Table 23.2: C++ wrapper – Using C library in C++ codes. Note that there is no .h extension | ||
If you want to use functions, structure, macros and other built-in item available in the Standard C headers in C++ programming environment or ISO/IEC C++ compilers, use the C++ wrappers.
These C++ wrappers are C headers that prefixed by c character such as <cassert> from <assert.h>.
Other than those ISO/IEC C and ISO/IEC C++ Standard headers should be implementation dependant, it is non standard. Keep in mind that there are a lot more non standard headers that you will find.
If you want to use for example, the member functions or classes of these non standard headers, you have to get the information through their documentation.
The issue of using the non standard library is the program portability. There is a lot of C/C++ implementation out there, so you decide it, depend on your needs and what is the target platform your program are developed for and what compiler you are going to use to develop the programs.
Acomplete C++ Standard Library documentation that includes STL.
In C++, you include the contents of a standard header by using theinclude preprocessor directive as shown below:
#include <iostream>#include <cstdlib>You can include the standard headers in any order, a standard header more than once, or two or more standard headers that define the same macro or the same type. Do not include a standard header within a declaration.
A Standard C header never includes another standard header. Every function in the library is declared in a standard header.
Unlike in Standard C, the standard C++ header never provides a masking macro, with the same name as the function that masks the function declaration and achieves the same effect.
All names other than operator delete and operator new in the C++ library headers are defined in the std namespace, or in a namespace nested within the std namespace. You refer to the name cout, for example, as std::cout.
The most portable way to deal with namespaces may be to obey the following two rules:
To assuredly declare an external name in namespace std that is traditionally declared in <stdlib.h>, for example, include the header <cstdlib>. Knowing that the name might also be declared in the global namespace.
To assuredly declare in the global namespace an external name that is declared in<stdlib.h>, include the header<stdlib.h> directly. Knowing that the name might also be declared in namespace std.
For example if you want to use std::cout, you should include<iostream>. If you want to useprintf(), you should include<stdio.h> instead of <cstdio>.
using namespace std;
|
external-declaration
translation-unit external-declaration
external-declaration
function-definition
declaration
The following examples tested by using Visual C++ 6.0 and Visual Studio .Net. It just a re compilation of the program examples from other Modules, assuming that the compiler is fully ISO/IEC C++ compliance. Notice some of the code modifications.
/* a simple mathematics calculation */
// this program is from module 1, C program.
// header files used is the C++ wrapper, no .h anymore.
// the stdlib.h for system("pause") also has been removed
#include <cstdio>
// the main() function
int main( )
{
// variables declaration and initialization
int x, y, z;
x = 20;
y = 2;
printf("\nGiven x = 20, y = 2\n");
printf("\nx / y = %d", x / y);
x = x * y;
y = y + y;
printf("\nx = x * y");
printf("\ny = y + y");
printf("\nNew value for x / y = %d", x / y);
z = 20 * y / x;
printf("\nz = 20 * (y / x )= %d\n", z);
return 0;
}

// demonstrates the use of function prototypes C++ program, no .h anymore
#include <iostream>
// but has to explicitly use the 'using namespace std'
using namespace std;
// another method simplifying type identifier
typedef unsigned short USHORT;
// function prototype
USHORT FindTheArea(USHORT length, USHORT width);
int main()
{
USHORT lengthOfYard;
USHORT widthOfYard;
USHORT areaOfYard;
cout<< "\nThe wide of your yard(meter)? ";
cin>> widthOfYard;
cout<< "\nThe long of your yard(meter)? ";
cin>> lengthOfYard;
areaOfYard = FindTheArea(lengthOfYard, widthOfYard);
cout<< "\nYour yard is ";
cout<< areaOfYard;
cout<< " square meter\n\n";
return 0;
}
USHORT FindTheArea(USHORT l, USHORT w)
{
return (l * w);
}

// demonstrates the use of function prototypes
// variation of the C++ program, no .h anymore
// without the 'using namespace std;'
#include <iostream>
// another method simplifying type identifier
typedef unsigned short USHORT;
// a function prototype
USHORT FindTheArea(USHORT length, USHORT width);
int main()
{
USHORT lengthOfYard;
USHORT widthOfYard;
USHORT areaOfYard;
// without using namespace std globally, you have to
// explicitly use the std for every occurrences of the...
std::cout<< "\nThe wide of your yard(meter)? ";
std::cin>> widthOfYard;
std::cout<< "\nThe long of your yard(meter)? ";
std::cin>> lengthOfYard;
areaOfYard = FindTheArea(lengthOfYard, widthOfYard);
std::cout<< "\nYour yard is ";
std::cout<< areaOfYard;
std::cout<< " square meter\n\n";
return 0;
}
USHORT FindTheArea(USHORT l, USHORT w)
{
return (l * w);
}

Example compiled usingg++.
// ***************namespace.cpp**************/
// demonstrates the use of function prototypes
// variation of the C++ program, no .h anymore
// without the 'using namespace std;'
#include <iostream>
// another method simplifying type identifier
typedef unsigned short USHORT;
// a function prototype
USHORT FindTheArea(USHORT length, USHORT width);
int main()
{
USHORT lengthOfYard;
USHORT widthOfYard;
USHORT areaOfYard;
// without using namespace std globally, you have to
// explicitly use the std for every occurrences of the...
std::cout<< "\nThe wide of your yard(meter)? ";
std::cin>> widthOfYard;
std::cout<< "\nThe long of your yard(meter)? ";
std::cin>> lengthOfYard;
areaOfYard = FindTheArea(lengthOfYard, widthOfYard);
std::cout<< "\nYour yard is ";
std::cout<< areaOfYard;
std::cout<< " square meter\n\n";
return 0;
}
USHORT FindTheArea(USHORT l, USHORT w)
{
return (l * w);
}
[bodo@bakawali ~]$ g++ namespace.cpp -o namespace
[bodo@bakawali ~]$ ./namespace
The wide of your yard(meter)? 200
The long of your yard(meter)? 10
Your yard is 2000 square meter
tenouk fundamental of C++ namespaces
Source code for the program examples is available inC++ Namespace source codes.
Check thebest selling C / C++, STL and UML books at Amazon.com.
Acomplete C++ Standard Library documentation that includes STL.
Standards: The C/C++ standards references (ISO/IEC is covering ANSI and is more general):
a. ISO/IEC 9899 (ISO/IEC 9899:1999) - C Programming language.
b. ISO/IEC 9945-1:2003 POSIX standard.
c. ISO/IEC 14882:1998 on the programming language C++.
d. ISO/IEC 9945:2003, The Single UNIX Specification, Version 4.