C++ Summary (2)

std::cout << "Count is " << count << std::endl;

std is the namespace used by standard library functions and is an abbreviation for standard (standard).

using namespace std , which declares the namespace std , and if there are symbols that do not specify a namespace, then std is used by default, so that cin, cout, vector , etc. can be used .

Assuming you don't use preprocessing using namespace std;, you need to add std::cin or std::cout.

cin is used to get user input from the console, and cout is used to output data to the console.

cin is an input stream object, cout is an output stream object, they can use >> and << respectively, because the corresponding operators are overloaded in their classes

C++ storage class

Storage classes are keywords used to define specific properties and lifetimes of variables and functions.
C++ provides the following four storage classes:

  1. auto: auto is the default storage class of C++, which is used to automatically infer the data type of the variable. Local variables defined inside functions default to auto storage class.
  2. register: register is used to define register variables, which store variables in registers for quick access. However, since modern compilers are better able to optimize variable storage and access, the use of the register keyword has become uncommon and may even be ignored.
  3. static: static is used to define static variables and static functions. A static variable persists during program execution, even out of scope. Static functions can only be used within the source file in which they are defined.
  4. extern: extern is used to declare external global variables or functions, that is, variables or functions defined in other files. With the extern keyword, we can use global variables or functions defined in other files in the current file.

These storage class keywords are used for proper storage and access control of variables and functions in C++ programs. According to needs, you can choose the appropriate storage class to define variables and functions to meet the needs of the program.

Operators:
Logical AND &&
Logical OR II
Logical NOT!

Multiplication formulas

#include<iostream>  
#include <iomanip>
using namespace std;

int main()
{
    
    
    int i, j;
    for (i = 1; i < 10; i++){
    
    
        for (j = 1; j <= i; j++){
    
    
            cout << j << " × " << i << " = " << setw(2)<<i *j << "  ";
        }
        cout << endl<<endl;
    }
}

C++ function

A function is a group of statements that together perform a task. Every C++ program has at least one function, the main function main() , and all simple programs can define other additional functions.

You can divide the code into different functions. How you divide your code into different functions is up to you, but logically, the division is usually done in terms of each function performing a specific task.

A function declaration tells the compiler the function's name, return type, and parameters. A function definition provides the actual body of the function.

The C++ standard library provides a large number of built-in functions that programs can call. For example, the function strcat() is used to concatenate two strings, and the function memcpy() is used to copy memory to another location.

There are many other names for functions, such as methods, subroutines, or programs, and so on.

A function consists of a function header and a function body .

All components of the function:

  1. Return type: A function can return a value. return_type is the data type of the value returned by the function. Some functions perform the desired operation without returning a value, in which case the return_type is the keyword void .
  2. Function Name: This is the actual name of the function. The function name and parameter list together form the function signature.
  3. Parameters: Parameters are like placeholders. When the function is called, you pass a value to the parameter, which is called the actual parameter. The parameter list includes the type, order, and number of function parameters. Parameters are optional, that is, functions may contain no parameters.
  4. Function Body: A function body contains a set of statements that define what the function does.
// 函数返回两个数中较大的那个数
 
int max(int num1, int num2) 
{
    
    
   // 局部变量声明
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}

insert image description here

Guess you like

Origin blog.csdn.net/weixin_44659309/article/details/131414808