Structure of C++

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Structure of a C++ Program

Every C++ program must have the following:

int main(void)
{
return 0;
}

And what goes between the brackets? Firstly there is a list of declarations which tell the
compiler the variables you are going to use (more about this later). Secondly there is a list of
statements which the processor is going to carry out. Both of these are optional, so the program
above is perfectly valid, though it does absolutely nothing at all.

The definition of the C++ program can be expanded to:

int main(void)
{
optional declaration list
optional statement list
}

// my first program in C++ Hello World!

#include <iostream.h>
int main ()
{
cout << "Hello World!";
return 0;
}

The left side shows the source code for the first program, which can be saved as, for
example, helloworld.cpp. The right side shows the result of the program once compiled and
executed. To edit and compile a program depends on the compiler being used. It depends on
whether it has a Development Interface or not and on its version.

Scope of variables

All the variables that we are going to use must have been previously declared. An
important difference between the C and C++ languages, is that in C++ we can declare variables
anywhere in the source code, even between two executable sentences, and not only at the
beginning of a block of instructions which is done in C.
Global variables can be referred to anywhere in the code,
within any function, whenever it is after its declaration.

The scope of the local variables is limited to the code


level in which they are declared. If they are declared at the
beginning of a function (like in main) their scope is the whole
main function. In the example above, this means that if
another function existed in addition to main(), the local
variables declared in main could not be used in the other
function and vice versa.

In C++, the scope of a local variable is given by the block in


which it is declared (a block is a group of instructions grouped together within curly brackets {} signs). If it is declared
within a function it will be a variable with function scope, if it is declared in a loop its scope will be only the loop,
etc...

In addition to local and global scopes there exists external scope, that causes a variable to be visible not only in the
same source file but in all other files that will be linked together.

Comment (optional)

// my first program in C++

This is a comment line. All the lines beginning with two slash signs (//) are considered
comments and do not have any effect on the behavior of the program.
In C++ you can use two styles of comments:
 new style: using the // used in front of each comment line. Any text on the line after the
comment mark // is a comment, which is ignored by the compiler.
 old style: using /* to mark the beginning of a comment and */ to mark the end. Old style
comments can span over any number of lines. Any text contained between the opening
and closing comment marks is a comment and is ignored by the C++ compiler.

/* my second program in C++ Hello World! I'm a C++ program


with more comments */
#include <iostream.h>
int main ()
{
cout << "Hello World! "; // says Hello World!
cout << "I'm a C++ program"; /* says I'm a C++ program*/
return 0;
}
Preprocessor Directive
a. #include Directive

#include <header_file>

Sentences that begin with a pound sign (#) are directives for the preprocessor. They are
not executable code lines but indications for the compiler. In this case the sentence #include
<iostream.h> tells the compiler's preprocessor to include the iostream standard header file. This
specific file includes the declarations of the basic standard input-output library in C++, and it is
included because its functionality is used later in the program.

b. #define Directive

#define identifier string

The #define directive is used to define an identifier and a string that will be substituted
for the identifier each time it is encountered in the source file. A common use of #define is to
define the size of things that might change over the evolution of the program, for example,
#define size 5.

Function Prototype

A function prototype provides the compiler with the name and arguments of the function
contained in the program and must appear before the function is used.

void area(int l, int w);


int hypoteneous(int a, int b);

The Main Function

int main ()

The body of any C++ program always begins with main. Anything you write before this
statement can be regarded as "setup" code which prepares things so the program can begin.
 Every C/C++ program must have a main function.
 A C/C++ program can only have one main function in any program.
 The main keyword is case sensitive, and must be entirely in lowercase, so Main, or MAIN,
or any combination involving uppercase letters will result in an error.
 The location of main doesn't matter (so long as it is after the setup code) but
conventionally it goes right at the beginning of the program, just as in all the examples
you will see.
a. Variable Definition

A variable name is assigned to a data storage location. Your program uses


variables to store various kinds of data during program execution. A variable definition
informs the compiler of the variable name and the type of data it will hold.

b. Program Statements

The real work of the C++ program is done by its statements. C++ statements display
information on the screen, read keyboard input, perform mathematical operations, call
functions, read disk files and all other operations that a program needs to perform. A
statement is a complete direction instructing the computer to carry out some tasks.

c. Expressions

In C++, an expression is anything that evaluates to a numeric value. C++


expressions comes in levels of complexity. The simplest C++ expression consists of a single
item: a single variable, literal constant or symbolic constant.

e. Return Statement

The return 0 instruction causes the main() function finish and return the code that the
instruction is followed by, in this case 0. This it is most usual way to terminate a program that has
not found any errors during its execution. As you will see in coming examples, all C++ programs
end with a sentence similar to this.

The program has been structured in different lines in order to be more readable, but it is
not compulsory to do so. For example, instead of

int main ()
{
cout << " Hello World ";
return 0;
}

we could have written:

int main () { cout << " Hello World "; return 0; }

in just one line and this would have had exactly the same meaning.
Here is a program with some more instructions:

// my second program in C++ Hello World! I'm a C++ program


#include <iostream.h>
int main ()
{
cout << "Hello World! ";
cout << "I'm a C++ program";
return 0;
}

In this case we used the cout << method twice in two different instructions. Once again,
the separation in different lines of the code has just been done to give greater readability to the
program, since main could have been perfectly defined thus:

int main () { cout << " Hello World! "; cout << " I'm to C++ program "; return 0; }

We were also free to divide the code into more lines if we considered it convenient:

int main ()
{
cout <<
"Hello World!";
cout
<< "I'm a C++ program";
return 0;
}

You might also like