Introduction to C++


The best way to learn a programming language is by writing programs. Typically, the first program beginners write is a program called "Hello World", which simply prints "Hello World" to your computer screen. Although it is very simple, it contains all the fundamental components C++ programs have:

The left panel above shows the C++ code for this program. The right panel shows the result when the program is executed by a computer.



Let's examine this program line by line:
Line 1: // my first program in C++ Two slash signs indicate that the rest of the line is a comment inserted by the programmer but which has no effect on the behavior of the program. Programmers use them to include short explanations or observations concerning the code or program. In this case, it is a brief introductory description of the program.

Line 2: #include Lines beginning with a hash sign (#) are directives read and interpreted by what is known as the preprocessor. They are special lines interpreted before the compilation of the program itself begins. In this case, the directive #include , instructs the preprocessor to include a section of standard C++ code, known as header iostream.h, that allows to perform standard input and output operations, such as writing the output of this program (Hello World) to the screen.

Line 3: void main () This line initiates the declaration of a function. Essentially, a function is a group of code statements which are given a name: in this case, this gives the name "main" to the group of code statements that follow. The function named main is a special function in all C++ programs; it is the function called when the program is run. The execution of all C++ programs begins with the main function, regardless of where the function is actually located within the code.

Lines 4 and 6: { and } The open brace ({) at line 4 indicates the beginning of main's function definition, and the closing brace (}) at line 6, indicates its end. Everything between these braces is the function's body that defines what happens when main is called. All functions use braces to indicate the beginning and end of their definitions.

Line 5: cout << "Hello World"; This line is a C++ statement. A statement is an expression that can actually produce some effect. It is the meat of a program, specifying its actual behavior. Statements are executed in the same order that they appear within a function's body. This statement has three parts: First, cout, which identifies the standard character output device (usually, this is the computer screen). Second, the insertion operator (<<), which indicates that what follows is inserted into cout. Finally, a sentence within quotes ("Hello world"), is the content inserted into the standard output. Notice that the statement ends with a semicolon (;). This character marks the end of the statement, just as the period ends a sentence in English. All C++ statements must end with a semicolon character. One of the most common syntax errors in C++ is forgetting to end a statement with a semicolon.

5 comments:

  1. Thanks for the excellent article. this Is Excellent information for the experienced C++ and beginner/business proprietor who may be trying to spare a few bucks and learn some C++ on their own.
    By Cognex AWS Training in Chennai

    ReplyDelete
  2. Great Article! I got too much information from this post. Thanks for sharing such a helpful article.If any one who want to learn C&C++ get a free demo call on 9311002620 or visit our further website :-https://htsindia.com/Courses/embedded-systems-and-robotics/cplusplustraininginstituteinsouthdelhi

    ReplyDelete
  3. Updated. Thank you for your advice. which you have shared here . Thanks for sharing this article here .There is a lot a matter about learning a particular knowledge of aspects. Other wise If If Any one who want learn C++ & Advance contact us on 9311002620 or visit :- https://htsindia.com/Courses/embedded-systems-and-robotics/cplusplustraininginstituteinsouthdelhi

    ReplyDelete
  4. C and C++ coding skills are core skills in coding. Very tough to learn. Many fresher candidates want to learn these coding skills but remain fail to grab in-depth understanding. They may go for alternative skills that is popular also all over the world.
    Like CCNA, AWS, Azure, Google cloud, CompTIA, etc.
    Start learning CCNA course with experts in just one month.

    ReplyDelete
  5. minecraft java This particular is usually apparently essential and moreover outstanding truth along with for sure fair-minded and moreover admittedly useful My business is looking to find in advance designed for this specific useful stuffs…

    ReplyDelete

100 C Programs with Code and Output

Program 1: C Program To Read Two Numbers And Print The Sum Of Given Two Numbers. Program 2: C Program To Read Three Numbers And Prin...