Transcript Document

A First Program
"Hello, Dave"
• Before diving right into the nitty-gritty of
C++ language details, let's get started with a
full-fledged C++ program! The idea of this
program is to introduce you to the overall
structure of a C++ program and give you
the flavor of C++.
• Here, then, is an entire C++ program:
//include this file for cout
#include <iostream.h>
int main()
{
//print out the text string, "Hello, World!" cout <<
"Hello, World!" << endl;
return 0;
}
Compiling and Running
Go to the PDF
Brief Explanation of "Hello, Dave"
• Now that you've successfully compiled your
program, let's take a look at each line of
code so that you have a general
understanding of how the program works. It
will probably look confusing if you've never
seen C++ syntax before, but that's
completely natural.
Here's the program again:
//include this file for cout
#include <iostream.h>
int main()
{
//print out the text string, "Hello, World!" cout <<
"Hello, World!" << endl;
return 0;
}
Let's take a look at each line of
code that makes up hello.C
//include this file for cout
– This line is a comment line. The // indicates that
everything following it should be ignored by the compiler.
This allows you to add English explanations to what
might otherwise be confusing code. You have the freedom
to comment your code as much as you like -- some
programmers write code with no comments at all; others
write several lines of comments for each line of C++
code. It's all up to you. Keep in mind, though, that if
anyone else will ever read your code, you'll probably want
to add comments. Even if you are the only one who will
ever read your code, you should add comments. It sounds
implausible, but programmers often don't understand code
they've written weeks ago!
#include <iostream.h>
– This line is read "pound include i-o-stream dot h". The
effect of this line is to essentially "copy and paste" the
entire file iostream.h into your own file at this line. So
you can think of this syntax as replacing the line
#include <iostream.h> with the contents of the file
iostream.h. #include is known as a preprocessor
directive, which will be covered much later.
– Where is the file iostream.h?
This file is located somewhere in your include path.
The include path indicates the directories on your
computer in which to search for a file, if the file is not
located in the current directory.
– Why do I need to include iostream.h?
In this case, iostream.h is a file containing code for
input/output operations. You need to include iostream.h
so that the compiler knows about the word cout, which
appears a couple of lines below.
• int main() {
• Every C++ program must have what is known as a main
function. When you run the program, the program will go
through every line of code in the main function and
execute it. If your main is empty, then your program will
do nothing.
• There are essentially four parts to a function definition.
They are the return type, the function name, the parameter
list, and the function body, in that order. In this case:
–
–
–
–
return type: int
function name: main
parameter list: ()
function body: { ... }
• For now, the important thing to remember is that the
function body is the part enclosed in { ... } ("curly
braces"). The { indicates the beginning of the function, and
the } indicates the end of the function. The function body
is the stuff in between.
//print out the text string, "Hello, World!"
– Another comment line. Remember, the compiler
ignores anything following // (up until the end of the
line), so you can say whatever you want on these lines.
cout << "Hello, World!" << endl;
– This is the line that prints out the text string, "Hello,
World!". For now, don't worry about how cout works,
just know how to use it. You can print out any series of
text strings by separating them with <<. So, instead of
saying cout << "Hello, World!" << endl;, you could say
cout << "Hello, " << "World" << "!" << endl;. The endl
simply adds a carriage return (stands for "end-line").
return 0;
– This line is necessary because the return type of main is
int (see above). We'll talk more about functions and
return types later, but for now understand that because
the function's return type is int, the function must return
an int (integer). To return 0 (which is an integer, we
simply write return 0;.