25-05-2012, 02:06 PM
A Crash Course in C++
C++.pdf (Size: 346.71 KB / Downloads: 74)
The goal of this chapter is to cover briefly the most important parts of C++ so that you have a base
of knowledge before embarking on the rest of the book. This chapter is not a comprehensive lesson
in the C++ programming language. The very basic points (like what a program is and the difference
between = and ==) are not covered. The very esoteric points (remember what a union is? how
about the volatile keyword?) are also omitted. Certain parts of the C language that are less relevant
in C++ are also left out, as are parts of C++ that get in-depth coverage in later chapters.
This chapter aims to cover the parts of C++ that programmers encounter on a daily basis. If you’ve
been away from C++ for a while and you’ve forgotten the syntax for a for loop, you’ll find that in
this chapter. If you’re fairly new to C++ and you don’t understand what a reference variable is,
you’ll learn that here as well.
If you already have significant experience with C++, skim this chapter to make sure that there
aren’t any fundamental parts of the language on which you need to brush up. If you’re new to
C++, take the time to read this chapter carefully and make sure that you understand the examples.
If you need additional introductory information, consult the titles listed in Appendix B.
The Basics of C++
The C++ language is often viewed as a “better C” or a “superset of C.” Many of the annoyances
or rough edges of the C language were addressed when C++ was designed. Because C++ is based
on C, much of the syntax you’ll see in this section will look familiar to you if are an experienced
C programmer. The two languages certainly have their differences, though. As evidence, The C++
Programming Language by C++ creator Bjarne Stroustrup weighs in at 911 pages, while Kernighan
and Ritchie’s The C Programming Language is a scant 274 pages. So if you’re a C programmer, be on
the lookout for new or unfamiliar syntax!
Preprocessor Directives
Building a C++ program is a three-step process. First, the code is run through a preprocessor, which recognizes
metainformation about the code. Next, the code is compiled, or translated into machine-readable
object files. Finally, the individual object files are linked together into a single application. Directives that
are aimed at the preprocessor start with the # character, as in the line #include <iostream> in the
previous example. In this case, an include directive tells the preprocessor to take everything from the
iostream header file and make it available to the current file. The most common use of header files is to
declare functions that will be defined elsewhere. Remember, a declaration tells the compiler how a function
is called. A definition contains the actual code for the function. The iostream header declares the
input and output mechanisms provided by C++. If the program did not include it, it would be unable to
perform its only task of outputting text.
The main function
main() is, of course, where the program starts. An int is returned from main(), indicating the result
status of the program. main() takes two parameters: argc gives the number of arguments passed to the
program, and argv contains those arguments. Note that the first argument is always the name of the
program itself.