Structure of c++ program


The basic elements of a program are the data declarations, functions, and comments. Let's see how these can be organized into a simple C++ program.

    Structure of c++ program

    The basic structure of a one-function program is:

    /***************************************
     * Heading comments *
     ***************************************/
     data declarations
     main()
     {
     executable statements
     return(0);
     }
    
  • The heading comments tell the programmer all about the program. The data declarations describe the data that the program is going to use.
  • Our single function is named main. The name main is special, because it is the first function called.
  • The line return (0); is used to tell the operating system (UNIX or MSDOS/Windows) that the program exited normally.

  • #include <iostream.h>
    main ()
    {
     // Tell the world hello
     cout << "Hello World\n";
     return (0);
    } 
  • iostream.h statement signals C++ that you are going to use a set of standard classes called the I/O stream classes. This is a type of data declaration.* Later you use the class cout from this package.
  • We are using the standard class cout (console out) to output the message. A standard class is a generally useful C++ object that has already been defined and put in the standard library.
  • A library is a collection of classes, functions, and data that have been grouped together for reuse
  • "Hello World" is one of the simplest C++ programs. It contains no computations, merely sending a single message to the screen. It is a starting point.