C4Swimmers Newsletter  

(Tutorial) The Anatomy of a C++ Program

Tutorial : The Anatomy of a C++ Program

C++ programs consist of classes, functions, variables, and other component parts. Most of this book 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.

In this lesson, you will 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 Lesson 1, "Getting Started," had many interesting parts. This section reviews 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>
 2:
 3: int main()
 4: {
 5:   std::cout << "Hello World!\ n";
 6:   return 0;
 7: }

Output:

Hello World!

Analysis

On the first line, the file iostream is included into the current file. Here's how that works: The first character is the # symbol, which is a signal to a program called the preprocessor. Each time you start your compiler, the preprocessor is run first. 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. The preprocessor is discussed in further detail in Lesson 15, "An Introduction to Macros and Templates," and in Lesson 29, "Tapping Further into the Preprocessor."

The command #include is a preprocessor instruction that says, "What follows is a filename. Find that file, read it, and place it 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 cause the preprocessor to look for the file iostream in the directory that holds all the include files for your compiler. The file iostream (input-output-stream) is used by cout, which assists with writing to the console. The effect of line 1 is to include the file iostream into this program as if you had typed it in yourself.

[Read more..]

Courtesy : Codeguru.com


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

post/pre increment-decrement operators

hello everyone,

recently I had seen some questions on applications of post and pre increment/decrement operators. Questions were like this

i=0,j=0;

i=i++ + ++j;

j=++i+ j++;

find output.

can anyone help me how to solve such type of questions. I have tried a lot but there is some hidden concept about such operations. I could not understand that concept.Its like the post increment operators work after the executions of statemennt therefore result of post increment is updated at the end(may be i cud not write it properly).

Can anybody help me out? Atleast suggest me a reference material.
If anybody knows the technique to solve it please help me.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.