In any language, there are some fundamentals you need to know before you can write even the most elementary programs. This chapter introduces three such fundamentals; basic program construction, variables, and input/output (1/0), It also touches on a variety of other language features, including comments, arithmetic operators, the increment operator, data conversion, and library functions
These topics are not conceptually difficult, but you may find that the style in C++ is a little austere compared with, say, BASIC or Pascal. Before you learn what it's all about, a C++ program may remind you more of a mathematics formula than a computer program. Don't worry about this. You'll find that as you gain familiarity with C++, it starts to look less forbidding while other languages begin to seem unnecessarily fancy and verbose.
As we noted in the Introduction, you can use either a Microsoft or a Borland compiler with this book. Appendixes C and D provide details about their operation. (Other compilers may work as well.) Compilers take source code and transform it into executable files, which your computer can run as it does other programs. Source files are text files (extension . CPP) that correspond with the listings printed in this book. Executable files have the .EXE extension, and can be executed either from within your compiler, or, if you're familiar with MS-DOS, directly from a DOS window.
The programs run without modification on the Microsoft compiler or in an MS-DOS window. If you're using the Borland compiler, you'll need to modify the programs slightly before running them; otherwise, t output won't remain on the screen long enough to see. Make sure to read Appendix D, "Borland C++Builder," to see how this is done.
Basic Program Construction
Let's look at a very simple C++ program. This program is called FIRST, so its source file is FIRST.CPP. It simply prints a sentence on the screen. Here it is:
#include <iostream> using namespace std;
cout << 'Hello World"<<endl;
Despite its small size, this program demonstrates a great deal about the construction of C++ programs. Let's examine it in detail.
Functions are one of the fundamental building blocks of C++. The FIRST program consists almost entirely of a single function called main(). The only parts of this program that are not part of the function are the first two lines-the ones that start with #include and use. (We'll see what these lines do in a moment.)
We noted in Chapter 1, "The Big Picture," that a function can be part of a class, in which case it is called a member function. However, functions can also exist independently of classes. We are not yet ready to talk about classes, so we will show functions that are separate standalone entities, as main() is here.
The parentheses following the word main are the distinguishing feature of a function. Without the parentheses, the compiler would think that mainly refers to a variable or to some other program element. When we discuss functions in the text, we'll follow the same convention that C++ uses: We'll put parentheses following the function name. Later on, we'll see that the parentheses aren't always empty. They're used to hold function arguments: values passed from the calling program to the function.
The word int preceding the function name indicates that this particular function has a return value of type int. Don't worry about this now; we'll learn about data types later in this chapter and return values in Chapter 5. "Functions."
Braces and the Function Body
The body of a function is surrounded by (sometimes called curly brackets). These braces play the same role as the BEGIN and END keywords in some other languages: They surround or delimit a block of program statements. Every function must use this pair of braces around the function body. In this example, there are only two statements in the function body: the line starting with cout, and the line starting with the return. However, a function body can consist of many statements.
Always Start with main()
When you run a C++ program, the first statement executed will be at the beginning of a function called main(). (At least that's true of the console mode programs in this book.) The program may consist of many functions, classes, and other program elements, but on startup. control always goes to main(). If there is no function called main() in your program, a linker error will be generated.
In most C++ programs, as we'll see later, main() calls member functions in various objects to carry out the program's real work. The main () function may also contain calls to other stand-alone functions.