- [Instructor] A C++ program is basically a collection
of statements and expressions usually organized
into functions and classes.
In this lesson,
I'll show you the general anatomy of a C++ program.
Please understand that this is not exhaustive.
There are many other elements that may be involved
in a C ++ program.
This is just the basics
and there's no need to pay too much attention
to the details.
We'll go over each of these concepts in more detail
throughout the course.
For now it's just important that you have a general overview
of how these things work.
Here's a sample Hello World in C++,
you'll find this code in chapter two
of your exercise files as hello.CPP.
White space is any characters that are normally invisible,
including new lines, spaces and tabs
and white spaces mostly ignored and C++
one notable exception is when a space is necessary
to distinguish between two words or tokens.
For example, the space between the words int and main
is significant and is required.
Extra spaces would be ignored.
Comments are notations in the code
that are ignored by the compiler.
In practice ,they're stripped out by the preprocessor
C++ supports two different types of comments.
Single line comments are introduced by two forward slashes.
The comment ends at the end of the line.
In this example,
there are two separate comments each on a line by itself.
A comment may begin on a line after existing code
without affecting the code itself.
C++ also supports multi-line comments.
A multi-line comment may span several lines.
The comment begins at the special slash asterisk token,
and it ends after the matching asterisks slash token,
both styles of comments are inherited from the C language.
The multi-line comment was part of the original C language
and the single line comment was added later.
The entry point for any C++ program
has always a function called main.
All C++ programs must have a main function.
This is also true for C,
we'll talk about functions at length later in this course.
The main function may also have arguments
for parsing the command line.
The first argument is an integer
representing the number of command line arguments.
The second argument is an array of character arrays.
The second argument is sometimes written like this.
It means exactly the same thing.
We'll cover arrays and pointers
in more detail later in this course.
The body of a function is enclosed in curly braces.
The code between the braces
is run whenever the function is called,
the main function must return an integer value.
Zero is commonly returned
to indicate the program exited normally
to indicate an air condition,
a main function would return a one instead of a zero
other values may have other meanings
in specific circumstances
and we'll cover functions in more detail later
in this course.
The include directive instructs the preprocessor
to include another file to pass to the compiler.
In this case we're including the C standard IO header,
which supports the puts function
as well as other standard functions
that C++ inherits from C.
It is very common to include header files
at the top of your source code.
In fact, it is rare for a program
not to include a header files.
Will cover the preprocessor and include files
in more detail later in the course.
One final note,
you'll often see this line done in other ways.
This is also valid.
I choose to use puts instead of see out because it's simple.
Other styles will be explained later in this course.
So let's take a look at this in the IDE.
Here's a working copy of hello. CPP from chapter two
of the exercise files.
You notice I have comments at the top,
I have the include directive
and I have the main function
with its contents inside curly braces.
And when I build and run this,
it displays the message "Hello world".
So as we learned, white space is entirely ignored.
So I can eliminate a lot of this white space, right?
I can even eliminate that
and save and build and run.
And you notice that it works exactly the same.
I can put all this white space back.
I can add a bunch of extra new lines.
I can put extra spaces in.
If I put extra spaces here inside the quotes,
you'll notice that it'll actually display
those extra spaces.
Those are not ignored 'cause they're inside the quotes,
but everything else here is entirely ignored.
I can put in comments,
I can use multi-line comments,
or I can put in single line comments like here
and when I build and run,
it works exactly the same.
So this is the basic form of a C++ program.
As I said, we'll go into much more detail
about all of these topics as we continue with this course.