As you learned earlier, whitespace is the term used in C++ to describe spaces, horizontal and vertical tabs, newline, and form-feed characters. In many instances, whitespace separates one part of a statement from another and enables the compiler to identify where one element in a statement ends and the next element begins. For example, look at the following line of code:
This statement involves int, which is a type name, and fruit, which is the name of a variable. There must be at least one whitespace character (usually a space) between int and fruit for the compiler to be able to distinguish them. This is because intfruit would be a perfectly acceptable name for a variable or indeed anything else, and the compiler would interpret it as such.
On the other hand, consider this statement:
No whitespace characters are necessary between fruit and =, or between = and apples, although you’re free to include some if you wish. This is because the equals sign (=) isn’t alphabetic or numeric, so the compiler can separate it from its surroundings. Similarly, no whitespace characters are necessary on either side of the plus sign (+). In fact, you’re free to include as little or as much whitespace as you like, so you could write the previous statement as follows:
If you do this, it’s unlikely you’ll be congratulated for good programming style, but the compiler won’t mind.
Apart from its use as a separator between elements in a statement, or when it appears in a string between quotes, the compiler ignores whitespace. You can, therefore, include as much whitespace as you like to make your program more readable. In some programming languages, the end of a statement is at the end of the line, but in C++ the end of a statement is wherever the semicolon occurs. This enables you to spread a statement over several lines if you wish, so you can write a statement like this:
Documenting Your Programs
Documenting your program code is extremely important. Code that seems crystal clear when you write it can look extraordinarily obscure when you’ve been away from it for a month. You can document your code using comments, of which there are two sorts in C++: single-line comments and multiline comments (that is, comments that can span several lines).
You begin a single-line comment with a double slash (//), for example
The compiler will ignore everything on the line following the double slash, but that doesn’t mean the comment has to fill the whole line. You can use this style of comment to explain a statement:
You can also temporarily remove a line of code from your program just by adding a double slash to the beginning of the line:
This converts the statement to a comment, which is something you might want to do during the testing of a program, for example. Everything from the first // in a line to the end of the line is ignored, including any further occurrences of //.
The multiline comment is sometimes used for writing more verbose, general descriptive material—explaining the algorithm used within a function, for example. Such a comment begins with /*, ends with */, and everything between these two is ignored. This enables you to embellish multiline comments to highlight them, for example
You can also use this comment style for temporarily disabling a block of code. Just put /* at the beginning of the block and */ at the end. However, you must take particular care not to nest /* ... */ comments; you’ll cause error messages from your compiler if you do. This is because the closing */ of the inner nested comment will match the opening /* of the outer comment:
The last part of the outer comment is left “dangling,” and the compiler will try to compile it, which will inevitably result in failure. For this reason, the // form of comment is the most widely used in C++ programs.
The Standard Library
The standard library contains a substantial number of functions and other things that support, augment, and extend the basic language capabilities of C++. The contents of the standard library are just as much a part of C++ as the syntax and semantics of the language itself. The standard for C++ defines both, and so every compiler that conforms to the standard will supply the complete standard library.
Bearing this in mind, the scope of the standard library is extraordinary. You get a vast range of capability, from essential elements such as basic language support, input and output functions, and exception handling (an exception is an unusual occurrence during program execution—often an error of some kind) to utility functions, mathematical routines, and a wide range of prewritten and tested facilities that you can use to store and manage data during execution of your program.
To use C++ most effectively, you should make sure that you have a good familiarity with the contents of the standard library. You’ll be introduced to many of the capabilities of the standard library as you learn the C++ language in this book, but the degree of coverage within the book will inevitably be incomplete. It would take another book comparable with the size of this one to cover the capability and use of the standard library comprehensively.
The definitions and declarations necessary to use standard library facilities appear in the standard headers touched upon earlier. There are a few cases in which the standard headers will be included in your program files by default, but in most instances you must add an #include directive for the appropriate header for the library facilities that you want to use. You’ll find a comprehensive list of the standard headers in Appendix C, with a brief description of what sort of functionality each one supports.
Almost everything in the C++ standard library is defined within the namespace std. This means that all the names that you’ll use from the library are prefixed with std. As you saw at the beginning of the chapter, when you reference something from the standard library, you can prefix the name with std, as in the following statement:
std::cout << "The best place to start is at the beginning";
Alternatively, you can put a using directive at the beginning of your source file:
This allows you to use the name cout without its std prefix so you can write that statement as follows:
cout << "The best place to start is at the beginning";
You also saw earlier that you have a blanket capability for introducing names from the std namespace into a program file:
using namespace std;
This allows you to omit the std prefix for any standard library names that are defined in the headers you’ve included in your program. However, it has the serious disadvantage that it allows potential clashes between names you’ve defined and identical names in the standard library headers that you’ve included.
In this book I always include the std namespace prefix where necessary in code fragments. In complete working programs, you’ll generally add using statements for standard library names that you use repeatedly in code. Names that you use once or twice you’ll just qualify with the namespace name.Programming in C++
Because C++ inherits and enhances the power and flexibility of the original C language, you have a comprehensive capability for handling time-critical, low-level programming tasks and for dealing with problems for which a traditional procedural approach may be preferable. The major strengths of C++, though, are its powerful and extensive object-oriented features. These provide the potential for writing programs that are less error-prone, less time-consuming to maintain, simpler to extend, and easier to understand than their equivalent procedural solutions.
There are fundamental differences between these two programming methodologies, so let’s contrast them to highlight just how they’re different and see some of the reasons why an object-oriented approach can be so attractive.
blog comments powered by Disqus