Explain the use of the addition and assignment operators in the C++ language

  • 2020-05-07 20:10:47
  • OfStack

addition operators: + and -
grammar


expression + expression 
expression  �  expression

note
The add operator is:

Plus (+) Reduction (�)

These 2 - base operators have left - to - right associations.

The addition operator takes the arithmetic or pointer type of operand. The result of the addition (+) operator is the sum of the operands. The result of the subtraction operator is the difference between the operands. If one operand is a pointer or both operands are Pointers, they must be Pointers to an object, not to a function. If both operands are Pointers, the result is meaningless, unless they are Pointers to objects in the same array.
The addition operators take the operands of types arithmetic, integral, and scalar. The following table defines these operands.
The type used for the addition operator

S.N. 构造函数 & 描述
arithmetic 整型和浮点类型统称为“算术”类型。
integral 所有大小(long、short)和枚举数的 char 和 int 类型为“整数”类型。
scalar 标量操作数是算术类型或指针类型的操作数。

The legal combination of these operators is:

Arithmetic + arithmetic Scalar + integer Integer + scalar Arithmetic with arithmetic Scalar scalar

Note that addition and subtraction are not equivalent operations.


// expre_Additive_Operators.cpp
// compile with: /EHsc
#include <iostream>
#define SIZE 5
using namespace std;
int main() {
  int i = 5, j = 10;
  int n[SIZE] = { 0, 1, 2, 3, 4 };
  cout << "5 + 10 = " << i + j << endl
     << "5 - 10 = " << i - j << endl;

  // use pointer arithmetic on array

  cout << "n[3] = " << *( n + 3 ) << endl;
}

Pointer addition
In an addition operation, if one of the operands is a pointer to an array of objects, the other must be an integer. The result is a pointer of the same type as the original pointer and a pointer to another array element. The following code snippet illustrates this concept:
short IntArray[10]; // Objects of type short occupy 2 bytes
short *pIntArray = IntArray;


for( int i = 0; i < 10; ++i )
{
  *pIntArray = i;
  cout << *pIntArray << "\n";
  pIntArray = pIntArray + 1;
}

Although adding the integer value 1 to pIntArray does not mean "adding 1 to the address", it means "adjusting the pointer to point to the next object in the array", which happens to be outside of 2 bytes (or sizeof(int)).
Pay attention to
pIntArray = pIntArray + 1 code is rarely found in C++ programs; For incrementing, the following form is preferred: pIntArray++ or pIntArray += 1.

Pointer to the subtraction
If both operands are Pointers, the result of the subtraction operation is the difference between the two operands (in the array element). The subtraction expression produces signed integer results of type ptrdiff_t (defined in the standard include file STDDEF.H).
One of the operands can be an integer if the operand is the second operand. The result of subtraction is of the same type as the original pointer. The value of subtraction is a pointer to the element in the (n and i) array, where n is the element pointed to by the original pointer, and i is the integer value of the second operand.

assignment operator

grammar


    expression assignment-operator expression 
assignment-operator : one of
  =  *=  /=  %=  +=   � =  <<=  >>=  &=  ^=  |=

note
The assignment operator stores a value in an object specified by the left operand. There are two kinds of assignment operations: simple assignment, in which the value of the second operand is stored in the object specified by the first operand; A compound assignment in which an arithmetic, shift, or bit operation is performed before the result is stored. All the other assignment operators in the following table, except the = operator, are compound assignment operators.
Assignment operator

运算符 含义
= 整型和浮点类型统称为“算术”类型。
*= 所有大小(long、short)和枚举数的 char 和 int 类型为“整数”类型。
/= 标量操作数是算术类型或指针类型的操作数。
%= 整型和浮点类型统称为“算术”类型。
+= 所有大小(long、short)和枚举数的 char 和 int 类型为“整数”类型。
�= 标量操作数是算术类型或指针类型的操作数。
<<= 将第1个操作数的值按第2个操作数的值指定的位数左移;将结果存储在第1个操作数指定的对象中。
>>= 将第1个操作数的值按第2个操作数的值指定的位数右移;将结果存储在第1个操作数指定的对象中。
&= 获取第1个和第2个操作数的按位“与”;将结果存储在第1个操作数指定的对象中。
^= 获取第1个和第2个操作数的按位“异或”;将结果存储在第1个操作数指定的对象中。
|= 获取第1个和第2个操作数的按位“与或”;将结果存储在第1个操作数指定的对象中。

operator keyword
three compound assignment operators have text equivalents. They are:

运算符 等效
&= and_eq
|= or_eq
^= xor_eq

There are two ways to access these operator keywords in your program: include the header file iso646.h or compile with the /Za (disabled language extension) compiler option.


// expre_Assignment_Operators.cpp
// compile with: /EHsc
// Demonstrate assignment operators
#include <iostream>
using namespace std;
int main() {
  int a = 3, b = 6, c = 10, d = 0xAAAA, e = 0x5555;

  a += b;   // a is 9
  b %= a;   // b is 6
  c >>= 1;   // c is 5
  d |= e;   // Bitwise--d is 0xFFFF

  cout << "a = 3, b = 6, c = 10, d = 0xAAAA, e = 0x5555" << endl
     << "a += b yields " << a << endl
     << "b %= a yields " << b << endl
     << "c >>= 1 yields " << c << endl
     << "d |= e yields " << hex << d << endl;
}

Simple assignment
The simple assignment operator (=) causes the value of the second operand to be stored in the object specified by the first operand. If both objects are arithmetic types, the correct operand is converted to the left type before the value is stored.
Objects of constant and mutable types can be assigned either to the left of a mutable type or to the left of a type that is neither constant nor mutable.
Assignments to objects of class types (structs, unions, and class types) are performed by a function named operator=. The default behavior of this operator function value is to perform bitwise replication; However, this behavior can be modified using the overloaded operator. (see overloading operators for more information.)
Any object of a class explicitly derived from a given base class can be assigned to an object of the base class. The reverse is not true, because there is an implicit conversion that can be converted from a derived class to a base class, but not from a base class to a derived class. Such as:


// expre_SimpleAssignment.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
class ABase
{
public:
  ABase() { cout << "constructing ABase\n"; }
};

class ADerived : public ABase
{
public:
  ADerived() { cout << "constructing ADerived\n"; }
};

int main()
{
  ABase aBase;
  ADerived aDerived;

  aBase = aDerived; // OK
  aDerived = aBase; // C2679
}

An assignment to a reference type behaves like an assignment to the object to which the reference refers.
For class-type objects, assignment is not the same as initialization. To demonstrate how different assignments and initializations work, consider the following code


UserType1 A;
UserType2 B = A;

The above code shows an initializer; It calls the constructor of UserType1 with arguments of type UserType2. Given the following code


UserType1 A;
UserType2 B;

B = A;

Assignment statement


B = A; 

May have the following effect 1:
The function operator= is called for UserType2, provided that operator= provides the UserType1 argument.
The function UserType1::operator UserType2 is called if an explicit conversion exists.
Call the constructor UserType1 that takes the UserType2::UserType2 argument and copies the result, provided such a constructor exists.
The compound assignment
The compound assignment operator shown in the table is specified as e1 op= e2, where e1 is a modifiable lvalue of a non-constant type, and e2 is 1 of the following:
The arithmetic types
Pointer (if op is + or wok)
e1 op= e2 behaves in the same way as e1 = e1 op e2, but e1 is only calculated once.
A compound assignment to an enumerated type generates an error message. If the left operand is of pointer type, the right operand must be of pointer type or must be a constant expression that evaluates to 0. If the left operand is of integer type, the right operand cannot be of pointer type.
The result of the assignment operator
After an assignment, the assignment operator returns the value of the object specified by the left operand. The type obtained is the type of the left operand. The result of an assignment expression is always an lvalue. These operators have right-to-left associations. The left operand must be a modifiable left value.
In ANSI C, the result of an assignment expression is not an lvalue. Therefore, the legal C++ expression (a += b) += c is illegal in C.


Related articles: