Structure of C++ Program

Structure of C++ Program (Click to watch video)

If you are a beginner at c++ then  the first thing you should know is that you need a compiler for compiling and running all your c++ programs. In short a compiler while help you to see the output of your program.  If you want to see a complete list of c++ compilers then click here. I would recommend the Bloodshed dev C++ compiler to you guys, but i am not going to use this compiler in my tutorials. This is because most of the schools or colleges around the world are still using the old and outdated Turbo C++. So even i am going to use the old compiler in all my tutorials. So all my codes will work properly in Turbo C++. Some codes may give error in other compilers because the old functions are simply deprecated in the newer compilers.

Okay, now getting started with C++ programming. First few words about how you should learn programming. According to me, the best way to learn any programming language in this world is by writing a program. If you write and practice then surely you will become perfect and acquire good skills in short time. But if you keep reading the programming books without an practical work then it wont help much. So always practice, make errors, fix them by finding solutions. This way you will become a great programmer.

So we will now write our first program and then we will understand it.

So the above code is a very simple code which will help us to print Hello World text as the output. We will have a look at every part of this code and understand it. The first line is a ‘single line comment’. Any line beginning with two forward slashes (//) is a single line comment in C++ language.

Comments are ignored by the compiler, which means that they are only for the humans. We write comments to tell other human what our program is all about. And in this case i am telling you people that this is my first c++ program. We should add comments to our program to help other programmers understand our program in a better way.

On the second line we have #include <iostream.h>. Now in this line iostream is a header file. We understand that it is a header file by looking at .h file format. And #include is a pre-processor directive. Preprocessor directives give instructions to the preprocessor to do certain task or take a certain action. And in this case we are just telling the preprocessor to include an external file (iostream.h) in our program. You must be wondering why are we including this file in our program, right? So the answer is that this file actually contains some elements or functions which we are going to use in our program. We will see more about this on 5th line of the program.

Third line says void main(). Remember that if a keyword is followed by pair of parenthesis then that is a function. So on this line main is a function in C++ and void is simply the return-type of main function. We will understand functions in the functions tutorial later on. However for now what you have to understand is that main is just the starting point of your program. All program execution begins at main function. No matter how many functions are there in your program or how many functions are above the main function, You have to understand and remember that always the main function will be executed first.

Then comes the opening curly brace. The opening curly brace and closing curly brace forms a block. Remember this to understand my next tutorials. And since this block occurs after main function, it forms the body of the main function. Again don’t worry if you are finding it difficult to understand about functions, since we will understand functions completely in functions tutorial. So inside the main functions body, we have :

This is the line which helps us to print Hello World as the output. This is a very important line in this program. We will read the line from right hand side to left hand side to understand it in a better way.

  • On the right hand side there is a semicolon ( ; ). Semicolon is used to end every statement in C++. Some lines may not have a semicolon at the end just because they are not technically statements.
  • Then there is Hello World in Quotations (” “). Anything between two double quotation marks is considered as string. String is simply group of characters in C++. In this case Hello World is a string.
  • Then after we have two less than signs ( << ). These are called Insertion Operator. They simply insert things on the right hand side to the things on the left hand side.
  • And at last comes cout (Console Out). cout is the standard output stream in C++. In simple terms whatever you pass to it will be displayed on the output screen.

So the meaning of entire statement can be summed up now. The statement means we are putting the string (Hello World) into the standard output stream (cout) using the insertion operator (<<). And in this way Hello World is displayed on screen as the output.

Now the question remains, Why did we include iostream header file in our program? 
The answer is that we have used cout keyword in our program which is defined inside iostream header file.  So if we remove the 2nd line completely then a error will occur. Because the compiler does not understand the meaning of cout.

In our program the compiler approaches cout and says i dont know what is cout ! And then it searches for the meaning of cout inside our program. Then it searches inside the ‘included’ header files. If it finds the meaning then compilation is successful or else error is reported to us.

So our basic code or syntax which we will use in all our beginner programs is as follows :

To fix output issues in Turbo C++

There are 3 more lines which we should add to fix output issues in Turbo C++. These output issues can be understood in a better way by watching the video. Click here to view the video.  The issues which come in Turbo C++ are:

  • Screen is not cleared before showing new output. Hence previous output is shown along with new output.
  • Output screen disappears in less than one second.

To fix these issues add clrscr() function at the beginning of main method and getch() function at the end of main method. clrscr() function helps to clear screen every time before showing new output. And getch() function helps to hold the output window on the screen until we respond by pressing a key on keyboard. But these functions are defined in another header file named conio. So we have to add one more header file in our program just below iostream header file.

So finally our fundamental program code becomes :

So i hope this article was quite informative for you guys. Thanks for reading and visiting my site.

Leave a Reply