Chapter 2 - Getting Started with C++

into C++

First introduce a simple C++ program that displays a message (different from the previous "Hello, world") 

#include<iostream>
int main()
{
	using namespace std;
	cout << "Come up and C++ me some time.";
	cout << endl;
	cout << "You won't regret it." << endl;
	return 0;
}

 

 Note that the file name here ends with .cpp. This is a common way for C++ programs to

Different extensions may be required in other environments

 In addition, C++ is case-sensitive, which means that uppercase letters and lowercase letters are strictly distinguished.

Analyze some knowledge of C++ programs through the above short program 

  • main function 

After removing the decoration, the basic structure of the program is as follows:

int main()
{
	statements;
	return 0;
}

These lines indicate that there is a function called main() and describe the behavior of that function.

These few lines of code constitute the definition of the function. The definition consists of two parts: the first line int main() is called the function header, and the part enclosed in curly braces is called the function body

The function header summarizes the interface between the function and the rest of the program;

A function body is a computer instruction that tells what the function should do.

In C++, each complete instruction is called a statement (the statement is the operation to be performed), and all statements end with a semicolon (please do not omit the semicolon);

The last statement is called the return statement, which ends the function;

The C++ syntax requires the definition of the main() function to begin with the function header int main(). The function header describes the interface between the function and the functions that call it.

The part in front of the function name is called the function return type, which describes the information returned by the function to its calling function;

The part in parentheses after the function name is called the formal parameter list or parameter list, which describes the information passed from the calling function to the called function

Is it possible not to use main()?

Usually a C++ program must contain a function named main(), main() is called by the startup code, and the startup code is added to the program by the compiler, which is a bridge between the program and the operating system . When running a C++ program, it usually starts from the main() function

Of course, there are some exceptions, which will not be discussed here.

  • Program Notes

There are two ways to comment, one is a single-line comment, using double slashes (//), its comment effect ends at the end of the line

The other is to include comments between the symbols /* and */, which can span multiple lines

  • C++ preprocessor and iostream files

If the program needs to use C++ input or output tools, these two lines of code must be provided:

#include<iostream>
using namespace std;

(line 2 can be replaced with other code)

The preprocessor processes the source files before the main compilation. The above program uses the #include<iostream> compilation directive, which causes the preprocessor to add the contents of the iostream file to the program (this is a typical A preprocessor action that replaces or adds text to a source file before it is compiled)

Why do you want to add the contents of the iostream file to the program?

This involves communication between the program and the outside world. The io in iostream refers to input (information entering the program) and output (information sent out of the program). In fact, the content of the iostream file will replace the line #include<iostream> in the program. The #include compilation instruction causes the content of the iostream file to be sent to the compiler along with the content of the source code file. The next stage of compilation will use this composite file .

  • head File

Files like iostream are called include files—also called header files because they are included in other files. Each header file supports a specific set of tools

  •  namespace

Namespace support is a C++ feature designed to make it easier to write large programs and programs that combine existing code from multiple vendors. If two packaged products contain a function with the same name, so that the compiler does not know which one to use when using it, namespaces allow manufacturers to package their products in a unit called a namespace, so that The name of the namespace can be used to distinguish which vendor's product it is.

If iostream is used above, it is necessary to include the namespace using namespace std to make the definition in iostream available to the program.

  • C++ output using cout

How to display the message: use the following C++ statement

cout << "Come up and C++ me some time.";

The part enclosed in double quotes is the message to be printed. In C++, a series of characters enclosed in double quotes is called a string because it is composed of several strings.

The << symbol indicates that the statement will send this string to cout; this symbol indicates the path through which information flows.

cout is a predefined object that knows how to display strings, numbers, and single strings.

cout's object properties include an insertion operator (<<), which inserts the information on its right into the stream;

 

The control character endl : endl represents an important concept: start a new line. Inserting endl into the output stream will cause the screen cursor to move to the beginning of the next line. Special symbols such as endl that have special meaning for cout are called control characters. endl is also defined in iostream and is located in the namespace std.

C++ also provides another way to wrap lines in the output, \n (\n is regarded as a character, named newline)

C++ statement

 A C++ program is a set of functions, and each function is a set of statements

Declare statements and variables

 To store an item of information in a computer, it is necessary to indicate where the information is stored and the required memory space. The method is to use the declaration statement to indicate the storage type and provide a location label

int x;

This statement provides two pieces of information: the memory required and the name of the memory location.

This statement indicates that the program needs enough storage space to store an integer. In C++, int is used to represent an integer, and the name x is used to identify the value stored in the memory unit.

 assignment statement

An assignment statement assigns a value to a storage location.

x=30;

Assign the integer 30 to the memory unit represented by the variable x; 

The symbol = is called the assignment operator, and C++ can use the assignment operator continuously 

function

C++ functions are divided into two types: those with a return value and those without a return value

Use a function that returns a value 

A function that returns a value produces a value that can be assigned to a variable or used in other expressions.

 A called function is called a called function, and a function including a function call is called a calling function. The values ​​in the parentheses are the information sent to the function, this is called passing to the function, and the values ​​sent to the function in this way are called parameters.

 

Guess you like

Origin blog.csdn.net/yangSHU21/article/details/131588755