C++primer set sail again

Source: Internet
Author: User

This article describes:

    • Write a simple C + + program
    • First Glimpse input/output
    • About annotations
    • Control structure
    • Introduction to Classes
    • Reference documents
Write a simple C + + program
`int main(){    return 0;}`

1. Each C + + program contains one or more functions, and must have a name of main. A function consists of a sequence of statements that perform function functions. The operating system executes the program by calling the main function.
2. The main function executes the statement that composes itself and returns a value to the operating system.
3. The return value of the main function must be of type int, which represents an integer. The int type is a built-in type , meaning that the type is defined by the C + + language.
4. The last part of a function body function definition is a block of statements that begins with curly braces and ends in curly braces:
{
return 0;
}
The only statement in the example is return, which terminates the function.

Note the semicolon after the return statement. In C + +, most statements use semicolons as
The end tag. Semicolons are easy to ignore, and missing writes to semicolons can lead to inexplicable
The compilation error message

    1. When return takes a value (such as 0), this value is the return value of the function. return value type
      Must be the same as the return type of the function, or can be converted to the return type of the function. For the main function,
      The return type must be of type int, and 0 is of type int.

    2. In most systems,the return value of the main function is a status indicator . Return value 0 often table
      The main function is executed successfully. Any other nonzero return value has the meaning of the operating system definition.
      Typically a non-0 return value indicates that an error occurred. Each operating system has its own way of telling users
      What does the main function return

Call GNU or Microsoft compiler

Commands to invoke the C + + compiler vary by compiler and operating system, and common compilation
The GNU compiler and Microsoft Visual Studio compilers. Invoking the GNU compiler's default
The command is g++:

$ g++ prog1.cc -o prog1

The $ here is a system prompt. This command produces a prog1 or prog1.exe.
Executable file. executable files do not have a suffix under UNIX systems ; * * and under Windows,
The suffix is. exe**.

- o prog1 is the compiler parameter and the file name used to hold the executable file. If you omit-o prog1, then the compiler generates a name of a.out under the UNIX system
An executable named A.exe is generated under Windows.

The Microsoft compiler uses the command CL to invoke:

C:\directory> cl -GX prog1.cpp

The c:directory> here is the system prompt , directory is the current directory name . CL is
The command that invokes the compiler. -gx is an option that uses the command line interface compiler
The program is required.

The Microsoft compiler automatically generates an executable file with the same name as the source file, which
Executable file has an. exe suffix and has the same name as the source file. In this case, the executable file
Name is Prog1.exe.

From the command-line compiler

For example, a UNIX shell window or
Windows command Prompt window) compile the program

`$ CC prog1.cc`

1. Here CC is the compiler command name, $ denotes system prompt. The compiler outputs an executable file that we can invoke by name.
2. In our system, the compiler produces an executable file named A.exe.
3. The UNIX compiler places the executable file in a file named A.out.

Run the executable file, which can be given at a command prompt

`$ a.exe`

In UNIX systems, even in the current directory, it is sometimes necessary to specify the text
The directory where the pieces are located
$ ./a.exe

“.” The trailing slash indicates that the file is in the current directory

The way to access the return value of the main function is related to the system. Whether UNIX or the Windows system
After executing the program, an appropriate echo command must be issued. UNIX system , by typing as
The following command gets the status:

`$ echo $?`

View status under Windows system

`C:\directory> echo %ERRORLEVEL%`
First Glimpse input/output

The iostream library is based on two types named IStream and Ostream, respectively, that represent input
streams and output streams.

The standard library defines 4 IO objects:

    • CIN Standard Input
    • cout standard Output
    • Cerr standard Error
    • Clog General Information

1. When reading from CIN, data is read from the Executing Program's window, and when written to Cin, Cerr or clog, the output
Write to the same window.

2. When running a program, most operating systems provide a way to redirect input or output streams. redirects allow you to associate these streams with the selected file.

About annotations
    1. Programs are usually mixed in two types of annotations. annotation pairs are generally used for multiple lines of interpretation, while double slash annotations are often
      Used for a half-or single-line tag.

    2. Too many comments mixed with program code can make the code difficult to understand, and it is usually better to put a comment block
      Placed above the interpreted code.

    3. When the code changes, the comments should be consistent with the code. Programmers even know other forms of documentation in the system
      has expired, or trusts the comment, that it will be correct. The wrong comment is worse than no comment,
      Because it would mislead the latter.

Control structure
    1. Indentation and formatting for C + +

      • The format of C + + programs is very free, and the position of braces, indents, comments, and line breaks is usually
        The semantics of the sequence are not affected. For example, the curly brace that represents the beginning of the body of the main function can be placed in the
        The same line as the main one, or you can put it in the beginning of the next line, or put it in your favorite
        Any place. The only requirement is that it is the compiler sees right in the argument list of main
        The first non-whitespace, non-comment character after the parentheses.

      • In the selection of the format wind
        The readability of the program to make it easier to understand. Once you've chosen some kind of wind
        Use them consistently.

    2. Talk about compilation again

      1. Syntax error

      2. Type error

        • The string literal is passed to the function that should get the integer argument.
      3. declaring errors

        • Forget to use "std::" When you access the name from the standard library.
        • and misspelled identifier names because of negligence:
      4. Enter a file terminator from the keyboard

        • The operating system uses a different value as the file terminator. Under Windows system we type
          control-z--type the "Ctrl" Key and the "Z" key at the same time to enter the file terminator.
          Unix systems, including Mac os-x machines, are typically used with control-d.
Introduction to Classes

Attention:

    • For custom classes, you must make the compiler accessible and class-related definitions

    • The class type is stored in a file, and its file name is the same as the program's source file name, which consists of a file name and a two-part suffix.

    • Usually the file name and the class name defined in the header file are the same. The usual suffix is. h, but there are some programmers to use. H,. hpp or. hXX. The compiler is usually not picky about the header file name

    • dot operator : only objects with class type are applied, the left must be a class object, and the right side must be a member of that type

Reference documents
    • C + + Primer learning Note 1 Quick Start
    • C++primer Study notes (1)
    • C + + Primer Learning Notes

C++primer set sail again

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.