The C++ Standard specifies new names for the header files, as described in Chapter 2, “Setting Out to C++.” If you’ve been using the old-style header files, you should change over to using the new-style names. This is not just a cosmetic change because the new versions sometimes add new features. For example, the ostream header file provides support for wide-character input and output. It also provides new manipulators such as boolalpha and fixed (as described in Chapter 17, “Input, Output, and Files”). These offer a simpler interface than using setf() or the iomanip functions for setting many formatting options. If you do use setf(), you should use ios_base instead of ios when specifying constants; that is, you should use ios_base::fixed instead of ios::fixed. Also the new header files incorporate namespaces.
Use Namespaces
Namespaces help organize identifiers used in a program in order to avoid name conflicts. Because the standard library, as implemented with the new header file organization, places names in the std namespace, using these header files requires that you deal with namespaces.
The examples in this book, for simplicity, most often utilize a using directive to make all the names from the std namespace available:
#include
#include
#include
using namespace std; // a using-directive
However, the wholesale exporting of all the names in a namespace, whether needed or not, runs counter to the goals of namespaces.
Somewhat better is placing a using directive inside a function; this makes the names available just inside that function.
Even better, and the recommended approach, is to use either using declarations or the scope-resolution operator (::) to make available just those names a program needs. For example, the following makes cin, cout, and endl available for the rest of the file:
#include
using std::cin; // a using-declaration
using std::cout;
using std::endl;
Using the scope-resolution operator, however, makes a name available just in the expression that uses the operator:
cout << std::fixed << x << endl; //using the scope resolution operator
This could get wearisome, but you could collect your common using declarations in a header file:
// mynames — a header file
using std::cin; // a using-declaration
using std::cout;
using std::endl;
Going a step further, you could collect using declarations in namespaces:
// mynames — a header file
#include
namespace io
{
using std::cin;
using std::cout;
using std::endl;
}
namespace formats
{
using std::fixed;
using std::scientific;
using std:boolalpha;
}
Then a program could include this file and use the namespaces it needs:
#include "mynames"
using namespace io;
Use Smart Pointers
Each use of new should be paired with a use of delete. This can lead to problems if a function in which new is used terminates early via an exception being thrown. As discussed in Chapter 15, using an autoptr object to keep track of an object created by new automates the activation of delete. The C++11 additions unique_ptr and shared_ptr provide yet better alternatives.
Use the string Class
The traditional C-style string suffers from not being a real type. You can store a string in a character array, and you can initialize a character array to a string. But you can’t use the assignment operator to assign a string to a character array; instead, you must remember to use strcpy() or strncpy(). You can’t use the relational operators to compare C-style strings; instead, you must remember to use strcmp(). (And if you forget and use, say, the > operator, you don’t get a syntax error; instead, the program compares string addresses instead of string contents.)
The string class (see Chapter 16, “The string Class and the Standard Template Library,” and Appendix F, “The string Template Class”), on the other hand, lets you use objects to represent strings. Assignment operators, relational operators, and the addition operator (for concatenation) are all defined. Furthermore, the string class provides automatic memory management so that you normally don’t have to worry about someone entering a string that either overruns an array or gets truncated before being stored.
The string class provides many convenience methods. For example, you can append one string object to another, but you can also append a C-style string or even a char value to a string object. For functions that require a C-style string argument, you can use the c_str() method to return a suitable pointer-to-char.
Not only does the string class provide a well-designed set of methods for handling string-related tasks, such as finding substrings, but it also features a design that is compatible with the Standard Template Library (STL) so that you can use STL algorithms with string objects.
Use the STL