Coupon Accepted Successfully!


Structure of a C++ Program

C++ programs consist of objects, classes, functions, variables, and other statements. Figure shows four different parts of a C++ program.

Description: 107337.png

Parts of C++ program

  1. Preprocessor directives (Include header files section):
    Preprocessing refers to the first step in compiling a C++ file from a source language into machine language. One of the most important features of C++ language is to offer preprocessor directives. The preprocessor directives are to be included at the beginning of the program before the main(). It begins with symbol# (hash). It can be placed anywhere, but quite often, it is declared at the beginning before the main() function or any particular function. In traditional C, # (hash) must begin at the first column.
    The following is a preprocessor directive that can be used in C++ programs besides many more.
    The #define directive
    For example
    #define PI 3.14
    This statement defines macro templates. During preprocessing, the preprocessor replaces every occurrence of PI (identifier) with 3.14 (substitute value). Here, PI is a macro template and 3.14 is its macro expansion. The macro templates are generally declared with capital letters for quick identification. One can also define macros with small letters. The macro templates and its expansions must be separated with at least one blank space. It is not necessary to provide space between # and define. It is optional to the programmer. To increase readability, the programmer should provide space.
    Include header files section
    One can write modular programs by making use of the #include directives. It is used to read the content of file that is included at the beginning of the program. Another file can be included at the start of the program with #include name_of_the_file.cpp.
    The C++ program also depends on some header files for function prototypes. They are used in the program. Each header file is to be extended with a .h extension. The file should be included using #include directive as per the format given below.
    Example: #include<iostream.h> or #include“iostream.h”
    In this example, <iostream.h> header file is included at the beginning by the programmer. This preprocessor directive is essential, which uses input/output statements such cin and cout. All the definitions and prototypes of the function defined in this file are available in the current program. This file also gets compiled with the original program.
    Besides the <iostream.h> header file, numerous other header files are used in programs in different chapters. Details of such header files are not given, which is beyond the scope of this discussion.
  2. Class declaration or definition: Declaration of class is done in this section. In class definition, prototype or definitions of function are also declared.
  3. Class function definitions: This part contains definition of functions. The definition of function can also be written outside the class but before main(). The outside definitions of function should need class name and scope access operator before the function name.
  4. The main() function: Like C, C++ programs also start with the main() function. The execution of every program starts with the main() function. The programming rules for C++ are the same as C. However, there are a few differences that are discussed at appropriate places.

Test Your Skills Now!
Take a Quiz now
Reviewer Name