Structure of C++ Program - C programming language - C code - C++ Structure

structure of C++ programstructured programming - c code - c programmingdata structures and algorithms...



Structure in C++ Program

Structure of C++ Program


Structure in C++Program:

General Syntax of a normal C++ program is below:

Preprocessor directives < header file
int main () 
Body of the program 
Each C++ program has three main components. These are: Preprocessor directivesmain function and body of the main function. Now, "hello world" example to explain these components:
# include<iostream.h> 
# include<conio.h> 
int main () 
cout<<" hello world!"; 
getch (); 
Return 0; 
}

Output of the program 
Hello world! 

The result of the above program is that it prints " hello world!" on the screen. It is one of the simplest programs that can be written in C++, but it contains components of almost every C++ program.
 
Structure of C++ Program



1. Preprocessor Directives (#Include, #Define):

A processor is a collection of special statements which are executed before the compilation process. Almost every C++ program contains a processor. The #include preprocessor directives is commonly used load header files with extension (.h). In the above program two #Include directives have been used a stick food <iostream.h> and #include <conio.h>. #include <iostream.h> is used for the C++ objects cout and #include <conio.h> for the built-in function getch ().
In C++, The Other commonly used processor directive is #define which is used to define symbolic constants. Its general format is:

#define identifier value: 
For Example:
#define PI 3.14159
#define NEWLINE '\n'
This defines to new constants: PI and NEWLINE.
Once they are defined, they can be used in the rest of the program as if they were any other regular constant, for Example:

#include <iostream.h>
#include <conio.h>
#define PI 3.1459
#define NEWlINE '\n'
int main( )
{
Double R=5.0; //radius
double circle;
Circle is equal to 2*5*r;
cout<<"area of the circle:"<<circle;
cout<< NEWLINE;
getch ( );
Return 0;
}


Output of the program
area of the circle: 31.4159
The #define directive is not a C++ therefore it assumes the entire line as the directive and does not require a (;) at its end.

Structure of C++ Program


2. main( ) Function:

The main function is the point by where all C++ program start their execution. It does not matter whether there are other functions other name is defined before or after it, the

Previous
Next Post »