Your Ad Here

THE STRUCTURE OF A C++ PROGRAM


When a programmer learns a new programming language, the first program he or she traditionally
writes is a Hello World program—a program that displays the message “Hello World”
on the screen. Creating this simple program illustrates that the language is capable of
instructing the computer to communicate with the “outside” world.



#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
return 0;
}

CODE: HELLO WORLD PROGRAM


At this point, you’re not expected to understand all the code in Figure 1-1. Just notice that the
code begins with the preprocessor directive, #include <iostream>. The C++ preprocessor
is a program that processes your C++ program before the compiler processes it. The
#include directive tells the compiler to include another file when the program is compiled.
This makes it easy for you to use code previously written by you or others in your programs
without having to recreate it. You will learn more about the Visual C++ compiler later in this
chapter. Following the #include you see <iostream>, which is the name of a header file
you want to include in this program. The iostream header file gives your program access to
what it needs to perform input and output in a C++ program. The name of the header file is
placed within angle brackets (< >). The angle brackets tell the compiler to look for this file
in a directory that is specified by the compiler you are using.

The next line (using namespace std;) instructs the compiler to use the std namespace.
You can think of a namespace as a container that holds various program elements.
The std namespace contains everything C++ programs need to use the Standard C++
library. The Standard C++ library adds functionality to the C++ programming language.
For example, this program needs to use the std namespace to have access to cout and
endl, which you see on the fifth line in Figure 1-1. Notice that this line ends with a semicolon
(;). In fact, all C++ statements end with a semicolon. The reason the previous line,
#include <iostream>, does not end with a semicolon is that it is a preprocessor
directive, not a C++ statement.



0 comments:

Post a Comment

Popular Posts

Recent posts