Structure of C++ Program
C++ Programming

Structure of C++ Program

 

A program is a sequence of logical instructions that can be executed by a compiler in a computer. All programs are written to solve any problem by programming language. The best way to learn a programming starts from the root of that language. Therefore, here we will try to write our first program:

Step-1:

//  first program in C++(C Plus Plus)

# include <iostream>

using namespace std ;

int main ()

{

      cout << ” Hello C++ Programming \n” ;

      return 0;

}


Step-2:

Output: 

Hello C++ Programming

The first step shows the source code for our first program. The second step shows the result of the program once compiled and also executed. The source code writing style and way of compiling a source code (program) depends on the version of compiler and development environment installed in your computer. The previous program is the typical program that programmer trainee writes for the first time in his/her education life. Apparently the result on screen is “Hello C++ Programming”.  

The substantial things is it is contains the fundamental components that every C++ program has. Maximum person tries this easy program by reading a blog or book. We are going to look line by line at the code we have just written:


// my first program in C++

This line is used for showing comments. Any lines beginning with two slash signs (//) are considered comments. Comments lines do not have any effect on the behavior of the program. Generally, the programmer uses them to include short explanations or observations within the source code itself.


#include <iostream>

The preprocessor are directed by the hash sign (#). Generally some lines are begins with # symbol at the top of the source code. In this case the directive #include<iostream> tells the preprocessor to include the iostream standard file. The iostream file includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program.


using namespace std;

The namespace consists of all the element belongs to standard C++ library. The namespace is called by std. So, if we like to use the functionality belongs to std, we must declare the above mention expression as like:  using namespace std;

The expression must be ended with semicolon (;). It tells the C++ compiler to apply the prefix std::

After using that prefix, names will be usable those need prefixes. It allows us to use cout in place of std::cout. This makes larger programs easier to read.

int main ()

This line indicates that main function started. All C/C++ program need main() function to start the program body. C/C++ program must have a main () function and under it all the lines of source code will be enclosed by an opening and closing curly braces ({}). The required parenthesis that follows the word “main” indicates that it is a function.  The keyword int is the name of data type in C++. It stands for “integer”. It is used here to indicate the return type for the main() function.

cout << “Hello C++ Programming“;

The above line is a perfect C++ statement.  The cout represents to the standard output stream in C++ and its duty is to print/show output to the user’s screen. Here, it says to send the string “Hello C++ Programming” to the standard output stream. The single symbol << represents the C++ output operator. The characters enclosed in quotation marks “” are called string. This displays on the output device, which is usually the computer screen. It usually happened when any statement executes.  The last two characters \n represents the newline character. When the output device encounters that character, it advances to the beginning of the next line of the next on the screen. Finally, note that every program statement must end with a semicolon (;)


return 0;

Return is optional for the main () function in Standard C++. We include it here only because some compilers expect it to be included as the last line of the main () function. “Return 0” means main() will not return anything to any other function. “Return 1” means main() function will return some value according to data type mentioned in main() function prototype declaration section. Here “1” means true and “0” means false.  

Without namespace:

#include<iostream.h>                                   //cout and cin

#include<conio.h>                                           //optional(for clrscr() and getch())

void main()

{

clrscr();                                                                  //optional(clear the screen)

cout<<“hello”;

getch();                                                                //optional

}

Structure of C++ Program
Output —Structure of C++ program

C++ Program Structure -II

 In C++, we do not have strict rules on how to separate instructions(or statements) in different lines or single line. For example,

int main ()
{
cout << ” Xcnotes.com”;
return 0;
}

We also use this method

int main () { cout << “Xcnotes.com”; return 0; }

All in just one line and this would have had exactly the same meaning as the previous code. In C++, the separation between statements is specified with an ending semicolon (;) at the end of each one(end of the statement), so the separation in different code lines does not matter at all for this purpose. We can write many statements per line or write a single statement that takes many code lines. The division of code in different lines serves only to make it more legible and schematic for the humans that may read it.

Let us add an additional instruction to our first program:

// my second program in C++

#include <iostream>
using namespace std;
int main ()
{
cout << “Hello “<<endl;
cout << “Xcnotes.com”;
return 0;
}

Output:

Hello

Xcnotes.com

In this case, we performed two insertions into cout in two different statements(i.e. cout << “Hello “<<endl;
cout << “Xcnotes.com”;). Once again, the separation in different lines of code(using endl) has been done just to give greater readability to the program, since main could have been perfectly valid defined this way also:


int main () { cout << ” Hello”; cout << ” Xcnotes.com”; return 0; }

We were also free to divide the code into more lines if we considered it more convenient:

int main ()
{
cout <<
“Hello”;
cout
<< “Xcnotes.com”;
return 0;
}

And the result would again have been exactly the same as in the previous examples.

Preprocessor directives (those that begin by #(#include<iostream.h>,#include<conio.h>) are out of this general rule since they are not statements. They are lines read and processed by the preprocessor and do not produce any code by themselves. Preprocessor directives must be specified in their own line and do not have to end with a semicolon (;).

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment