Having learned about tokens in C++, we are now ready to write our first C++ program.

The aim is to learn the basic structure and components of a C++ program.

So what follows next is our program. It has been written and executed using the Dev C++ Compiler.

Here is a live preview of the program and its output.
Structure of a basic C++ program for CBSE 11 and 12


Let us understand each line of the program.

Any statement(s) in a program that begins with // or is enclosed inside /*    */  characters denotes a comment.

Comments are statements that are ignored by the compiler and are not executed. Their main purpose is to tell the reader what is being done in the program. Thus, they help the reader in understanding a program.

A statement that begins with // is a single line comment. For comments that extend in multiple lines, we use /*  and  */ as the starting and ending characters to enclose them.

Statements that begin with # sign are called preprocessor directives. These statements are processed before the program is compiled by the compiler.

The directive #include is used to include another file in our source program. Inside the angle brackets, we have the name of the header file which we are including in our program.

The first file  iostream. This file has been included for performing basic input/output tasks in the program. Without this file, we can not take input from the user or display the output to the user.

conio.h file denotes another header file which has been used for function getch().

We’ll talk more about header files later.

Using this statement, we are including the ‘std’ namespace in our program. A namespace is a section in which certain names are recognized.

All elements in the standard C++ library (Eg. cout and cin ) are a part of the std namespace. So that’s why we have included this statement.

This line includes a  function called main() in our program. All C++ programs have this special function.

When a program is run, control passes to the main() function. In other words, program execution begins only with this function.

‘int’ used before main() denotes that this function returns an integer after its execution.

This is the body of the main() function. The body of any function in enclosed inside curly braces { }.

We have 3 statements inside the body. Each statement ends with a semicolon( ; ).

The first statement is a cout statement.  cout is an object which corresponds to the standard output stream and ‘<<‘ is the insertion operator.

So cout<<“text\n” means that the text is inserted by ‘<<‘  operator onto cout which then displays the string ‘text’  on the output screen.

The ‘\n’ character is an escape sequence. It directs the next output onto a new line.

Next is getch() function. getch means ‘get a character’. It takes a single character as input.

Using this function, the output screen remains open until the user presses any key on the keyboard.

The last statement ‘return 0’ denotes that the main() returns a value 0 to whoever called it (Compiler or the Operating System). As we had stated above, int main() means main() returns an integer.

This is how programs work in C++. If you have any queries, let us know in comments below.



ABOUT THE AUTHOR:

Founder & Senior Editor of ChewCode.com and Editor at GizmoBolt.com. Sakshi is currently pursuing MCA. She is a foodie and has a unique love for learning computer programming languages. Follow her on Google + | Facebook | Twitter. Contact her at [email protected] for feedback and sending tips.


Tags: , , , ,

Leave A Response