C + + Primer Plus 6 Chapter II

Source: Internet
Author: User
Tags class definition control characters

1 //myfirst.cpp--displays a message2 3#include <iostream>//a preprocessor directive4 intMain ()//function Header5{//start of function body6     using namespaceStd//Make definitions Visible7cout <<"Come up and C + + me some time.";//message8cout << Endl;//start a new line9cout <<"You won ' t regret it!"<< Endl;//More outputTen //If The Output window closes before you can read it, One //Add the following code: A     //cout << "Press any key to continue." <<endl; -     //cin.get ();  -     return 0;//Terminate main () the}//End of function body

To explain the above code:

1, the first line of the double slash "//" represents a single line comment. Annotations are for programmers to see and help programmers quickly understand the role of code. At compile time, the compiler ignores annotations. In addition to a single-line comment, there are several lines of comments:/* *

2, the third line #include <iostream> preprocessor compilation directives. The preprocessor compiles pre-processing prior to compiler master compilation. The iostream file is imported into the source file, forming a compound file before the main compilation. Add iostream content to the source program, because cout, Endl, and so on will be used later in the program. The back will be to cout, Endl exactly what. Where iostream is called the include file--is included in other files, also called the header file--is included in the file's starting position. In C, the header file uses the extension. h, and C + + is compatible with the form of the extension of the language, C + + has no extension in the new style, specifically to view the relevant files.

3, line fourth int main () is the function header, int is the type of the function's return value, Mian is the function name. Parentheses are parameter lists, also called formal parameter lists. Null here, called an empty argument list. The five and 15th lines are two curly braces, the contents of the curly braces are the function body, the function body is the function of the specific action to be performed. return 0 in line 14th, which indicates that the function return value is a special 0.main function, is the compiler's entry function. A separate program is to have the main function, so that the compiler knows where to start execution. There are also programs that do not have a main function, such as a dynamic-link library, which is called a function library, also called a tool, to other programs. This does not require the main function.

4, the sixth row using namespace Std; This is called a using compiler directive. STD is a namespace that calls to two different packaged products in a large program, and may provide a method, which requires namespaces for the compiler to know exactly which method to call. For example, the namespace is M, N, so that the A method in M can be called through M::A (), and the A method in N is called with n::a (). And the Std namespace is special, it is the C + + standard namespace, C + + standard component classes, functions, variables are placed in this namespace, such as cout, Endl is placed in the namespace Std. With the using compiler directive, all names within the STD can be used. This is not recommended in large projects, of course. You can use this using std:: cout, using Std::endl to use cout and Endl.

5, line seventh cout << "Come up and C + + me some time."; The last semicolon represents the end of a statement. A sequence of characters enclosed in double quotation marks is called a string. The << symbol indicates that the string is sent to cout, which indicates the path to the flow of information. Cout is a predefined object that knows how to display strings, numbers, characters, and so on. The << symbol is an insert operator for cout that inserts information to the right of the symbol into the cout corresponding stream.

6, line eighth cout << Endl; Endl is a control, and inserting a endl in the output stream causes the screen cursor to move to the next line. Special symbols such as these that have special meanings to cout are called control characters. You can also use \ n to break lines. For example, cout<< "You are beautiful\n", where \ n is treated as a character, called a line break. One difference, however, is that Endl causes the screen to refresh output (output cache). and \ n will not have this effect.

1 //carrots.cpp--Food processing Program2 //uses and displays a variable3 4#include <iostream>5 6 intMain ()7 {8     using namespacestd;9     Ten     intCarrots;//declare an integer variable One      ACarrots = -;//assign a value to the variable -cout <<"I have"; -cout << carrots;//Display the value of the variable thecout <<"carrots."; -cout <<Endl; -Carrots = carrots-1;//Modify the variable -cout <<"Crunch, Crunch. Now I has"<< Carrots <<"carrots."<<Endl; +     //cin.get (); -     return 0; +}

1, this example is not much different from the example above. Talk about the 10th line int carrots; This represents a declaration statement. int indicates the data type and tells the compiler to request a memory space of a specified size. int, which represents an integer in C + +. Carrots represents a name for the requested memory space. Used to represent the value stored by the storage space.

Carrots is a variable, that is, the value can be changed.

2, line 12th carrots = 25; is an assignment statement that assigns the 25 token to the memory space stored by the carrot. There is a connection assignment concept, such as: A=b=c=3, which assigns 3 to C, assigns the value of C to B, and assigns the value of B to a. is assigned from right to left.

3, line 17th carrots = carrots-1; Represents the value of carrots minus 1 for 24 and then assigns the result of 24 to the variable carrots. 25.

4, expand Under, the following:cout<<25; and cout<< "25"; What's the difference? 25 is a number, which is stored as binary in the computer. Binary notation corresponds fully to computer memory. In memory, each unit (bit) can be set to on or off. 0 indicates off, 1 means open. Memory is usually organized in byte-bit units, with a byte containing 8 bits. Therefore, the general int type is 4 bytes, which is 32 bits. 25 There is a binary representation of 00000000 00000000 00000000 00011001. And for the string "25", is actually the character ' 2 ', ' 5 ', ' + ' three characters, a single character is a byte, that is 8 bits, "25" stored in the computer is 00000010 00000101 00000000. That's why it's all 25, because cout converts the integer 25 to a string based on the data type, and then displays it as 25. Why convert to string display? It can be understood that what is shown to a person needs to be converted to a string.

5, line 18th cout << "Crunch, Crunch. Now I have "<< carrots <<" carrots "<< endl; Use to cout splicing technology.

Class Introduction:

1, a class is a user-defined data type. A class definition describes the data format and its usage. An object is an entity created from a data format specification. For example: cout is an object of the Ostream class, and Cin is an object of the IStream class. Only these two objects are built-in objects that you can use without the user having to create them manually.

C + + Primer Plus 6 Chapter II

Related Article

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.