Structure and Sections of a C++ Program

A C++ program can structurally be divided into different sections. Most sections are present by convention for developers to better understand a program. But there are some, which need to be followed in the order given. Primarily, a C++ program can be divided into

  1. Documentation Section
  2. Link Section
  3. Using directive
  4. Class declaration/definition
  5. Member function definition
  6. Main function

1. Documentation Section

Documentation section is where comments about what the underlying code are written. It is not mandatory to include a documentation section but, including one help programmers and users understand the program code easily. Comments are not compiled by compiler i.e. they are ignored. So, they don't have any syntax and we can write anything in the comment section. Usually in large programs with thousands of lines of codes, comments are used to minimize confusion and increase readability.

Comments in C++ can be written in two ways. They are:

a. Single line comment: Single line comment is written within a single line after '//'. For e.g.

// This is a single line comment in C++.

b. Multiple line comment: Multiple line comment is written in multiple lines and is enclosed within '/*' and '*/'. For e.g. 

/*
This is multiple
line comment in
C++.
*/

2. Link Section

Link section is where header files required for the program are included. Header files consists of function prototypes and on program execution, these files are placed on to the file by the preprocessor. They may be predefined like iostream, cstring, cmath, etc or user-defined.

Syntax

#include <header_filename>   //predefined headers
or
#include "header_filename"   //user-defined headers

For e.g.

#include <iostream>, #include <cmath>, etc.

3. Using directive

This section allows the use of namespace. Namespace consists of data and functions which will be used in the program.

Syntax of Using directive

using namespace namespace_name;

For e.g.

using namespace std;

Note: Here, std is the standard namespace in C++.

Defining a namespace


When multiple methods in a program have same signature, the compiler has no way of knowing which method is being specified. This issue is avoided by using a Namespace. Instead of defining a standard namespace std, a user can define a new namespace himself. Defining a namespace is similar to defining a class, the only difference is namespace is not terminated using semicolon(;) like class.

Syntax of namespace

namespace namespace_name
{
    attributes;
    methods();
}

4. Class declaration/definition

In this section, classes used in the program are declared and/or defined. Body of class is enclosed by curly brackets and ends with a semicolon. Class consists of attributes and functions which are the members of that class.

Syntax

class classname
{
  private: 
    attributes;
    methods();
  public: 
    attributes;
    methods();
  protected: 
    attributes;
    methods();
};

For example

class example
{
  private:
    int a,b;
  public:
    void input();
    void displaySum();
};

5. Member function definition

Member functions can be defined inside or outside the class. If the member function is defined outside the class, we need to use class name to which the function belongs and scope resolution operator(::) before function name.

Syntax

returntype classname::function_name([argument list])
{
    body of function
}

For example

void example::input()
{
    cout <<"Enter values of a and b:";
    cin >> a >> b;
}

6. Main function

Main function is the most important function of a C++ program. This is where the program execution always begins. The main() function is compulsory in a C++ program.

Syntax

int main()
{
    statements;
    ... ... ...
}

Other parts of a C++ program except main() function are optional. There must be main() function in every C++ program.

Example

C++ program to find the sum of two numbers

#include <iostream>    /* Link Section */

using namespace std;    /* Using directive */

class findsum    /* Class definition */
{
  private:
    int a,b;
  public:
    void input();
    void sum();
};

void findsum::input()    /* Member function definition */
{
    cout << "Enter values of a and b:";
    cin >> a >> b;
}

void findsum::sum()    /* Member function definition */
{
    cout <<"Sum = "<<a+b;
}

int main()    /* Main function definition */
{
    findsum x;
    x.input();
    x.sum();
    return 0;
}

Output

Enter values of a and b:6 9
Sum = 15