Turn Desktop View Off
by Dinesh Thakur Category: Object-oriented programming (OOP)

Programs are a sequence of instructions or statements. These statements form the structure of a C++ program. C++ program structure is divided into various sections, namely, headers, class definition, member functions definitions and main function.

                                            Structure of a C++ Program

Note that C++ provides the flexibility of writing a program with or without a class and its member functions definitions. A simple C++ program (without a class) includes comments, headers, namespace, main() and input/output statements.

Comments are a vital element of a program that is used to increase the readability of a program and to describe its functioning. Comments are not executable statements and hence, do not increase the size of a file.

                                         A Simple C++ Program

C++ supports two comment styles: single line comment and multiline comment. Single line comments are used to define line-by-line descriptions. Double slash (//) is used to represent single line comments. To understand the concept of single line comment, consider this statement.


/ / An example to demonstrate single line comment It can also be written as

/ / An example to demonstrate

/ / single line comment


Multiline comments are used to define multiple lines descriptions and are represented as / * * /. For example, consider this statement.


/* An example to demonstrate

multiline comment */


Generally, multiline comments are not used in C++ as they require more space on the line. However, they are useful within the program statements where single line comments cannot be used. For example, consider this statement.


for(int i = 0; i<10; //loop runs 10 times i++)


Compiler ignores everything written after the single line comment and hence, an error occurs. Therefore, in this case multiline comments are used. For example, consider this statement.


for(int i = 0; i<10; /*loop runs 10 times */ i++)


Headers: Generally, a program includes various programming elements like built-in functions, classes, keywords, constants, operators, etc., that are already defined in the standard C++ library. In order to use such pre-defined elements in a program, an appropriate header must be included in the program. The standard headers contain the information like prototype, definition and return type of library functions, data type of constants, etc. As a result, programmers do not need to explicitly declare (or define) the predefined programming elements.


Standard headers are specified in a program through the preprocessor directive" #include. In Figure, the iostream header is used. When the compiler processes the instruction #inc1ude<iostream>, it includes the contents of iostream in the program. This enables the programmer to use standard input, output and error facilities that are provided only through the standard streams defined in <iostream>. These standard streams process data as a stream of characters, that is, data is read and displayed in a continuous flow. The standard streams defined in <iostream> are listed here.

• cin (pronounced "see in") : It is the standard input stream that is associated with the standard input device (keyboard) and is used to take the input from users.

• cout (pronounced "see out") : It is the standard output stream that is associated with the standard output device (monitor) and is used to display the output to users.

• cerr (pronounced "see err") : It is the standard error stream that is associated with the standard error device (monitor) and is used to report errors to the users. The cerr object does not have a buffer (temporary storage area) and hence, immediately reports errors to users. '

• clog (pronounced "see log"): It is the buffered error stream that is associated with the standard error device (computer screen) and is used to report errors to users. Unlike cerr, clog reports errors to users only when the buffer is full

For many years, C++ applied C-style headers, that is, .h extension in the headers. However, the standard C++ library introduced new-style headers that include only header name. Hence, the most modem compilers do not require any extension, though they support the older .h extension. Some of C-style headers and their equivalent C++ style headers are listed in Table.

                                  C-style and C++ style header

Namespace: Since its creation, C++ has gone through many changes by the C++ Standards Committee. One of the new features added to this language is namespace. A namespace permits grouping of various entities like classes, objects, functions and various C++ tokens, etc., under a single name. Different users can create separate namespaces and thus can use similar names of the entities. This avoids compile-time error that may exist due to identical-name conflicts.

The C++ Standards Committee has rearranged the entities of the standard library under a namespace called std. In Figure, the statement using namespace std informs the compiler to include all the entities present in the namespace std. The entities of a namespace can be accessed in different ways which are listed here.


• By specifying the using directive

using namespace std;

cout<<"Hello World";

• By specifying the full member name

std: :cout<<"Hello World";

• By specifying the using declaration

using std:: cout;

cout<<"Hello World";


As soon as the new-style header is included, its contents are included in the std namespace. Thus, all the modern C++ compilers support these statements.


using namespace std;

However, some old compilers may not support these statements. In that case, the statements are replaced by this single statement.



Main Function: The main () is a startup function that starts the execution of a c++ program. All C++ statements that need to be executed are written within main ( ). The compiler executes all the instructions written within the opening and closing curly braces' {}' that enclose the body of main ( ). Once all the instructions in main () are executed, the control passes out of main ( ), terminating the entire program and returning a value to the operating system.

By default, main () in C++ returns an int value to the operating system. Therefore, main () should end with the return 0 statement. A return value zero indicates success and a non-zero value indicates failure or error.

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles