Basic Structure of C++ Program

Basic Structure of C++ Program

In this article, we are going to show you the Basic Structure of the C++ Program i.e. How to start a new project and how to develop the first program. So here first of all let us start with the Basic Structure or skeleton of a C++ program.

Structure of the C++ Program

To write a C++ program and to execute successfully we need to follow some standard procedure that is specific to C++ and each language will have its own procedure/rules. The basic C++ program skeleton looks like this.

Basic Structure of C++ Program

This is the most basic thing, which is needed to write and execute a C++ program. There are many unanswered questions in your mind if you are new to programming. What exactly #include <iostream> means why we need it etc, what is main, etc. So let us understand what all these elements are. Let us say the project name is Program.cpp.

What is int main()?

Basically, int main is a method as the name itself suggests it is the main method or the first method which gets called when you run the C++ program. Actual program execution starts from the main() method. We are starting from the main() method. The main() method is the starting point of the C++ program. And the parenthesis ‘()’ brackets are there for the function.

What does this int?

This is called the return type of a function. We will be learning about the return types in a later article but right now as a formality you have to write it always and when you have written int there then you have to write return 0 at the end of the main function.

What is int main()?

The body of the main function contains opening curly braces ‘{‘ and closing curly braces ‘}’. The main logic of your program lies inside the body of the main function.

Why return 0?

It is an information to Operating System program terminated successfully. It is like a standard in C++ programs, it must be written. return 0;

What does it mean by this #include?

The #include is a preprocessor directive that asks the compiler to include the header file mentioned within the ‘< >’ or within the “ “ quotes. Here in the above C++ skeleton, we are requesting the compiler to include iostream header file. (Iostream stands for input/output stream, which is responsible for basic input/output operation). These iostream header files are also programs, which are already written and known to C++ compilers.

Let’s write something inside the main function. We want to give a welcome message that is we want this program to print on the screen ‘hello C++’.

So how to print something on the screen? There is a built-in object available in C++ that is ‘cout’ which means console output. The monitor is treated as a console that is the output console. Then there is something for taking input from the keyboard that is ‘cin’ that means console input. Here keyboard is treated as an input console. Let us use the ‘cout’ and print something on the monitor.

What does it mean by this #include?

Output:

Basic Structure of C++ Program

For using ‘cout’, we have to use two angular brackets that is ‘<<’. This is called the insertion operator. It looks as if inserting something in Cout. So once we inserted it would appear on the monitor. It gives the feeling that you are inserting something. So that’s why it is called the insertion object or operator.

We have to use this one for printing anything. Then here inside the double quotes, we have written “Hello C++”. This is a string or a message. Whatever the messages that you have to print, you have to give it in double-quotes.

What is cout in C++?

Cout is nothing but console output using this we can display the content in the terminal. Similar to cout there are standard streams each will be used to serve a different purpose.

  1. cin: standard input stream
  2. cerr: standard error (output) stream
  3. clog: standard logging (output) stream
Now, where do these ‘cout’ and ‘cin’ come from?

These are present inside this ‘iostream’ header file. Actually, this is a library. The library will contain the collection of built-in objects or functions that a programmer can use and easily write the program. This is provided by the C++ compiler.

When you install the compiler, you will get the libraries. There are many libraries we will learn about them in upcoming articles. So, whatever your requirement is, depending on that you can include the library in your program. So, everything from the library will be attached to your program and you can use it.

So ‘cout’ and ‘cin’ is present in iostream.h library. In some compilers, you have to write down ‘iostream.h’. If this ‘iostream’ doesn’t work in your compiler then you write ‘iostream.h’. We have used ‘cout’ in our program to print messages on the screen. One thing about cout and cin is we cannot use them directly.

What is using namespace std?

C++ supports a concept called a namespace, which will be discussed in detail in coming lectures where all the built-in functions and objects are present. std namespace contains cout, cin, and other functions. To use this in our program we are informing the compiler by saying using namespace std. There is another way of writing it.

So, we have written ‘using namespace std;’ at the beginning of our program. If we don’t write the statement ‘using namespace std;’ then we have to write ‘std::’ before the cout or cin statement. The ‘::’ is called scope resolution. So, the correct method is right down std then scope resolution and cout. For example,

What is using namespace std?

This is how we use the ‘cout’ object. Now if you’re writing a very lengthy program or bigger program and you have to use ‘cout’ many times then instead of writing it like this we will follow the above method where we write the statement ‘using namespace std;’ at the beginning of the program. So when we say that we are ‘using namespace std’ we don’t have to write ‘std::’ separately.

What does it mean by namespace?

All the built-in things are available in iostream header file that is library are grouped under one name that is ‘std’. So, for using that we have to say using namespace std.

In the next article, I will show you how to write the basic C++ Programs. Here, in this article, I try to explain the Basic Structure of the C++ Program and I hope you enjoy this Basic Structure of the C++ Program article.

Leave a Reply

Your email address will not be published.