Something about programming

Operators in C++

Previous tutorial: Input and Output in C++. Streams

C++ has different kind of operators. Each operator define some action. We already saw couple operators: assignment operator =, and stream insertion/extraction <<, >>. There are many others in C++.

We'll list almost all operators but many of them we'll learn in later tutorials.

Each operator has one or more operands. Operator with one operand is called unary. Operator with two operands - binary. And each operator have a result - we'll call it: evaluation of operator to some value. Let's discuss sum operator +:

1 + 2; // result is 3 int a = 3; int b = 5; a + b; // result is 8

As you can see operators can use literals and variables. We can do anything (that allowed by the rules of C++) with the result of operator:

Precedence of Operators in C++ Language

int c = a + b;

Here is the interesting thing. There are two operators on this line. Operator + has operands a and b. And operator = has operands c and result of a + b.

So, if there are more then one operator on line, they will be executed one by one in specific order. The order in which operators are executed is called precedence. + is executed before = as assignment has lower precedence than addition.

Moreover, there is such thing as associativity.

Associativity of Operators in C++

Associativity is the order of operators execution when operators have same precedence. Let's look at the example. If we have several additions, they will be executed one by one from left to right - it corresponds to math rules. This is left to right associativity. Same if we have additions and subtractions operators mixed - they have same precedence.

Example of right to left associativity is assignment:

int a = 1; int b = 2; int c; c = b = a;

First will be executed right assignment. The result of this assignment is value that was assigned to b. After this line all three variables will have same value 1.

Precedence groups in C++

Almost all groups have left to right associativity. I'll point which groups has right to left.

1. Highest priority has scope resolution operator. We met it already - it's two colons. It's used for namespaces:

std::cout;

2. In this group there are all functional type casts - we'll know about it later.

Dot . and ->. Member access operators. They are used with more advanced data types than we know.

[] and (). First is subscript - array element access, and second is function call. Again, we'll learn them soon too.

Post-increment (or postfix) ++ and post-decrement --. They are used with numbers. They increase or decrease the number by one:

int a = 1; cout << a++ << endl; // 1 cout << a++ << endl; // 2 cout << a++ << endl; // 3

Post-increment first return current value and after that increases variable by 1. The result of first line: 1 on the screen, but in memory after that line there is 2 already... Purpose of these operators is to reduce amount of code. We can substitute increment/decrement with this code:

a = a + 1; a = a - 1;

3. This group has right to left associativity. There is pre-increment (or prefix) and pre-decrement:

int a = 1; cout << ++a << endl; // 2 cout << ++a << endl; // 3 cout << ++a << endl; // 4

First, operator adds 1 and only after that it returns value. Prefix and postfix operators are used in different situations and they have their own use.

Unary operators + and -. Unary means that operator uses only one operand.

int a = 1; cout << -a << endl; // -1

We use minus when we want to change sign of number. And we can use plus explicitly if we want.

Logical and bitwise NOT: !, ~. We omit all bitwise operators by now. Logical NOT changes bool value to opposite:

bool a = true; !a; // false

C-style type casts also in this group. We'll have a big talk about type casting very soon.

Several memory operators: allocation new, deallocation delete, sizeof, indirection a*, address-of a&. We can learn size of variable with this code:

int a = 38422; cout << sizeof(a) << endl; // 4

4. Operators to access members through pointers: ->* and .*.

5.Multiplication *, division / and remainder %. The interesting operation here is remainder:

cout << 10 % 4 << endl;

Remainder perform division and return remainder. So in this case on the screen you'll see 2. It's quite common operation in programming that's why it has separate operator in most programming languages.

6. Addition + and subtration -. Nothing interesting here. As you can see they have lower precedence than multiplication, remainder, division.

7. Bitwise shifts. Left shift << and right shift >>. Important thing - these are not stream insertion/extraction operators. Here we see that one symbol has two meanings - it's called operator overload. And C++ use this feature a lot as we'll see in the future.

Bitwise shifts works with bits - they move bits of number to the left and to the right. It has interesting practical value. With bitwise shifts we can easily multiply/divide by power of 2.

int a = 2 << 3; int b = 2 * 2 * 2 * 2;

These two lines are equivalent and the result will be 16.

8. Comparisons: less than <, less than or equal <=, greater than >, greater than or equal >=. They return bool value.

9. Equality == and inequality !=. Return bool too.

10. Bitwise AND &. All bitwise operators work with separate bits. We'll have separate tutorial where we'll learn how to use them.

11. Bitwise XOR ^.

12. Bitwise OR |.

13. Logical AND &&.

14. Logical OR ||. Logical operations return bool value. With logical AND and OR we can build complex conditions (that's the theme of next tutorial).

15. This group has right to left associativity. There is throw operator, it's used when something unexpected happens.

Only one ternary operator in C++ ?:. Ternary means that it requires three operands. First part of operator is condition. If it's true, then second part executed, if first part is not true, then third part is executed. So the idea behind this operator: only second or third part will be executed and it depends on condition in first part:

enemy_is_near ? shoot : run;

This code is not working - it's just idea how to use ?:. Later we'll actually learn how to check if enemy is near and act accordingly.

Also in this group several assignments operators. =, +=, -=, *=, /=, %= and bitwise analogues >>=, <<=, ^=, &=, |=. We can use only first one - it's called direct assignment. Others (compound assignments) will allow you to reduce amount of code. Let's compare:

int a = 10; int b = 10; a = a + 5; b += 5;

It's the same. Others compound assignments work the same way.

16. The latest one is comma ,. Here is how it can be used:

int a = 2, b = 3;

Here we defined two int variables in one line.

Conclusion

That's a brief view on operators in C++. We'll discuss most of them in details when we'll learn corresponding topics.

Comments:

No comments yet