OOPortal OOPortal
Function/Variable Scope   «Prev 

Namespaces in C++

Header files, such as iostream.h or vector, are routinely used by the system to provide libraries that support important elements in C++ coding. They provide external variables, function prototypes, constant declarations, inline functions, and typedef declarations imported by other modules by an #include directive. When the header files are wrapped in namespace declarations, the code should have an appropriate using declaration, such as using namespace std for accessing standard library names.
Keeping related code together makes it easier to update and document as changes occur in one central location. Associated code and internal variables implementing those header files can be maintained in code files usually having suffixes .c and .cpp. Compile this code in the command-line oriented environment by compiling each file as either source code or object code.

Legacy versus Modern C++ I/O

There are currently two versions of the C++ object-oriented I/O library in use: the older one that is based upon the original specifications for C++ and the newer one defined by Standard C++. The old I/O library is supported by the header file <iostream.h>.
The new I/O library is supported by the header <iostream>.
For the most part the two libraries appear the same to the programmer. This is because the new I/O library is, in essence, simply an updated and improved version of the old one. In fact, the vast majority of differences between the two occur beneath the surface, in the way that the libraries are implemented, not in how they are used. From the programmer's perspective, there are two main differences between the old and new C++ I/O libraries. First, the new I/O library contains a few additional features and defines some new data types. Thus, the new I/O library is essentially a superset of the old one. Nearly all programs originally written for the old library will compile without substantive changes when the new library is used. Second, the old-style I/O library was in the global namespace.
The new-style library is in the std namespace. (Recall that the std namespace is used by all of the Standard C++ libraries.) Since the old-style I/O library is now obsolete, this book describes only the new I/O library, but most of the information is applicable to the old I/O library as well

C++ Streams

Like the C-based I/O system, the C++ I/O system operates through streams.
However, to summarize: A stream is a logical device that either produces or consumes information. A stream is linked to a physical device by the I/O system. All streams behave in the same way even though the actual physical devices they are connected to may differ substantially. Because all streams behave the same, the same I/O functions can operate on virtually any type of physical device. For example, you can use the same function that writes to a file to write to the printer or to the screen. The advantage to this approach is that you need learn only one I/O system.