Thursday, July 15, 2010

C++: Behind the Scenes explanations of our simple program

Hope you're enjoying the series. I hope you are learning something valuable
here.

In previous post, I've wrote a small "Hello World" program to demonstrate
how C++ programmers write a small program. Now, let's look at the "behind
the scenes" work at how this is done, as well as definitions of some terms
used in C++.

So, the first statement was "#include". This tells the computer (more
precisely, the compiler) that we want to use facilities and instructions (or
functions) found in a library (a collection of functions) so that we can do
something with our program. In my example, I've included two libraries: the
string library, used to work with strings (a group of characters) and
iostream, the code that allows us to read from and write to a screen (called
input/output stream). Strings and iostream functions will be examined in
more detail later. This is fine for now, since all we are dealing with is
simple input and output, as well as creating basic textual programs. When we
get into more complex things such as writing to and reading from files and
working with vectors (a group of same data), we'll include more libraries in
our code.

C++ offers so many libraries for doing tasks. There is the math library
(called cmath( that allows a computer to perform calculations, an
input/output manipulator called "iomanip" and so forth. I'll introduce you
to the concept of libraries, functions and classes (our own data types) as
we go along, but for our situation, we'l only need string and iostream.

The next line was, "using namespace std". This means that we want to work
with standard template library (more on that later), a group of functions
for doing general things. If we don't include this line, we are forced to
write the following:
std::cout << "A test message..." << std::endl;
The two colons (::) is formally known as "scope resolution operator", which
is used to tell the compiler where to lookup certain operators from certain
libraries. This becomes important as we go onto designing our own data
types, or classes. But for now, I'd say it'd be safe to use "using namespace
std;" when doing our program exercises.

The next few lines were the actual program code. It starts with the line,
"int main()", followed by a left brace ({). This is the special function (a mini program that does
something on behalf of the programmer), since it is required to even run a
program (the compiler will check to make sure you've put that). This tells
the computer that this is where the actual program starts. Then how does a
program know we are done with it? It looks for the line that says, "return
(0);" followed by a right brace (]). The curly braces ({, }) tells the
computer that we are in a particular section, or scope in our program. So, if
you are writing a program, the best practice is to do the following first:
int main()
{
Return (0);}
And compile it (be sure to include any libraries you'll need...). Anything
in between these lines (between the left brace and the return) is our
program itself.

Next, we'll cover what "cout" and "cin" means - how to display information
and get something from users.
//JL

1 comment:

  1. Great explanation! You might want to type left and right curly braces in the format where they have their own separate lines, because that format is usually more convenient for visual readers to comprehend.

    Ex.:

    int main()
    {
    return 0;
    }

    ReplyDelete