Translate

Introduction to C++


We now discuss the C + + programming language, which facilitates the approach to program design disciplines. Most of the C + + program here we will discuss about the information and displays the results. In this section, we will discuss examples that demonstrate how C + + program can display a message and get information from the user for processing.


C++ using the notation a little foreign to the layman or non-programmer. We now have a simple program that prints a line of text, where this program illustrates some of the key features of the C++ language, and we will discuss each line of the program in detail below. Here is the first example that we will discuss:

1 #include <iostream> // allows program to output data to the screen
2  // function main begins program execution
3
4 int main()
5
6   {
7
8  << "My First C++!\n"; // display message
9
10   return 0; // indicate that program ended successfully
11
12  } // end function main


every line that starts with / / indicates that the rest of each line is a comment. Programmers insert comments to document programs and also help people to read and understand every line of the program is written. Comments will not cause your program on a computer to perform any action when the program is executed, it will be ignored by the C + + compiler and does not cause any. An initial comment with / / is called the single-line comments as ending at the end of each line.

As for the comments of more than one line using / * and end with * /


  • #include <iostream> // allows program to output data to the screen


Is a preprocessor directive, which is a message addressed to the C + + preprocessor. Lines beginning with # are processed by the preprocessor before the program is compiled. This line tells the preprocessor to participate in the program include the contents <iostream> as input / output stream header file. This file should be included for each program will be to output data to the screen or data input from the keyboard using the flow-style input / output C + +.


  • int main()


Is part of every C + + program. Parentheses after main indicate that main is called function block program. C + + programs typically consist of one or more functions and classes. Exactly one function in each program must have a main. In the example above contains only one function. Pogram C + + begins executing the main function, even if the primary is not the first function in the program. Int is the key word in the left main and indicated that "return" is the primary integer value. We will discuss the function of depth in the next tutorial, and for now, we simply enter keywords int the left of the main course in any program.


  • << "My First C++!\n"; // display message   


Command to the computer to do the action, and to print a string of characters contained between double quotation marks. String is sometimes referred to as a string of characters, a message or a string literal. We refer to the characters between the quotation marks just as strings. Spacing characters in the string will not be ignored by the compiler, but to be followed include.

Signs << referred to as the operator, and is the stream insertion operator. When the program is executed, the value on the right of the operator, will be included in the output stream on a computer screen. Note that the operator in the direction of flow of the data stream. The characters of the right operand is usually scored exactly like what we write in between double quotes on the compiler. Note that the character \ n is not printed on the screen. Backslash (\) is called the escape character, and it shows bahwatanda backslash (\) character is "special" to be output. When a backslash (\) encountered in a series of characters, the next character will be combined with a backslash (\) to form an escape sequence. The escape sequence \ n here means new line.

We can add Some other common escape sequences as below:




  • return 0; // indicate that program ended successfully


is one of the means used to get out of the function. When a return statement is used in the primary endpoint, a value of 0 indicates that the program we have written have been stopped. Right brace,}, is the end of the main function.

No comments:

Post a Comment