C++ Basics Part 2
C++ Basics Part 2
Operators
Special built-in symbols that have functionality, and work on operands
Operators are actually functions that use a more familiar notation. We'll discuss this
further when we get to functions
operand -- an input to an operator
Arity - how many operands an operator takes
unary operator -- has one operand
binary operator -- has two operands
ternary operator -- has three operands
Examples:
int x, y = 5, z;
z = 10; // assignment operator (binary)
x = y + z; // addition (binary operator)
x = -y; // -y is a unary operation (negation)
x++; // unary (increment)
This works because the result of one operation sends back the answer (i.e. a return value) in
its place, to be used in the next piece of the statement. In the above, (a + b) happens first, then
the answer becomes the first operand in the next + operation.
Precedence - rules specifying which operators come first in a statement containing multiple
operators
x = a + b * c; // b * c happens first, since * has higher
// precedence than +
Associativity - rules specifying which operators are evaluated first when they have the same
level of precedence.
Most (but not all) operators associate from left to right.
Assignment Operator
Value on the right side (R-value) is assigned to (i.e. stored in) the location (variable) on the left
side (L-value)
R-value -- any expression that evaluates to a single value (name comes from "right" side
of assignment operator)
L-value -- A storage location! (not any old expression). A variable or a reference to a
location. (name comes from "left" side of assignment operator
Typical usage:
variable_name = expression
Associates right-to-left
x = y = z = 5;
Arithmetic Operators
Name
Add
binary x + y
Subtract -
binary x - y
Multiply
binary x * y
Divide
binary x / y
Modulus %
binary x % y
Minus
unary -x
For integer types, / gives the quotient, and % gives the remainder (as in long division)
int x = 19, y = 5, q, r;
q = x / y; // q is 3
r = x % y; // r is 4
An operation on two operands of the same type returns the same type
An operation on mixed types (if compatible) returns the "larger" type (see "Automatic Type
Conversions" below).
int x = 5;
float y = 3.6;
z = x + y; // what does z need to be? x + y returns a float.
Operator precedence
Arithmetic has usual precedence
1. parentheses
2. Unary minus
3. *, /, and %
4. + and 5. operators on same level associate left to right
Many different levels of operator precedence (about 18)
When in doubt, can always use parentheses
Example:
z = a - b * -c + d / (e - f);
+= e;
-= e;
*= e;
/= e;
%= e;
means
means
means
means
means
v = v + e;
v = v - e;
v = v * e;
v = v / e;
v = v % e;
Pre-increment: incrementing is done before the value of x is used in the rest of the expression
Post-increment: incrementing is done after the value of x is used in the rest of the expression
Note - this only matters if the variable is actually used in another expression. These two
statements by themselves have the same effect:
x++;
++x;
Examples
int x = 5, count = 7;
result = x * ++count; // result = 40, count = 8
int x = 5, count = 7;
result = x * count++; // result = 35, count = 8
Better to use newer C++ cast operators. For casting between regular variables, use static_cast
c1 = static_cast<char>(i2);
i1 = static_cast<int>(d2);