Structure of C++ Program

Programs written in any language follow certain sequence of statements in particular predefined order which know as structure of a program. Similarly, the structure of C++ program has various sections, namely, comments, inclusion of header file, class definition,global declaration, member functions definitions and main function.

Comments section

Comments are used in programs to increase the readability of a program. Comment part also known as documentation section. Comments are ignored by compiler, hence they do not increase the size of a program.

Like C, C++ also supports two types of comment styles:

  • Single line comment defines line-by-line descriptions.
  • Multiline comment defines multiple lines descriptions
// This is single line comment

/* This is multi-line comment
you can write as 
many line you want */

In C++ mostly single comments are used as they do not require more lines confusing programmers. However, multi line comments comes in to play when require to comment in between of executable statement, as single line comment ignores everything written after //.

Below example demonstrate single line comment using between executable statement which generate syntax error.

#include <iostream>
using namespace std;
int main()
{

int arr[5]={10, 20, 30, 40, 50};
  int i;
for (i=0; i<5; //this is for loop comment i++)

	cout<<arr[i]<<" ";

return 0;
}                  
//OUTPUT
||=== Build: Debug in cppex (compiler: GNU GCC Compiler) ===|
main.cpp||In function 'int main()':|
main.cpp|17|error: expected ')' before ';' token|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Now use below line of code in place of for loop.

for (i=0; i<5; /*inside for loop comment */i++)
//OUTPUT
10 20 30 40 50

Including Header Files

This section includes adding standard header file to the C++ Program through the preprocessor directive #include which contains declaration and definition of various built in function, classes, keywords, constants, operators and object which we might need for writing program.

#include <iostream>

These headers files contain prototype, definition and return type of library functions, data type of constants. You can look at header files at “C:\MinGW\include” or “C:\Program Files\CodeBlocks\MinGW\include”.

Namespace

Namespace is introduced in C++ to overcome difficulty of similar identifier in a same scope. A namespace allows grouping of many entities like identifiers, classes, objects, functions and tokens, under a single name. Also allowing users to create separate namespaces and thus can use similar names entities avoiding compile-time error due to identical-name conflicts.

using namespace std;

std is the standard namespace which contain definition for identifier like cout, cin, cerr and clog in it.

The keyword using technically instructing compiler to replace identifier like cout, cin with a standards define in namespace. After that it will be read as std::cout, std::cin or std::endl.

If you don’t use the std namespace compiler try to call cout or cin whose definition doesn’t exist leading to syntax error. You can also use namespace without including whole standard namespace as shown in below example.

//Example 1
#include <iostream>
using namespace std;

int main(){
cout<<"Hello world!";
}

//Example 2
#include <iostream>

int main(){
std::cout<<"Hello world!";
}

Using namespace std is bad practice example 2 is best practice for including standard namespace.

Class Declaration

Class is a user defined type used to prevent accessibility or ability make changes to certain data and functions by outside entities. A class is declared with keyword class with data and functions (also called methods) as its members.

The accessibility of members of a class is governed by the three access specifiers namely private, protected and public. By default, all members of a class are private it means only member functions or methods of the class can access the data. The public members are accessible to outside the class using object.

Note: Protected will be explained along with inheritance.

#include <iostream>
using namespace std;

class example{            // creating class with name EXAMPLE
   int a,b;              // data members
   public:              // access specifier
       void assign();  // member funtion defined outside
       int add(){     // member function or inline function
       return a+b;
       }
};
int GV = 10;      // global variable

void example::assign(){    //member function defination
    cout<<"Enter a and b values"<<endl;
    cin>>a>>b;

}

//main function
int main()        
{
    example obj;       // creating object of class
    obj.assign();      // calling function using obj
    cout<<"Addition of 3+5 = "<<obj.add()<<endl;
    cout<< "I am global variable GV = "<<GV;
    return 0;
}
//OUTPUT
Enter a and b values
3 5
Addition of 3+5 = 8
I am global variable GV = 10

Objects are instance of a class contain member variables, constants, member functions, and overloaded operators defined by the programmer. And note that using a class is not mandatory we can write program without class too like C program.

Global declaration

Global section contains variables that are available to more than one function termed as called global variables. These variable are declared outside of all the functions and before main(). This section also contain prototype of all user-defined functions.  Refer above example line no.12.

Member Functions Definition

Function inside a class is referred as member function which can access private data of that class. These functions can have their definition and declaration inside the class called as inline function and can also have declaration inside the class and definition outside with scope resolution operator(::) outside the class.

void example::assign(){
...
...
}

Member function is called by an object with dot (.)operator in main function. consider above example.

obj.assign();
}

Main Function Section

As we already studied in C, main function hold similar definition in C++ too. It is the starting point of a program, allowing us too create variables, object of the class and then use this objects to call various functions defined inside the class. This part is important in structure of C++ program as it indicate compiler from where it should start compiling.

By default, return type of main () in C++ is integer. Therefore, main () should end with the return 0 statement indicating successful execution.

int main()
{
.....
.....
}

Please inform us if anything incorrect or want to share more information about the topic in comments.

Leave a Reply

Your email address will not be published. Required fields are marked *