Summary
A C++ program consists of one or more modules called functions. Programs begin executing at the beginning of the function called main() (all lowercase), so you should always have a function by this name. A function, in turn, consists of a header and a body. The function header tells you what kind of return value, if any, the function produces and what sort of information it expects arguments to pass to it. The function body consists of a series of C++ statements enclosed in paired braces ({}).
C++ statement types include the following:
-
Declaration statement—A declaration statement announces the name and the type of a variable used in a function.
-
Assignment statement—An assignment statement uses the assignment operator (=) to assign a value to a variable.
-
Message statement—A message statement sends a message to an object, initiating some sort of action.
-
Function call—A function call activates a function. When the called function terminates, the program returns to the statement in the calling function immediately following the function call.
-
Function prototype—A function prototype declares the return type for a function, along with the number and type of arguments the function expects.
-
Return statement—A return statement sends a value from a called function back to the calling function.
A class is a user-defined specification for a data type. This specification details how information is to be represented and also the operations that can be performed with the data. An object is an entity created according to a class prescription, just as a simple variable is an entity created according to a data type description.
C++ provides two predefined objects (cin and cout) for handling input and output. They are examples of the istream and ostream classes, which are defined in the iostream file. These classes view input and output as streams of characters. The insertion operator (<<), which is defined for the ostream class, lets you insert data into the output stream, and the extraction operator (>>), which is defined for the istream class, lets you extract information from the input stream. Both cin and cout are smart objects, capable of automatically converting information from one form to another according to the program context.
C++ can use the extensive set of C library functions. To use a library function, you should include the header file that provides the prototype for the function.
Now that you have an overall view of simple C++ programs, you can go on in the next chapters to fill in details and expand horizons.