Structure of C++ Program Class 12

YouTube video

Structure of C++ Program Class 12

In this C++ video tutorial, you will learn the basic syntax and structure of C++ program with a simple example. C++ programming concepts are explained in detail. The structure of a C++ program is the following.

preprocessor directives

void main()

{

Body of main function

}

The major components in the structure of C++ program are the following.

  • preprocessor directives
  • header files
  • main() function

Related: Federal board past papers for all subjects are available with SLO based model papers for matric and intermediate.

Preprocessor Directives

Preprocessor directive is the instruction given to the compiler before the execution of actual program. It is also called compiler directive.

In C++ programming, we can use two preprocessor directives which are #include and #define. The ‘include’ preprocessor directive is used to add a header file in your C++ program. On the other hand, the ‘define’ preprocessor directive is used to specify the value of a constant variable.

The syntax of ‘include’ directive is the following.

#include <header file>

Header Files

Header files are collection of built-in functions to perform different tasks. There are many header files in C++ programming. Each header file has its own predefined functions that can be used in our programs. The header file has .h extension. Before using any built-in function in the program, you have to add its header file otherwise error will be generated by the C++ compiler.

Header files can be included in your C++ program in the following way.

 #include <iostream.h>

#include <conio.h>

#include <math.h>

The main() Function

The main() function is the default function of C++ program. It is essential part of C++ program. If main function is missing then program will generate an error. If you define multiple functions in a program then main() function will execute first among them.

The main function consists of a return type, name of function i.e. main and arguments. the last part of main function contains body of main function that may have one or more C++ statements.

If main function does not return a value, then it is said to be void otherwise some return type is specified like int or float.

Example

#include <iostream.h>

void main()

{

cout<< “Information Technology”;

}

#include <iostream.h>

In this statement, #include is the preprocessor directive which is used to add a header file i.e. <iostream.h>. This header file is included in this program because it contains cin and cout objects. If we don’t include this header file then we cannot use these statements and the program will generate an error.

void main()

In this line of code, void is used because this function does not return a value therefore void is used. void is followed by the name of main function and parentheses i.e. (). We can use void inside parentheses if there are no arguments or leave it blank.

Function Delimiters { }

A set of curly braces {} are used to specify the start and end of main function. They are called function delimiters. There are many other kinds of delimiters like character delimiters (‘’), string delimiters (“”) and so on.

cout<< “Information Technology”;

The cout is the standard output statement which is used to display output on the screen. The cout is follow by insertion operators (<<) which indicates that the string is sent to the cout which will display it on the screen. The double quotation marks are used to write a character string which will display as it is on the screen without printing string delimiters i.e. “”. In the end, statement terminator symbol is used which is denoted by a semicolon (;). It indicates the termination of a C++ statement in the program.

Program Output

The output of this program will show a single line written inside the double quotation marks. The program will show output in the following format.

Information Technology

Similar Posts