C ++ study notes - a

1.1 Write a simple C ++ program

1.2 acquaintance O

istream and ostream iostream library comprising two basic types, respectively, the input and output streams.
Flow: Over time, the character sequence generated or consumed formula.

Standard input and output objects

Library defines four I / O objects.
cin Standard input
cout standard output
cerr standard error output warning and error messages,
general messages clog output program is running

Data is written to the stream

Output operator (<<) can accept two operands, the left side is a ostream object is the value of the right side of the object to be printed.

Read from the data stream

Operator input (>>) accepts an istream as a left operand, and an object as its right operation. Read data from a given istream, and stored in a given subject.

Use standard library name

  • Indicating the name of the prefix std :: cin, cout and endl are called defined in std namespace (namespace) in.
    All standard library names are defined in the namespace std.

Advantages: namespace can help us avoid inadvertently defined name conflict, and the use of the library conflict caused by the same name.
Cons: When using a standard library name, you must explicitly explain the name you want to use the standard namespace. For example: std :: cout, by using the scope operator (: :) to indicate the name cout is defined in the namespace std.

  • endl is known manipulator (manipylator) special value.
    Endl effect writing the current line is ended, and the content of the buffer associated with the device (buffer) of the brush device. Buffer refresh operation ensures that all output produced by the program so far are really written to the output stream.

1.3 Introduction Notes

There are two C ++ Notes: single-line and delimiters annotation.

  1. Single-line comments begin with a double slash (//), terminated by a newline.
    All current content will be on the right side of the line double slash is ignored by the compiler, it can contain any text, including additional double slash.
  2. Two delimiter (/ and /). To / start, with / end, can contain anything except * / outside.

Note can not be nested delimiter

1.4 Flow Control

General statement is executed sequentially, but programming language provides a number of different control flow statements.

1.4.1 while statement

1.4.2 for statement

1.4.3 indefinite number of read input data

while (std :: cin >> put)
use an istream as a condition when the state detecting flow effect.

  • If the stream is valid, that stream is not an error is encountered, the detection is successful.
  • Encounters end of file EOF (end-of- file), or encountered an invalid input (e.g. int read variable value is not an integer), istream the state of the object becomes invalid. Istream object is in an invalid state will make the condition is false.

1.4.4 if statement

1.5 Introduction class

Data structure (Data Structure)
C ++, the data structure to define their own definition of a class (class) by yo.
A class defines a set of operations of a type, and associated therewith.
C ++ can be defined as the use of built-in type as natural class type (class type).

To use the class, we need to focus on three things:

  1. The class name
  2. Defined location
  3. Compatible Operating

Use the header file to access classes defined by their own applications.
Traditionally, the first document according to which the name of the defined classes are named.
Suffix headers general use .h, there .H, .hhp or .hxx. The standard library header files usually without the suffix.

1.5.1 Sales_item class

Each class actually defines a new type, the type name is the name of the class. With built-in types, you can define a variable of type class.

Read Sales_item

#Include command to start the program;
the time from a standard library file contains the header, header file name should be surrounded by angle brackets (<>). Are not part of the standard library header files, the double quotation marks ( "") surrounding the.

Addition Sales_item objects

For int, and we calculate the traditional sense - the arithmetic result of the addition of two numbers.
For Sales_item objects, using a custom "and" Object - two members Sales_item objects corresponding to the addition result.

Use file redirection

Most operating systems support file redirection - a mechanism to allow us to associate with standard input and standard input file name:
$ addItems outfile
assumptions:

  • $ Operating system prompt
  • The addition procedures have been compiled executable file named additems.exe (UNIX is additem)

The above command will read from the file named infile sales record, and the results are entered into the outfile file, two files should be located in the current directory.

1.5.2 acquaintance member function

Member function (member function) function is defined as a part of the class, sometimes referred to as a method (method).

Point operator (.) Only for the object class type. Left operands which must be a class type of the object, right operands must be a member of the type name, the expression "xxx xxx member named object."

With the call operator (()) to invoke a function call operator comprising a pair of parentheses, to place arguments (argument) list (empty).
If the member function does not accept the argument, the result is call the member function itself.

1.6 bookstore

Glossary

  • Buffer (buffer) a memory area for storing data. IO facilities typically input (output) data in a buffer, the read operation and the program operation in the buffer is irrelevant. We can explicitly refresh the output buffer, in order to force the data written to the output buffer devices. By default, read cin flushes cout; non-normal program termination will refresh cout.

  • Built-in type (built-in type) defined by the language types such as int.

  • Cerr a ostream object associated standard error, is typically written to the same standard output device.
    By default, the data is not written to cerr buffer. cerr generally for outputting error messages or other content are not part of the normal output logic.

  • a clog ostream object data associated standard error.
    By default, data is written to clog buffered. clog commonly used to write the report of the implementation of the program information, stored in a log file.

Data structure (data structure), and data on a logical combination operation allowed.

EOF (end-of-file) system-specific identification indicating no more data in the file.

Original: Big Box  C ++ study notes - a


Guess you like

Origin www.cnblogs.com/chinatrump/p/11612227.html