Digital Communication


Digital Communication

C++ Operators: presentation of C++ operators


Whether you are learning C++ or have been working with this programming language for a long time, C++ operators form the basis of effective programming. Although the meaning of some operators is intuitive, this is not the case for all.

What are C++ operators?

Basically, an operator is simply a character designating an operation. An operator can refer to one operand, but frequently it refers to several operands. Therefore, in most cases you get a new value. A popular example are arithmetic operators, for example “+” for adding and “-” for subtracting two numbers.

C++ operators are not only differentiated based on their function. THE operator status or affinity are also important criteria for differentiation:

Continuity Number of operands Example
Unary operators An operand Logical negation: !var_name
Binary operators Two operands Addition : value1 + value2
Ternary operators Three operands if-else condition: condition? condition_true : condition_false

Carrier Priority

As with simple arithmetic operators or Python operators, there is also a prioritization of C++ operators. This indicates the order in which operators are parsed. For arithmetic operators, the known rules of dot before dash apply. However, there are also other rules you need to follow for other C++ operators.

if (var1 && var2 || var3) {


In the example above, the logical expression after the “if” operator is evaluated first. Here again, the “&&” (logical AND) operator takes precedence over the “|| » (logical OR). So, if the evaluation of “var1 && var2” or the evaluation of “var3” returns “true”, the function call of “do_something()” is executed.

If you want more certainty, you can also use parentheses.

Operator overloading in C++

You can overload most C++ operators. This means you can assign a new meaning to an existing operator in a context. To perform operator overloading in C++, you must use the keyword “operator “. Then you define the C++ operator you want to overload. Otherwise, operator overloading in C++ behaves like function overloading.

Overview of C++ Operators

Arithmetic operators

Some of the C++ arithmetic operators are already known from school. They work with numbers and then return a new number. With two exceptions (the pluses and minuses which we can think of as signs), the arithmetic operators are all binary operators.

C++ operator Meaning Example
+ Addition / unary plus 6 + 4
- Subtraction / unary minus 10 - 6
* Multiplication 10 * 3
/ Division 20 / 10
% Modulo 21 % 2

Assignment operators

As in most programming languages, values ​​are stored in variables in C++. To assign specific values ​​to such variables, you need special operators.

Simple assignment operators

C++ operator Meaning Example
= Simple assignment x = 3
++ Increment x++
Decrement x–

Compound assignment operators

In addition to simple assignment operators, C++ also supports compound operators. These are arithmetic or “bitwise” operations connected simultaneously with a value assignment :

In the sample code above, the variable “x” is given a numeric value of 4 based on a simple assignment. The compound assignment operator “+=” is then used to perform an arithmetic addition and store the resulting value directly in “x”. The assignment looks like this: “x = x + 2”.

C++ operator Meaning Example
+= Sum and allocation x += 2
-= Subtraction and assignment x -= 2
*= Multiplication and assignment x *= 2
/= Division and assignment x /= 2
%= Modulo and assignment x % = 2
&= Bitwise AND operator and assignment b &= 1
<<= Rotate left and assign b <<= 1
>>= Rotate right and assign b >>= 1
^= Bitwise OR operator and assignment b ^= 1
` =` Bitwise OR operator and assignment

C++ Logical Operators

You can use C++ logical operators to make comparisons of two expressions. With the exception of “non-logical” expression, which always refers to a single instruction and overrides its true value, logical operators are binary.

C++ operator Meaning Example
&& AND logical true && true
` `
! NOT logical !true

Comparison operators

Comparison operators are C++ operators that examine the relationship between two elements; they are therefore binary. Except for the three-way comparison which returns a number, the value returned by all C++ comparison operators is a true value.

C++ operator Meaning Example
== Equality a == b
!= Inequality a != b
<= Less than or equal to a <= b
>= Greater than or equal to a >= b
< Smaller than a < b
> Bigger than a > b
<=> Three-way comparison a <=> b

Bit manipulation

To be able to access individual bits efficiently and improve the speed of programs, there are C++ bit operators that are particularly important for performance-oriented programming.

C++ operator Meaning Example
& AND bit operator a & b
` ` OR bit operator
^ Exclusive OR bit operator a ^ b
~ 1’s complement operator ~a
<< Left Shift Operators a << b
>> Right Shift Operators a >> b

Storage management

C++ is a machine language and therefore offers a number of operators to help you manage memory.

C++ operator Meaning Example
& Determine the address & x
sizeof() Determines the memory requirements of an expression sizeof(x)
new Creates a new object and returns pointers object* pointer = new object()
delete Delete an object delete object

Data access for objects and pointers

The following C++ operators will help you if you want to access individual members of objects or pointer storage areas.

C++ operator Meaning Example
* Indirection operator *pointer = 3;
. Member Access Operators object.member = 2;
-> Member access operators with a pointer objectpointer-> member = 2 ;

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact