Get the skills that get the hot IT projects—fast.
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


Steps

1.  You should create a base source code directory named SOURCE and then change to this directory. Next, create a work directory named FIRST and change to it.
2.  Start your text editor. In a pinch, you could use Notepad if you are running under Windows 95/NT. If you are using UNIX, you could use ed or vi. Refer to your system documentation for details.
3.  Type in the following source code, exactly as shown. Pay special attention to symbols, such as brackets and semicolons.
// filename: first.cpp - my first C++ program
#include <iostream>
using namespace std ;
int main( ) /* main function */
{    cout << “My first C++ program!” << endl ;
     return( 0 ) ;
}
4.  Save the file, naming it FIRST.CPP. Then exit the editor and return to the command line.
5.  At the command line, type the command required to run the compiler and linker. For example, if you are using Microsoft Visual C++, you would type cl first.cpp. The compiler will run and (if you typed all text correctly) return to the command line without any error messages. If the compiler does report an error, refer to the following “Comments” section.
6.  At the command prompt, type the name of the executable (called first). If you are on a UNIX system and you do not see a file named first, look for a file named a.out; if you find this filename, execute it. You should see the following message on your screen:
My first C++ program!

In the next section, “How It Works,” I will discuss how this program operates.

How It Works

Now it is time to examine the process in more detail. First, I will review the processes that are required to create a program. Then I will examine the source file itself, line by line.

The procedure to create a program is always the same. First, you create source code and save it in a file so that the compiler can parse it. You can use any text editor to type your source code. If you are using Windows, you can use Notepad or WordPad. If you must use a word processor, be sure to save the file as a pure ASCII text file. If you are using UNIX, you can use vi, emacs, or ed. I recommend that you use a dedicated programmer’s editor for serious source editing.

The next step is to type the source code to satisfy the functionality for the program. Visual style for the source layout is important for both you and others who will be maintaining your source code. A number of styles are accepted. See the following “Comments” section for examples.

After you have typed the source code and have saved the file, the compiler must be run against the source file. The compiler reads the source file, performs interpretation, and produces an object file. The object file, in its current form, cannot be executed directly.

Next, the linker must be run to produce the executable. The linker combines the object file(s), plus any required library functions and classes to produce the program. The output of the linker, if everything is successful, is an executable program. This program is now ready to run.

Now shift your attention to the source code itself and discover what each line does.

The first line in the file is identified as a comment. A comment in C++ can be denoted in one of two ways. One way, as is demonstrated in the example, consists of the two-character sequence // (two forward slashes). This style is referred to as the line comment. The compiler disregards any text beyond this sequence. The comment in this source text

// filename: first.cpp - my first C++ program

simply tells any readers that the name of this file is first.cpp and that this is your first C++ program. The second form of comment in C++ consists of two individual character sequences /*(slash-star) and */(star-slash) and is commonly referred to as a block comment. The sequence /* begins the comment and the sequence */ ends the comment. The difference between the two styles is that block comments can span many lines, whereas the line comment cannot. In addition, program statements can exist after the block comment’s end sequence. A block comment is shown in the following example:

int main( ) /* main function */

The second line of code in the file

#include <iostream>

is a preprocessor directive. The preprocessor directive is executed before the compiler and is used to perform initial processing of a source file. The #include directive instructs the preprocessor to read in the file identified within the brackets (or double quotes, if used). The file is literally inserted into the source file at the point of the directive. After all preprocessing is complete, the compiler is invoked on the resultant file. If your compiler complained about this line of code, please review the following “Comments” section for more information.

The third line in the source file

using namespace std ;

is a using directive and is used in conjunction with the namespace feature. Namespaces are used to partition the global namespace. This eliminates, or at least reduces, name conflicts. Refer to the following “Comments” section for a discussion of the using directive if your compiler complains.

The function found on the fourth line of code

int main( ) /* main function */

is the starting point for a C++ program; the main function is a requirement of every C++ program. The int is a C++ data type and designates that the return value from main is an integer value. In short, the operating system loads a program into memory and then calls the main function to start the ball rolling. The body of the main function is delineated by the opening and closing braces {}. The “Comments” section that follows discusses an optional declaration of main. Functions will be discussed in How-To 1.4.

The statement on the fifth line

cout << “My first C++ program!” << endl ;

displays a message to standard output (the screen). The insertion operator << is used to put data to the cout object. Notice that the insertion operator can be chained. In the example, the first data item sent to the cout object is a string of text, followed by the endl manipulator. (Note that the last character in endl is L, not the numeric value 1.) Finally, a semicolon is used to end the statement.

The last line of code

return( 0 ) ;


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.