 # C + + | introductory tutorial (3) operator

Posted on Feb. 2, 2023, 12:09 a.m. by Shlok Chandra
Category: c++ Tag: c++

## 3 operators

Function: Used to perform code operations

### 3.1 Arithmetic Operators

Function: Used to handle addition, subtraction, multiplication and division

+ - * / add, subtract, multiply and divide

/ Division: When two integers are divided, the result is still an integer. The fractional part is removed

% modulo (also called mod) : Can only be mod of two integers

++ Increment -- Decrease: Increments or decrements the variable by 1

Preincrement (decrement) : The increment (decrement) operator precedes a variable by increment before computation. On the other hand, you calculate first and then increase.

``int main(){ using namespace std; Int a=2; int a=3; int a=2; int b=++a; coutbendl; Int c=2; int c=2; int c=2; int d=c++; coutdendl; return 0; }``

### 3.2 Assignment Operator

Function: Assigns the value on the right to the left

= assignment

+=, -=, *=, /= plus equals, build equals, multiply equals, divide equals: These symbols are a simplification of doing the left hand side to the right and then assigning the result to the left hand side.

### 3.3 Comparison Operators

Function: Used to compare expressions and returns a Boolean value.

= = is equal to the

! = is not equal to

\ is greater than the

= less than or equal to

\= greater than or equal to

### 3.4 Logical Operators

Function: Logical processing of values

! Logical nonoperation

Logic and Operations

| | logical or operation

### 3.5 Operator Priority Summary (Supplement)

#### 3.5.1 Priority 1 (from high to low priority) Combine left to right

Highest priority: parentheses () All priorities can be broken by parentheses

Access operators: array index access [], pointer member access -, object member access., object scope access ::

Postfix increment/decrement operators: ++, --

It is important to note that the priority of the access operators is very high. If the parse operator * and the access operator are placed on either side of a variable, the compiler will first line the access operator. Here's an example:

``#includeiostream using namespace std; class Person { public: int years; Person(int y) { years = y; }}; int main() { Person* p = new Person(20); cout  *p.years; delete p; return 0; }``

Note line 13 cout * p.ears; Because p is a pointer to the Person object, it will not be combined with the parse operator and then the access operator orz, but with the access operator on the right. But object Pointers should not be used. To access the member, so the compiler will report an error.

Error (activity) E0153 expression must have class type, but it has type "Person *"

Error C2228 ".years "must have class/structure/union to the left

For this error, just change the priority. I'm going to use parentheses to combine p with the left-hand side first.

``#includeiostream using namespace std; class Person { public: int years; Person(int y) { years = y; }}; int main() { Person* p = new Person(20); cout  (*p).years; delete p; return 0; }``

(Of course you can also use -, but I don't want to hahaha)

#### 3.5.2 Unary Left Join Operator

The logical not! Bit inverse ~ pre-increment ++ pre-decrement -- take negative - take positive + parse * take address cast type (TYPE)

#### 3.5.3 Binary Arithmetic Comparison Bit Operator (to the right)

, modulus

According to the displacement

To compare

With or by location

Logic and or

#### 3.5.4 Ternary Assignment Comma (left)

Ternary Operator (left)

The assignment operator (left)

The comma operator (left)

Search