Page 13 - Modular_V2.0_C++_Flikpbook
P. 13

cout<< "Hello World";

                     getch();
                 }
                                                                                          DOSBox 0.74, Cpu speed: max 100%
                 Now if you run the program, the output screen will appear. To        Hello World_
                 get back to the editor, press the Enter key.
                                                                                                  Output
                    STRUCTURE OF A C++ PROGRAM

                 A program is a set of statements, where each statement has a specific meaning and purpose. To
                 understand the program better, let’s number each line of the code:

                 #include<iostream.h>                        // Line 1
                 #include<conio.h>                           // Line 2
                 void main()                                 // Line 3

                 {                                           // Line 4
                     clrscr();                               // Line 5

                     cout<<"Hello World";                    // Line 6
                     getch();                                // Line 7

                 }                                           // Line 8
                    Line 1: The # symbol indicates a preprocessor directive. It instructs the compiler to include
                    the iostream.h header file before compilation. This header file provides predefined objects
                    such as cout, which is used for displaying output. Without including iostream.h, the cout
                    object cannot be used.

                    Line 2: This  line includes  the  conio.h  header  file.  The  conio.h  file  provides  predefined
                    functions such as getch(), which is used for handling console input.
                    Line 3: This line defines the main() function, which serves as the entry point of the program.
                    Every C++ program must have a main() function, as execution starts from this function.

                    Line 4: The opening curly brace { marks the beginning of the main() function’s body.
                    Line 5:The clrscr(); function is used to clear the console screen before displaying output. This

                    ensures that any previous output does not interfere with the current program output.
                    Line 6: This line contains the cout object, which is used to print messages on the output
                    screen. The message to be displayed must be enclosed in double quotes. In this case, it prints
                    "Hello World". At the end of the line, a semicolon (;) is used as a statement terminator. In
                    C++, each statement must end with a semicolon.

                    Line 7: The getch() function is used to pause the program execution until a key is pressed.
                    This ensures that the output window remains open until the user interacts with the keyboard.
                    Line 8: The closing curly brace } marks the end of the main() function.







                                                                                                                  11
                                                                                              Introduction to C++
   8   9   10   11   12   13   14   15   16   17   18