Getting Started with C++

Structure of a C++ Program

Programs refer to a sequence of instructions or statements. These statements are what form the structure of a C++ program. Moreover, the C++ program structure divides into several sections which are namely headers, class definition, member functions definitions and main function. So, this article will let us learn about struct C++ in detail.

struct c++

It is essential to remember that C++ offers the flexibility of writing a program with or without a class and its member functions definitions. A simple C++ struct program (without a class) will include comments, namespace, headers, main() and input/output statements.

Browse more Topics Under Getting Started with C++

Comments

Comments are an essential element of a program that comes into use for increasing the readability of a program. In addition, it also helps in describing its functioning. Similarly, comments are not executable statements. Thus, they do not increase the size of a file.

C++ struct is known for supporting two comment styles-

  • Single line comment
  • Multiline comment

The use of single comments is to define line-by-line descriptions. Double slash // comes into use for representing single-line comments. Consider the statement below to understand its concept:

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

/ / An example to demonstrate

/ / single line comment

On the other hand, multiline comments come into use to define multiple lines descriptions. Further, they are represented as / * * /. For instance, consider the statement given below:

/* An example to demonstrate multiline comment */

Usually, multiline comments do not have much use in C++ because they need more space on the line. But, they come into use within the program statements where single-line comments cannot be of use. Consider the following statement for instance:

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

The compiler will ignore everything which is written after the single line comment and hence, an error occurs. Therefore, in this case, multiline comments come into use. For example, consider this statement.

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

Headers

Usually, a program includes different programming elements such as built-in functions, classes, keywords, constants, operators and more which are already defined in the standard C++ library.

For using such pre-defined elements in a program, an appropriate header must be included in the program. Moreover, the standard headers comprise information such as prototype, definition and return type of library functions, the data type of constants and more.

Consequently, programmers do not have to declare (or define) the predefined programming elements explicitly. Further, standard headers specify in the program through the preprocessor directive” #include.

Next, the file <iostream>, which is a standard file that should come with the C++ compiler, is short for input-output streams. This command comprises code for displaying and getting input from the user.

Namespace

Ever since its creation, C++ has undergone a lot of changes by the C++ Standards Committee. Similarly, Namespace is one of the new features in this language. It allows the grouping of different entities such as classes, objects, functions and a variety of C++ tokens, etc., under a single name.

Different users can form separate namespaces. Thus, they can make use of similar names of the entities. This will avoid the compile-time error that may be present because of the identical-name conflicts.

The C++ Standards Committee has rearranged the entities of the standard library under a namespace known as std. The entities of a namespace can be accessed in several ways which are as follows:

  • 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 includes, its contents include in the std namespace. Thus, all modern C++ compilers support these statements.

#include<iostream>

using namespace std;

But, some old compilers may not support these statements. In this scenario, these single statements replace the statements.

#include<iostream.h>

Main Function

The main () is a startup function that starts the execution of a c++ program. All C++ statements which are 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 the main ( ).

As soon as all the instructions in main () execute, the control passes out of main ( ), and terminates the whole program and return a value to the operating system.

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

FAQ on Structure of a C++ Program

Question 1: What is the structure of the C++ program?

Answer 1: In C++, a program divides into three sections:

  • Standard Libraries Section
  • Main Function Section
  • Function Body Section.

Question 2: What is a structure in C++ with example?

Answer 2: Structure refers to a collection of variables of various data types under a single name. Moreover, it is similar to a class in that, both hold a collection of data of various data types. For instance, one wishes to store some information about a person like their name, citizenship number, salary and more.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.