Chp2 - C PROGRAMMING • Chapter 2 • The Parts of a C Program • A Simple Program • Listing 2.1 HELLO.CPP demonstrates the parts of a C

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: C++ PROGRAMMING • Chapter 2 • The Parts of a C++ Program • A Simple Program • Listing 2.1. HELLO.CPP demonstrates the parts of a C++ program . • A Brief Look at cout • Listing 2.2. • Using cout. • Comments • Types of Comments • Using Comments • Listing 2.3. HELP.CPP demonstrates comments . • Comments at the Top of Each File • A Final Word of Caution About Comments • Functions • Listing 2.4. Demonstrating a call to a function . • Using Functions • Listing 2.5. FUNC.CPP demonstrates a simple function . • Summary • Q&A • Quiz • Exercises Chapter 2 The Parts of a C++ Program C++ programs consist of objects, functions, variables, and other component parts. Most of this guide is devoted to explaining these parts in depth, but to get a sense of how a program fits together you must see a complete working program. ToChapter you learn • The parts of a C++ program. • How the parts work together. • What a function is and what it does. A Simple Program Even the simple program HELLO.CPP from Chapter 1, "Getting Started," had many interesting parts. This section will review this program in more detail. Listing 2.1 reproduces the original version of HELLO.CPP for your convenience. Listing 2.1. HELLO.CPP demonstrates the parts of a C++ program. 1: #include <iostream.h> 2: 3: int main() 4: { 5: cout << "Hello World!\n"; 6: return 0; 7: } Hello World! On line 1, the file iostream.h is included in the file. The first character is the # symbol, which is a signal to the preprocessor. Each time you start your compiler, the preprocessor is run. The preprocessor reads through your source code, looking for lines that begin with the pound symbol (#), and acts on those lines before the compiler runs. include is a preprocessor instruction that says, "What follows is a filename. Find that file and read it in right here." The angle brackets around the filename tell the preprocessor to look in all the usual places for this file. If your compiler is set up correctly, the angle brackets will cause the preprocessor to look for the file iostream.h in the directory that holds all the H files for your compiler. The file iostream.h (Input-Output-Stream) is used by cout, which assists with writing to the screen. The effect of line 1 is to include the file iostream.h into this program as if you had typed it in yourself. New Term: The preprocessor runs before your compiler each time the compiler is invoked. The preprocessor translates any line that begins with a pound symbol (#) into a special command, getting your code file ready for the compiler. Line 3 begins the actual program with a function named main(). Every C++ program has a main() function. In general, a function is a block of code that performs one or more actions....
View Full Document

This note was uploaded on 01/13/2012 for the course CS 131 taught by Professor Clayton during the Spring '08 term at Bethune Cookman.

Page1 / 12

Chp2 - C PROGRAMMING • Chapter 2 • The Parts of a C Program • A Simple Program • Listing 2.1 HELLO.CPP demonstrates the parts of a C

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online