The use of addition and assignment operators in C + + language _c language

Source: Internet
Author: User
Tags arithmetic bitwise scalar


Addition operator: + and-
Grammar


expression + expression 
expression–expression


Note
The Add operator is:


    • Add (+)
    • Minus (–)


These binary operators have left to right associations.



The add operator takes an operand of arithmetic or pointer type. 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 an operand is a pointer or two operands are pointers, they must be pointers to objects, not pointers to functions. If two operands are pointers, the result is meaningless unless they are pointers to objects in the same array.
The add operator takes the operands of the arithmetic, integral, and scalar types. The following table defines these operands.
Type used for the add operator


S.N. Constructors & Descriptions
Arithmetic Integral and floating-point types are collectively referred to as "arithmetic" types.
Integral The char and int types of all sizes (long, short) and enumerators are "integer" types.
Scalar Scalar operands are operands of an arithmetic type or a pointer type.

The legal combination of these operators is:




    • Arithmetic + arithmetic
    • Scalar + integer
    • Integer + scalar
    • Arithmetic – Arithmetic
    • Scalar-Scalar


Please 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 = Ten;
  int N[size] = {0, 1, 2, 3, 4};
  cout << "5 + =" << i + J << Endl
     << "5-10 =" << i-j << Endl;

  Use pointer arithmetic on array

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



pointer addition
In addition operations, if one of the operands is a pointer to an array of objects, the other operand must be an integral type. The result is the same pointer as the original pointer type and a pointer to another array element. The following code fragment illustrates this concept:
Short INTARRAY[10]; Objects of type short occupy 2 bytes
Short *pintarray = Intarray;


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


Although adding an integer value of 1 to Pintarray does not mean "add 1 to the address", it means "adjust the pointer to the next object in the array", which happens to be outside of 2 bytes (or sizeof (int)).
Attention
Pintarray = Pintarray + 1 code is rarely found in C + + programs; To achieve increments, the following form is preferable: pintarray++ or Pintarray = 1.



Pointer subtraction
If two operands are pointers, the result of the subtraction is the difference between the two operands (in the array element). Subtraction Expressions produce type ptrdiff_t (in the standard contains file stddef. The signed integer result of the definition in H.
One of the operands can be an integral type, provided that the operand is the second operand. The type of the result of the subtraction is the same as the type of the original pointer. The value of subtraction is a pointer to the array element (N–i), 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 the value in the object specified by the left-hand operand. There are two assignment operations: a 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 arithmetic, shift, or bitwise operations are performed before the result is stored. All other assignment operators except the = operator in the following table are compound assignment operators.
Assignment operator





The=integer and floating-point type is collectively referred to as the "arithmetic" type.
operator meaning
*= the char and int types of all sizes (long, short) and enumerators are "integer" types. The
/= scalar operand is an operand of an arithmetic type or a pointer type. The
% = integer and floating-point type are collectively referred to as the "arithmetic" type. The char and int types of all sizes (long, short) and enumerators for
= = are of type integer. The
–= scalar operand is an operand of an arithmetic type or a pointer type.
<<= shifts the value of the first operand to the number of digits specified by the value of the second operand, and stores the result in the object specified by the first operand.
>>= shifts the value of the first operand to the right of the number specified by the value of the second operand, and stores the result in the object specified by the first operand.
&= Gets the bitwise AND of the first and second operands, and stores the results in the object specified by the first operand.
^= Gets the bitwise XOR of the first and second operands, and stores the results in the object specified by the first operand.
|= gets the bitwise AND OR of the first and second operands, and stores the results in the object specified by the first operand.





operator keywords
Three composite assignment operators have text equivalents. They are:


operator equivalent
&= 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 using the/za (Disable language extensions) 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 (=) stores the value of the second operand in the object specified by the first operand. If two objects are arithmetic types, the correct operand is converted to the type on the left before the value is stored.
Objects of both constants and mutable types can be assigned to the left value of a mutable type or to a left value that is neither a constant type nor a mutable type.
The assignment of an object to a class type (struct, union, and class type) is performed by a function named Operator=. The default behavior of this operator function value is to perform bitwise copying, but you can modify this behavior by using overloaded operators. (For more information, see overloaded operators.) )
Any object from a class that is explicitly derived from a given base class can be assigned to the base class object. Otherwise, because there is an implicit conversion, it can be converted from a derived class to a base class, but cannot be converted from a base class to a derived class. For example:


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
}


The assignment to a reference type behaves as if it were assigned to the object to which the reference points.
For class-type objects, assignment differs from initialization. To demonstrate how different assignments and initializations work, consider the following code


UserType1 A;
UserType2 B = A;


The code above shows an initializer, which invokes the UserType1 constructor of a parameter of type UserType2. Given the following code


UserType1 A;
UserType2 B;

B = A;


Assignment statement


B = A; 


May have one of the following effects:
The function operator= is invoked for UserType2, provided that the operator= provides UserType1 parameters.
If an explicit conversion function Usertype1::operator UserType2 exists, the function is called.
Calls the constructor that takes the Usertype2::usertype2 parameter and copies the result UserType1, provided that such a constructor exists.
Compound assignment
The composite assignment operator in the table that is displayed is specified in the form of E1 op= E2, where E1 is a modifiable left value of the very magnitude type, and E2 is one of the following:
Arithmetic type
Pointer (if OP is + or-)
The E1 op= E2 form behaves the same as the E1 = E1 OP E2, but the E1 is counted only once.
A composite assignment to an enumeration type generates an error message. If the left operand belongs to a pointer type, the right-hand operand must be of the pointer type or must be a constant expression that evaluates to 0. If the left operand is of an integer type, the right-hand operand cannot belong to the pointer type.
The result of the assignment operator
When assigned, the assignment operator returns the value of the object specified by the left-hand operand. The type obtained is the type of the left operand. The result of an assignment expression is always the left value. These operators have right-to-left affinity. The left operand must be a modifiable left value.
In ANSI C, the result of an assignment expression is not a left value. Therefore, a valid C + + expression (A = b) = c is illegal in C.


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.