Learn programming

Structure of C++ Program

A C++ Program consists of three main parts, These are:


  1. Preprocessor Directives
  2. The main () Function
  3. C++ Statements


Preprocessor Directives:

The instructions that are given to the compiler before the beginning of the actual program are called preprocessor directives. These are also known as compiler directives.
These directives normally start with a number sign # and keyword "include" or "define ". Preprocessor directives are used to include header files in the program.


Header file : 

Header file is a C++ source file that contains definition of library functions . Header files are added into program at the time of compilation of the program. A header file is added if the function /object defined in it is to be used in the program.
The preprocessor directive “include” is used to add a header file into the program. The name of file is written in angle bracket < > after “# include” directive. It can also be written in double quotes.
  • C++ has a large number of header files in which library functions are defined. The header file must be included in the program before calling its functions in the program.
  • A single header file may contain a large number of built in library functions.
  • For example ,the header file iostream.h has definition of different built in input output objects and functions.

main function
  1. The “main ()” function indicates the beginning of a C++ program. 
The “main()” must be included in every C++ program. 
  • When a C++ program is executed, the control goes directly to the main() function. 

  • The statements within this function are the main body of the C++ program.
•If main() function is not included, the program is not compiled and an error message is generated .

The syntax of main() function is :
     main () 



program statement…. 


}


Basic Input/Output Statement

  1. Input Statement:  Those statements which are used to get data and then assign it to variables are known as input statements,
  2. Output Statements : Those statements which are used to receive data from the computer memory and then send it to the output devices are called output statements.

The "cout"object-output stream

The "cout" is pronounced as ‘see out’. The ’cout’ stands for console output. The console represents the computer display screen. The ‘cout’ is a c++ predefined object, it is used as an output statement to display output on the computer screen and it is a part of "iostream" header file.

Stream :flow of data from one location to another is called stream.
  1. The ‘ cout ’ is standard output stream in c++.
  2. This stream sends the output to the computer screen.



Syntax of "Cout"


cout<<constant/variable;

  • cout ‘ is name of output stream object.

<<‘ insertion operator , separates variable, constants.

  • The string constant are given in a double quotation marks.
  • numeric constants , variables and expressions are without quotation marks.



Written & Edited by: Learn C Plus Plus- (Maisam Afshar)

Previous
Next Post »