Wednesday, January 16, 2019

Structure of a simple c++ program

in this post, you will learn about the structure of a simple c++ program. and you learn about the section which is used to make a program.

Learn about the structure of a c++ program.

A pure c++ program has included these sections:

  • Documentation section.
  • Preprocessor directives section.
  • Global declaration section.
  • Method declaration section.
  • Main function declaration.
  • Method defining section.

Documentation section:

Documentation section used in a program for only commenting. it's not important to implement this section in the program. but it helps the others to understand the program better. 
you will completely understand this section using the example shown below of the post.
In the documentation section, there is single line comment or double(multiple) comments.

How to write a single line comment:

To write single line comment in the program. just make two (backslash) [ // ] and then write your comment.

              //you are learning c++

How to write double line comment:

To write double line comment in the program, just make the one (backslash) / and then make an asterisk symbol * and then write your comment and after that close the comment using one asterisk symbol and one backslash.

                /* you are learning c++ */
Note: the comment is not including in program and it's have not any effect on the code of the program. comments are not a part of the program.

Preprocessor directives section:

In preprocessor directive section there are two sections.

  • Link section.
  • Definition section.

Link section:

In the link section, we link the header files which is used in the program.

Header files:  header files is like a function which contents the definitions of the pre-defined functions. 

                  #include <iostream.h>
                  #include "iostream.h"

what is difference between the <iostream.h> or "iostream.h".    

The difference between these two declarations is that <iostream.h> is only searchable in the current directory. but the "iostream.h" means it can be searchable in the current and other predefined directories.

Definition section:

In the definition section, we define the symbolic constant. 

                #defined num 100
Note: it means there is num variable which has a constant value 100.

Global declaration section:

In the global declaration section, we defined the variables, class, structure, and functions to use in the program. 

                declare a variable:
                int a ;

                declare a function:

                int func(int,int);

                declare a structure:

                struct student 
                       inner part;

                declare a class:

                class A;

Method declaration section:

In method declaration section we declare the class and methods to perform a special task. 

declare a class of student name:
               class student 
                  data members;
                  data functions;

Main function:

In the main function, we write the actual code. which is performed by the compiler.
How to declare the main function:

              <return type> <keyword main> 
             { // start from the opening curly brackets

              }// end at the closing curly brackets

                 int main()

                  return 0;

Note: I used to return 0 because I used return type as an integer.

Method definition section:

In method definition section we defined the methods and functions to perform the sudden task.

we declare a sum function in a class. which has a void return type and the name of the function is the sum and we pass the two integer arguments to the function and class name is marks. 
so how we define the functions sum:

       void marks:: sum(int a, int b) 

          code of the function;


Note: it's just an example. don't stick with this. you just totally understand in next chapters.

To completely understand the structure of the c++ program, see this example.

#include<iostream>  // preprocessor section(link section)

class test  //global declaration section

   int a=10;


          void disp();  //method declaration section


void test::disp()   // method definition section

     std::cout<<"\nvalue of a is:"<< a;

int main()  //main function

    test t;
    return 0; 

To execute the program I use the Dev c++ compiler. 

learn c++ programming
the output of the program

Note: all the section depends upon the conditions of the program. 

Continue learning:
"Please share this knowledge as much possible as you can and also comment me your queries and question related to this topic. "

Previous Post
Next Post

Yash is a Full Stack web developer. he always will to help others. and this approach takes him to write this page.