C++ Programming Structure

Four parts of C++ Program Structure

C++ programming structure is mostly identical to c programming except for the class concepts.

  • Part 1: Header File or Preprocessor Section and Namespace declaration
  • Part 2: Global Variables or Global Functions
  • Part 3: Class declaration
  • Part 4: Main Function of C++

Part 1: Header File or Preprocessor Section

  • In C++, iostream is a very important header file. 
  • All preprocessor directives we can declare in this part.
  • The namespace declaration is mandatory for GCC compiler. no need for turbo c compiler

Part 2: Global Part

  • We can declare global variables and functions in this part.
  • We can access these variables or functions anywhere in this program.

Part 3: Class declaration

  • It is possible to declare Class in this part. But class concepts is not mandatory in C++.Class close parenthesis should end with a semi-colon.

Part 4: Main Function

  • Main Function is mandatory in C++ like C program.
  • Every C and C++ program starts with the main function.While program executes C++ compiler automatically calls the main function.

Structure Of C++ Program

// Structure Of C++ Program

//Part 1: Header Files/Preprocessor/Namespace
#include <iostream>
using namespace std;

//Part 2: Global Part (Optional)

//Part 3: Class (Optional)

//Part 4: Main Function 
int main ()
{
  cout << "my first program in C++!";
  return 0;
}

Above program is for just for printing "my first program in C++!"

Line By Line Explanation

//Structure Of C++ Program

This first line is a comment line. The lines starting with two slash signs (//) are the line comments. Basically, these lines do not affect the functionality of the program. We can use these lines for short notes or explanations of the piece of code

#include <iostream>

In gcc compiler
#include <iostream>

In turbo c++ compiler
#include <iostream.h>

The lines starting with the hash (#), are the header files or preprocessor. We should include header files in C++ program based on the Standard or Custom Library used. In above program, the #include<iostream> or #include<iostream.h> is an iostream standard file. The iostream header file includes the declarations of the basic standard input-output library.

#include <iostream> (or) #include <iostream.h>. It may slightly vary based on C++ compilers.

int main ()

A program starts its execution from this method "main". Wherever this main method is, the main method will be executed first. "()" is used at the end as main is a method.The main method is the body of the program. The main method is mandatory for C/C++ Program. A C++ program contains only one main method.we can find the body of the main function enclosed in braces ({}).

cout << "my first program in C++!";

cout is the simplest function of the standard output stream in C++, which is used to print datatypes values on the screen.In this case, 'my first program in C++!' sequence of characters will be printed on the screen. Always cout statements end with a semicolon character (;). Not only cout, all C++ statements ends with semicolon character (;)

If executed, above program output is.

'my first program in C++!' 

return 0;

The return statement is the common way to finish the main function. Normally, a return code is for returning the program status to the compiler. If the return code is 0, it means that the program has executed as expected without any errors.

Comments In C++

C++ supports two ways to declare comments:

  • line comment or single line comment (//)
  • block comment or multi-line comment

Simple syntax for comments

// line comment
/* block comment */ 

Comments Example Program for C++

// Structure Of C++ Program - single line comment

/* Structure Of C++ Program
   with multi-line comments or block comment */

#include <iostream>
using namespace std;

int main ()
{
  cout << "my first comment program in C++!"; // prints my first comment program in C++
  return 0;
}

my first comment program in C++