Writing a C++ Program

Properly written C++ programs have a particular structure. The syntax must be correct, or the compilerwill generate error messages and not produce executable machine language. This chapter introduces C++by providing some simple example programs and associated fundamental concepts. Most of the concepts presented in this chapter are valid in many other programming languages as well. While other languages may implement the concepts using slightly different syntax, the ideas are directly transferable to other
languages like C, Java, C#, and Ada.
General Structure of a Simple C++ Program 
Listing 2.1 (simple.cpp) is one of the simplest C++ programs that does something:
 You can type the text as shown in Listing 2.1 (simple.cpp) into an editor and save it to a file named
simple.cpp. The actual name of the file is irrelevant, but the name “simple” accurately describes the nature
of this program. The extension .cpp is a common extension used for C++ source code. After creating this file with a text editor and compiling it, you can run the program. The program prints the message

 • #include <iostream>
This line is a preprocessing directive. All preprocessing directives within C++ source code begin with
a # symbol. This one directs the preprocessor to add some predefined source code to our existing
source code before the compiler begins to process it. This process is done automatically and is invisible to us.Here we want to use some parts of the iostream library, a collection precompiled C++ code that C++
programs (like ours) can use. The iostream library contains routines that handle input and output (I/O) that include functions such as printing to the display, getting user input from the keyboard, and dealing with files.
Two items used in Listing 2.1 (simple.cpp), cout and endl, are not part of the C++ language itself.
These items, along with many other things related to input and output, were developed in C++, com-
piled, and stored in the iostream library. The compiler needs to be aware of these iostream items so it can compile our program. The #include directive specifies a file, called a header, that contains the specifications for the library code. The compiler checks how we use cout and endl within our code against the specifications in the <iostream> header to ensure that we are using the library code correctly. Most of the programs we write use this #include <iostream> directive, and some programs we will write in the future will #include other headers as well.

• using namespace std;
The two items our program needs to display a message on the screen, cout and endl, have longer 
names: std::cout and std::endl. This using namespace std directive allows us to
omit the std:: prefix and use their shorter names. This directive is optional, but if we omit it, we
must use the longer names. Listing 2.2 (simple2.cpp) shows how the longer names are used. The
name std stands for “standard,” and the using namespace std line indicates that some of the
names we use in our program are part of the so-called “standard namespace.”
• int main() {
This specifies the real beginning of our program. Here we are declaring a function named main. All
C++ programs must contain this function to be executable. Details about the meaning of int and
the parentheses will appear in later chapters. More general information about functions appear in
Chapter 8 and Chapter 9.
The opening curly brace at the end of the line marks the beginning of the body of a function. The
body of a function contains the statements the function is to execute.
• cout << "This is a simple C++ program!"<< endl;
The body of our main function contains only one statement. This statement directs the executing
program to print the message This is a simple C++ program! on the screen. A statement is the
fundamental unit of execution in a C++ program. Functions contain statements that the compiler
translates into executable machine language instructions. C++ has a variety of different kinds ofstatements, and the chapters that follow explore these various kinds of statements. All statements in
C++ end with a semicolon (;). A more detailed explanation of this statement appears below.
• }
The closing curly brace marks the end of the body of a function. Both the open curly brace and closecurly brace are required for every function definition.
Share:

0 comments:

Post a Comment

Pages

Theme Support