Chapter two begins to learn C + +

Source: Internet
Author: User

Chapter IIStart learning C + + 2.1 Main Function

The simple code is as follows:

#include <iostream>int main () {//this is my first programusing namespace Std;cout << "Hello c++!" << Endl ; return 0;}

all statements in C + + End with a semicolon (;). return 0;

2.1.1 Function header as an interface

The function header describes the interface of the function with the function that invokes it. A function header contains a function return type, a functions name, and a list of parameters.

In C + +, int main (void) and int main () are equivalent.

The int main () in C means silence on whether to accept the argument.

2.1.2 Why main cannot use a different name

C + + programs must contain a function called Main, the function named Main is the entry of the program.

There are exceptions, however. in Windows programming, the program does not use the main function as a portal.

2.1.3 C + + comments

C + + style annotations with //

C-style annotations are /*...*/, and then C + + is fused in this way. However, you should try to avoid the use of this style of comments, because such comments will require the right and left to match, prone to problems.

2.1.4 Preprocessor

The #include <iostream> in the program is a preprocessing directive, which means to include the iostream file to the beginning of the program before compiling.

2.1. Naming of 5 header files

C + + is fully integrated with the C language, but should use C + + style files as much as possible. The following table lists the old style file naming and the new style of file naming.

C + + Legacy style

end With. h

such as iostream.h

C Old Style

end With. h

such as string.h

C + + new style

Does not have a name extension

such as iostream

C New Style

No extension, plus prefix c

such as CString

2.1.6 Namespaces

In order to solve the problem that the function libraries provided by different vendors may have the same name function,C + + proposes a namespace solution. The using directive display in the preceding code specifies that you want to use the std namespace. If using directives is used, the std:: prefix (scope qualifier) is added before each function or variable.

2.2 C + + statements2.2.1 Declaration Statements

C + + is a type-strict programming language that declares a variable before using it to inform the compiler of the type of the variable, which is typically:type name, such as int i;

2.2.2 Assignment Statements

The function of an assignment statement is to provide a value for the variable. The format of the assignment statement is:name = Express; if i = i + 1;

2.2.3 Other Statements

Other statements include function-call statements, expression statements, and so on.

2.3 Functions2.3.1 Function Format

The general format of the function is

Type functionname (argumentlist) {    statements}

2.3.2 Function Declaration

If the function is called before the definition of the function body, the compiler will report an unrecognized identifier error. The reason is that the compiler cannot predict the function name in advance. The solution is to declare the function by adding a function header to the function before the call to the function. This way the compiler knows in advance that the identifier used is a function and knows the return type and parameter list of the function.

Chapter two begins to learn C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.