Monday, November 29, 2010

Basic Program Construction of c++

Basic Program Construction

Let’s look at a very simple C++ program. This program is called FIRST, so its source file is
FIRST.CPP. It simply prints a sentence on the screen. Here it is:
#include <iostream>
using namespace std;
int main()
{
cout << “Every age has a language of its own\n”;
return 0;
}
Despite its small size, this program demonstrates a great deal about the construction of C++
programs

Functions
Functions are one of the fundamental building blocks of C++. The FIRST program consists
almost entirely of a single function called main(). The only parts of this program that are not
part of the function are the first two lines—the ones that start with #include 

                  Always Start with main()
When you run a C++ program, the first statement executed will be at the beginning of a function
called main(). (At least that’s true of the console mode programs in this book.) The program
may consist of many functions, classes, and other program elements, but on startup,
control always goes to main(). If there is no function called main() in your program, an error
will be reported when you run the program

Directives
The two lines that begin the FIRST program are directives. The first is a preprocessor directive,
and the second is a using directive. They occupy a sort of gray area: They’re not part of the
basic C++ language, but they’re necessary anyway




Preprocessor Directives
The first line of the FIRST program
#include <iostream>
might look like a program statement, but it’s not. It isn’t part of a function body and doesn’t
end with a semicolon, as program statements must. Instead, it starts with a number sign (#).
It’s called a preprocessor directive. Recall that program statements are instructions to the computer
to do something, such as adding two numbers or printing a sentence. A preprocessor
directive, on the other hand, is an instruction to the compiler. A part of the compiler called the
preprocessor deals with these directives before it begins the real compilation process.
The preprocessor directive #include tells the compiler to insert another file into your source
file. In effect, the #include directive is replaced by the contents of the file indicated. Using an
#include directive to insert another file into your source file is similar to pasting a block of
text into a document with your word processor.
#include is only one of many preprocessor directives, all of which can be identified by the initial
# sign. The use of preprocessor directives is not as common in C++ as it is in C, but we’ll
look at a few additional examples as we go along. The type file usually included by #include
is called a header file.
Header Files
In the FIRST example, the preprocessor directive #include tells the compiler to add the source
file IOSTREAM to the FIRST.CPP source file before compiling. Why do this? IOSTREAM is an example
of a header file (sometimes called an include file). It’s concerned with basic input/output
operations, and contains declarations that are needed by the cout identifier and the << operator.
Without these declarations, the compiler won’t recognize cout and will think << is being used
incorrectly. There are many such include files. The newer Standard C++ header files don’t have
a file extension, but some older header files, left over from the days of the C language, have
the extension .H.
C++ Programming Basics
2
C++
PROGRAMMING
BASICS

8 comments:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from .Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.

    ReplyDelete
  2. Great tips, many thanks for sharing. I have printed and will stick on the wall! I like this blog. Gtu online video lecture

    ReplyDelete
  3. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    rpa training in bangalore
    best rpa training in bangalore
    RPA training in bangalore
    rpa courses in bangalore

    ReplyDelete
  4. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
    AWS Training in Bangalore

    ReplyDelete
  5. Quality, beautiful home renovations Mississauga from Kitchen&Bath. Let our expert Renovation Team craft your dream home.

    ReplyDelete
  6. Thank you for your articles that you have shared with us. Hopefully you can give the article a good benefit to us. Harissa

    ReplyDelete
  7. Really it was an awesome article about JAVA, very interesting to read.You have provided an nice article,Thanks for sharing.
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete