structure of c++ program

Structure of a C++ program contains various sections. The general structure of any C++ program contains following section as given below:

cpp-program
  • Documentation:Documentation is an optional section. This is generally used to put comments for the program. Comments help to understand the programs in context of what we are developing in the program.
  • Header Files:This section includes header-files required by our C++ program. We can make our own header files and can use in the program.
  • Symbolic Constants, Global variable and functions:These sections are declared after the header files if required. Global variables can be used in the whole program and we can use functions in whole program after defining the code for the functions.
  • Class declaration:class is main building block of any C++ program. Class declaration contains the declaration of data members and functions. You will study the concept of class in later sections.
  • main() functions:main() function must be present in the every C++ program. Execution of all C++ program begins with the main() function.
  • A program that gives the idea of structure of a C++ program is given below. Don't worry if you don't understand it in this chapter you will study each section used in this program in later section.

C++ Program Structure:

/*Documentation*/

/*This is comment: Demo Program for C++ Structure*/

/*Header file inclusion*/

#include<iostream.h>

#include<conio.h>

/*global variable declaration*/

int x; float y; char z;/*global variables*/

void disp();/*global function declaration*/

/*class declaration*/

class demo

{

private:

int x,y;/*data-member declaration*/

public:

void input(int a, int b)/*member-function declaration*/

void show();

};

/*class declaration ends here*/

/*class-function definition*/

void demo::input(int a,int b)

{

x=a;

y=b;

}

/*class function definition*/

void demo::show()

{

cout<<"x="<<x<<endl;

cout<<"y="<<y<<endl;

}

/*main function definition*/

void main()

{

demo d1,d2;/*creating objects of class*/

..............................

d1.show();

int num;

show();/*local function call*/

}

/*global function definition*/

void show()

{

cout<<"in show\n";

}