2. A simple C + + program.

Source: Internet
Author: User

Each programmer's Hello World program

// This is a small C + + program  <iostream>int  main () {    "Hello,World" <<  Std::endl;    System ("pause");     return 0 ;}

1. Notes

The symbol//starts with a comment at the end of the line. The compiler ignores comments. The reason for writing comments is: Tell the program about the information, easy to understand.

2. #include

In C + +, there are many infrastructures (well-written classes and methods). Input and output functions, for example, are part of the standard library and not part of the C + + language. The C + + language is ready to use, but to use the standard library method, you have to tell the compiler. #include <>. Between <> is known as the standard header file. How the standard library is implemented, we don't care, we just have to tell the compiler to use the standard library, #include导入头文件, you can use the classes and methods in the standard library.

3.main function

Function: Return value function name (parameter)

The main () function is the entry for the program, and all C + + programs call this function.

4.{}

The function body of the function is between {}

int Main () {    // left brace     // The statements go-here.}    // Right Brace

5. Using standard library output

" Hello, World " << Std::endl;

STD namespace name. Why do you have a namespace?  If you have 3 names that are the same as the function show (). Sld::show () std::show () Sfd::show () represents a different function in 3 namespaces, so it is not easy to confuse.

Cout a class in the standard library. The cout overloads the << operator. The statements between << after "" are routed to the standard output stream.

Endl a class in the standard library. Std::endl represents the line break.

6. Return value

return 0;

The return statement indicates the end of the current function. The return value type must be compatible with the type of the function return value, or the program will error.

7. Deep thinking

The program uses two concepts that cover C + + programming: expressions and scopes.

An expression requires a program to calculate something. The calculation produces a result that may also have some effect (the effect affects the state of the program).

3+4

The expression 3+4, which calculates a result of 7, does not produce an additional effect.

" Hello, world!. " << Std::endl

Expression std::cout << "Hello, world!" << Std::endl no results, but changes the state of the program, the window shows Hello, world!

An expression contains an operator and an operand.

In the Hello World example, Std::cout, "Hello, world!", Std::endl are all operands, and two << are operators.

The operands have their own type. A type represents the meaning of a data structure and an operation that is meaningful to that data structure.

Types usually have names. For example, C + + defines an int as the name of a type that represents an integer, and the Library defines std:: ostream As the type that provides the output based on the stream. In our program, std:: cout type is std:: ostream.

The << operator requires two operands, but we have written two << operators and three operands.

The priority of the << operator, left-to-right. So first execute std::cout << "Hello, world!". The result returned by execution is also the std::cout type. The second << operation, which is actually the return std::cout << Std::endl

Scope: In this example we see 2 scopes, namespaces, and function body {}. A temporary variable is only valid in the scope in which it is declared.

8. After-school exercises

1) Compile the Hello World program.

#include <iostream>int  main () {    "Hello,World" <<  Std::endl;    System ("pause");     return 0 ;}

2) What result does the expression 3+4 produce?

The value 7 is calculated and has no effect on the program state.

3) Compile output

This (") is a quote, and this (\) is a backslash.
#include <iostream>int  main () {    "this(\") are a quote, and this (\ \) is a Backslas H. " << Std::endl;    System ("pause");     return 0 ;}

4) Rewrite hello,world! program so that line breaks occur where spaces appear in the program.

#include <iostream>#include<cstdio>intMain () {CharC;  while((c = Std::getchar ())! ='R')    {        if(c = =' ') Std::cout<<Std::endl; Elsestd::p Utchar (c); } System ("Pause"); return 0;}

  

2. A simple C + + program.

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.