Here is an example of a complete C++ program: // The C++ compiler ignores comments which start with
// double slashes like this, up to the end of the line.
/* Comments can also be written starting with a slash
followed by a star, and ending with a star followed by
a slash. As you can see, comments written in this way
can span more than one line. */
/* Programs should ALWAYS include plenty of comments! */
/* Author: Rob Miller and William Knottenbelt
Program last changed: 30th September 2001 */
/* This program prompts the user for the current year, the user's
current age, and another year. It then calculates the age
that the user was or will be in the second year entered. */
#include
using namespace std;
int main()
{
int year_now, age_now, another_year, another_age;
cout << "Enter the current year then press RETURN.\n";
cin >> year_now;
cout << "Enter your current age in years.\n";
cin >> age_now;
cout << "Enter the year for which you wish to know your age.\n";
cin >> another_year;
another_age = another_year - (year_now - age_now);
if (another_age >= 0) {
cout << "Your age in " << another_year << ": ";
cout << another_age << "\n";
} else {
cout << "You weren't even born in ";
cout << another_year << "!\n";
}
return 0;
}
This program illustrates several general features of all C++ programs. It begins (after the comment lines) with the statement
#include
This statement is called an include directive. It tells the compiler and the linker that the program will need to be linked to a library of routines that handle input from the keyboard and output to the screen (specifically the cin and cout statements that appear later). The header file "iostream" contains basic information about this library. You will learn much more about libraries of code later in this course.
After the include directive is the line:
using namespace std;
This statement is called a using directive. The latest versions of the C++ standard divide names (e.g. cin and cout) into subcollections of names called namespaces. This particular using directive says the program will be using names that have a meaning defined for them in the std namespace (in this case the iostream header defines meanings for cout and cin in the std namespace).
Some C++ compilers do not yet support namespaces. In this case you can use the older form of the include directive (that does not require a using directive, and places all names in a single global namespace):
#include
Much of the code you encounter in industry will probably be written using this older style for headers.
Because the program is short, it is easily packaged up into a single list of program statements and commands. After the include and using directives, the basic structure of the program is: int main()
{
First statement;
...
...
Last statement;
return 0;
}
All C++ programs have this basic "top-level" structure. Notice that each statement in the body of the program ends with a semicolon. In a well-designed large program, many of these statements will include references or calls to sub-programs, listed after the main program or in a separate file. These sub-programs have roughly the same outline structure as the program here, but there is always exactly one such structure called main. Again, you will learn more about sub-programs later in the course.
When at the end of the main program, the line
return 0;
means "return the value 0 to the computer's operating system to signal that the program has completed successfully". More generally, return statements signal that the particular sub-program has finished, and return a value, along with the flow of control, to the program level above. More about this later.
Our example program uses four variables:
year_now, age_now, another_year and another_age
Program variables are not like variables in mathematics. They are more like symbolic names for "pockets of computer memory" which can be used to store different values at different times during the program execution. These variables are first introduced in our program in the variable declaration int year_now, age_now, another_year, another_age;
which signals to the compiler that it should set aside enough memory to store four variables of type "int" (integer) during the rest of the program execution. Hence variables should always be declared before being used in a program. Indeed, it is considered good style and practice to declare all the variables to be used in a program or sub-program at the beginning. Variables can be one of several different types in C++, and we will discuss variables and types at some length later.
Friday, July 11, 2008
1.5 An Example C++ Program
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment