Chapter One Knowledge Points
1. Getting to know C++
1.1 Function
① C++ can contain one or more functions, but there must be one named main function
②The main function must return int type (built-in type/type defined by the language)
③ If the type of a variable named v is T, then V has type T, or V is a variable of type T.
1.2 Source file naming
The source file ends with a suffix. The suffix consists of a period plus one or more characters, such as .cpp, .cc, .cp, .cxx and .c, etc.
The code suffix of visual studio 2019 is .cpp
1.2.1 Return value
In 2019, the return value of 0 or -1 will not affect the output of the final program, and there is no compilation error.
2. Input and output
2.1 iostream database
Often used in the primer C ++ iostream database, which contains two basic types of data, respectively, the input stream (the istream) and the output stream (the ostream) ; a stream is a sequence of characters
2.1.1 Four basic IO objects
The standard library defines four IO object
istream types: ① cin (standard input)
ostream type:
② cout (standard output)
③ cerr (standard error-output warning and message error)
④ clog (output general information when the program is running)
2.1.2 Comparison of C and C++ header files
The header file in c++ is iostream, and in C language it is stdio.h (input and output of standard library functions)
2.2 Input/output operators
The output operator << , the left side must be an otream object, and the right side is the output value
Input operator >> , the left side is the istream object, and the right side is the input operand object
2.2.1 Separate output
std::cout << "Enter the number!"`
Output: Enter the number!
2.2.2 Input/output mixed use
std::cout << "Enter the number!" << std::endl;
std::cin >> v1 >> v2;
Equivalent to
( std::cout << "Enter the number!") << std::endl;
(std::cin >> v1 )>> v2;
Equivalent to
std::cout << "Enter the number!"
std::cout<< std::endl;
std::cin >> v1;
std::cin >> v2;
2.2.3 Long code output format
std::cout << "The sum of" << v1 << "and"
<< v2 << "is"
<< v1 + v2 << std::endl;
[ Note ]
If the output is not over, do not add a semicolon
If the output code is too long, it can be output in a newline, but you cannot add a semicolon at the end of each newline
2.3 String literal constant
String literal constant -enclosed in double quotes, the text in the middle is printed to standard output (cout)
When outputting a string literal constant, add "" (double quotation mark) to output, and the double quotation mark will automatically disappear in the output column when outputting
When outputting int or other types, the output is not enclosed in double quotation marks.
2.4 Scope operator
(::) One of the functions of the operator: access to the name of the namespace
3. The endl operator
3.1 Endl definition
Not end1(❌)
endl is the C ++ standard library manipulator (Manipulator)
Contained in: iostream
The namespace (namespace) is: std
It is mainly used with iostream objects, such as cout, cerr, etc.
3.2 endl function
endl has two functions: line break and refresh output stream
endl effect is the end of the current line, and all the output so far are really written to the output stream, rather than remain in memory waiting to be written stream in
endl will constantly refresh the output stream. Frequent operations will reduce the running efficiency of the program . This is also the reason why the C++ standard library uses buffers for stream input/output operations.
3.2.1 Comparison\nand endl
When there is no need to refresh the output stream, use \n as much as possible . For example, for an unbuffered stream cerr, you can use it directly.
3.2.2 Note for use
In the program when debugging print statements, such statements must be guaranteed "has been" refreshed flow. Otherwise, when the program crashes, the output buffer area also led us to find a program error when misjudgment.
4. Class
4.1 Class name and file name
① The header files belonging to the standard library are represented by <> for the header file name (such as iostream) and
the header files not belonging to the standard library are represented by "" .
②If the class name is state, the header file name is often state.h or the suffix is .H .hpp .hxx
The standard library file name does not need a suffix.
4.2 Member functions
Member functions (also called methods ) are part of the function of the class
If you define int type item1, and isbn belongs to a member function
There are: item1.isbn()
[isbn member of the object named item1]
4.2.1 Dot operator (.)
Left: Object of class type
Right: Name of a member function of the corresponding type
Result: The member specified by the operand on the right [If isbn represents a bibliography, the result is the corresponding bibliography]