Technical Point.

The Best Platform of Diploma CSE Students.

Subscribe Us

Breaking

Wednesday, May 6, 2020

Basic Program Construction

Basic Program Construction

#include
using namespace std;
int main()
{
return 0;
}


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 and using. (We’ll see what
these lines do in a moment).

A function can be part of a class, in which case it is called a member
function. However, functions can also exist independently of classes.
We are not yet ready to talk about classes, so we will show functions
that are separate standalone entities, as main() is here.


Function Name
The parentheses following the word main are the distinguishing feature
of a function. Without the parentheses the compiler would think that
main refers to a variable or to some other program element. When we
discuss functions in the text, we’ll follow the same convention that C++
uses: We’ll put parentheses following the function name. Later on we’ll
see that the parentheses aren’t always empty. They’re used to hold
function arguments: values passed from the calling program to the
function. The word int preceding the function name indicates that this
particular function has a return value of type int. Don’t worry about this
now; we’ll learn about data types and return values later.

Braces and the Function Body

The body of a function is surrounded by braces (sometimes called curly
brackets). These braces play the same role as the BEGIN and END
keywords in some other languages: They surround or delimit a block of
program statements. Every function must use this pair of braces around
the function body.

Always Start with main()

When you run a C++ program, the first statement executed will be at the
beginning of a function called main().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. In most
C++ programs, as we’ll see later, main() calls member functions in
various objects to carry out the program’s real work. The main() function
may also contain calls to other standalone functions.

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

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 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.

The using Directive

A C++ program can be divided into different namespaces. A
namespace is a part of the program in which certain names are
recognized; outside of the namespace they’re unknown. The directive
using namespace std;
says that all the program statements that follow are within the std
namespace. Various program components. If we didn’t use the using
directive, we would need to add the std name to many program

elements.

No comments:

Post a Comment

Recently

All C program

Write a C program to print ‘Hello World’ on screen . #include < stdio.h > #include < conio.h > void  m...