Summary of C++ (1)

C

#include<stdio.h>
#include<math.h>

int main()
{
    
    
    double x, y;
    scanf("%f,&x"); // 输入一个双精度浮点数,并将其存储在变量 x 中

    if (x >= 0)
    {
    
    
        y = sqrt(x); // 如果 x 大于等于 0,则计算平方根并将结果存储在变量 y 中
    }

    if (x < 0)
    {
    
    
        y = pow(x + 1, 2) + 2.0 * x + 1.0 / x; // 如果 x 小于 0,则进行一系列数学计算,并将结果存储在变量 y 中
    }

    printf("f(%f) = %f", x, y); // 打印函数值 f(x)

    return 0;
}

C++

#include <iostream>
#include <cmath>

int main()
{
    
    
    double x, y;
    std::cout << "Enter a number: ";
    std::cin >> x;

    if (x >= 0)
    {
    
    
        y = sqrt(x);
    }
    else
    {
    
    
        y = pow(x + 1, 2) + 2.0 * x + 1.0 / x;
    }

    std::cout << "f(" << x << ") = " << y << std::endl;

    return 0;
}

local variable

A variable declared inside a function or a block of code.
Can only be used by statements inside functions or code blocks:

#include <iostream>
using namespace std;
 
int main ()
{
    
    
  // 局部变量声明
  int a, b;
  int c;
 
  // 实际初始化
  a = 10;
  b = 20;
  c = a + b;
 
  cout << c;
 
  return 0;
}

global variable

Variables defined outside all functions (usually at the top of the program).
The value of a global variable is valid for the entire life of the program

define constant

In C++, there are two simple ways of defining constants:

  • Use the #define preprocessor:
#include <iostream>
using namespace std;
 
#define LENGTH 10   
#define WIDTH  5
#define NEWLINE '\n'
 
int main()
{
    
    
 
   int area;  
   
   area = LENGTH * WIDTH;
   cout << area;
   cout << NEWLINE;
   return 0;
}
  • Use the const keyword:
#include <iostream>
using namespace std;
 
int main()
{
    
    
   const int  LENGTH = 10;
   const int  WIDTH  = 5;
   const char NEWLINE = '\n';
   int area;  
   
   area = LENGTH * WIDTH;
   cout << area;
   cout << NEWLINE;
   return 0;
}

C++ modifier type

C++ allows modifiers to be placed before the char, int, and double data types

Data type modifiers:

signed: Indicates that the variable can store negative numbers. For integer variables, signed can be omitted, because integer variables are signed types by default.

unsigned: Indicates that the variable cannot store negative numbers. For integer variables, unsigned doubles the scope of the variable.

short: Indicates that the scope of the variable is smaller than int. short int can be abbreviated as short.

long: Indicates that the scope of the variable is larger than that of int. long int can be abbreviated as long.

long long: Indicates that the scope of the variable is larger than long. New data type modifiers in C++11.

float: Indicates a single-precision floating-point number.

double: Indicates a double-precision floating-point number.

bool: Indicates Boolean type, only true and false two values.

char: Indicates the character type.

wchar_t: Indicates wide character type, which can store Unicode characters.

The modifiers signed, unsigned, long, and short can be applied to integer types, signed and unsigned can be applied to character types, and long can be applied to double precision types.

These modifiers can also be combined, and the modifiers signed and unsigned can also be prefixed to the long or short modifiers. For example: unsigned long int.

#include <iostream>
using namespace std;
 
/* 
 * 这个程序演示了有符号整数和无符号整数之间的差别
*/
int main()
{
    
    
   short int i;           // 有符号短整数
   short unsigned int j;  // 无符号短整数
 
   j = 50000;
 
   i = j;
   cout << i << " " << j;
 
   return 0;
}

Type qualifiers in C++

  • const const defines a constant, indicating that the value of the variable cannot be modified

  • The volatile modifier volatile tells that the value of the variable may be changed by factors outside the program, such as hardware or other threads

  • restrict A pointer decorated with restrict is the only way to access the object it points to. Only C99 adds a new type qualifier restrict

  • Mutable means that member variables in the class can be modified in const member functions

  • static is used to define a static variable, which means that the scope of the variable is limited to the current file or function, and will not be accessed by other files or functions

  • register is used to define a register variable, which means that the variable is frequently used and can be stored in the register of the CPU to improve the running efficiency of the program

mutable instance

class Example {
    
    
public:
    int get_value() const {
    
    
        return value_; // const 关键字表示该成员函数不会修改对象中的数据成员
    }
    void set_value(int value) const {
    
    
        value_ = value; // mutable 关键字允许在 const 成员函数中修改成员变量
    }
private:
    mutable int value_;
};

static instance

void example_function() {
    
    
    static int count = 0; // static 关键字使变量 count 存储在程序生命周期内都存在
    count++;
}

register instance

void example_function(register int num) {
    
    
    // register 关键字建议编译器将变量 num 存储在寄存器中
    // 以提高程序执行速度
    // 但是实际上是否会存储在寄存器中由编译器决定
}

Guess you like

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