Pages

Wednesday, 8 June 2011

C++ Basics


Returning a Value from main( )
The code examples in this book always explicitly return an integer value from main( ).
By convention, a return value of zero indicates successful termination. A non-zero return
value indicates some form of error.
Explicitly returning a value from main( ) is not technically necessary, however, because
in the words of the International Standard for C++:
"If control reaches the end of main without encountering a return statement, the effect
is that of executing return 0;"
For this reason, you will occasionally find code that does not explicitly return a value from
main( ), relying instead upon the implicit return value of zero. But this is not the approach
used by this book.
Instead, all of the main( ) functions in this book explicitly return a value because of two
reasons. First, some compilers issue a warning when a non-void method fails to explicitly
return a value. To avoid this warning, main( ) must include a return statement. Second, it
just seems good practice to explicitly return a value, given that main( ) is declared with an
int return type!

Using Namespace std ?
One of the problems that a writer of a C++ book faces is whether or not to use the line:
using namespace std;
C h a p t e r 1 : O v e r v i e w 5
near the top of each program. This statement brings the contents of the std namespace into
view. The std namespace contains the C++ standard library. Thus, by using the std namespace,
the standard library is brought into the global namespace, and names such as cout can be
referred to directly, rather than as std::cout.
The use of
using namespace std;
is both very common and occasionally controversial. Some programmers dislike it, suggesting
that it defeats the point of packaging the standard library into the std namespace and invites
conflicts with third-party code, especially in large projects. While this is true, others point out
that in short programs (such as the examples shown in this book) and in small projects, the
convenience it offers easily offsets the remote chance of conflicts, which seldom (if ever) occur
in these cases. Frankly, in programs for which the risk of conflicts is essentially zero, having to
always write std::cout, std::cin, std::ofstream, std::string, and so on is tedious. It also makes
the code more verbose.
The foregoing debate notwithstanding, this book uses
using namespace std;
in the example programs for two reasons. First, it makes the code shorter, which means that
more code can fit on a line. In a book, line-length is limited. Not having to constantly use
std:: shortens lines, which means that more code can fit on a single line without causing the
line to break. The fewer broken lines, the easier the code is to read. Second, it makes the code
examples less verbose, which enhances their clarity on the printed page. It has been my
experience that using namespace std is very helpful when presenting example programs
shown in a book. However, its use in the examples is not meant as an endorsement of the
technique in general. You must decide what is appropriate for your own programs.

0 comments:

Post a Comment

Search This Blog