C++ Notes: Summary - Expressions

Parentheses () have three uses:
  1. Grouping to control order of evaluation, or for clarity.
    Eg, (a + b) * (c - d)
  2. After a function name to enclose parameters. Eg, x = sum(a, b);
  3. Around a type name to form a cast. Eg, i = (int)x;

Order of evaluation
  1. Higher precedence are done before lower precedence.
  2. Left to right among equal precedence except: unary, assignment, conditional operators.

Abbreviations
i, j - integer (char, short, int, long) values.
m, n - numeric values (integers, floating-point).
b, c - int expr: 0=true, non-zero=false; x, y - any type.
a - array; p - pointer; f - field.
obj - a class object; T - a class type.
x - anything, t - a type.
Operator Precedence
::
. [] -> (args) post ++ --
   type_id dynamic_cast static_cast
   cont_cast reinterpret_cast
unary + - * & pre ++ --
   ! ~ (type) new sizeof delete
->* .*
* / %
+ -
<< >>
< <= > >=
== !=
&
^
|
&&
||
?:
= += -= etc
throw
,
Remember at a minimum
  1. unary operators
  2. * / %
  3. + -
  4. comparisons
  5. && ||
  6. = assignments
Use () for all others

Arithmetic Operators
The result of arithmetic operators is double if either operand is double, else float if either operand is float, else long if either operand is long, else int.
n + mAddition. Eg 7+5 is 12, 3 + 0.14 is 3.14
n - mSubtraction
n * mMultiplication. Eg 3 * 6 is 18
n / mDivision. Eg 3.0 / 2 is 1.5 , 3 / 2 is 1
n % mRemainder (Mod) after dividing n by m. Eg 7 % 3 is 1
++iAdd 1 to i before using the value.
--iAs above for subtraction
i++Add 1 to i after using the value.
i--As above for subtraction
Comparison Operators
The result of all comparisons is 0 (false) or non-zero (true).
< <= == != > >=
Logical Operators
Logical values are zero (false) and non-zero (true).
b && cConditional "and". true if both operands are true, otherwise false. Short circuit evaluation. Eg (false && anything) is false.
b || c Conditional "or". true if either operand is true, otherwise false. Short circuit evaluation. Eg (true || anything) is true.
!b true if b is false, false if b is true.
Conditional Operator
b?x:yif b is true, the value is x, else y. x and y must be the same type.
Assignment Operators
= Left-hand-side must be an lvalue.
+= -= *= ...All binary operators (except && and ||) can be combined with assignment. Eg
a += 1 is the same as a = a + 1
Bitwise Operators
Bitwise operators operate on bits of ints. Result is int.
i & jBits are "anded" - 1 if both bits are 1
i | jBits are "ored" - 1 if either bit is 1
i ^ jBits are "xored" - 1 if bits are different
~iBits are complemented (0 -> 1, 1 -> 0)
i << jBits in i are shifted j bits to the left, zeros inserted on right.
i >> jBits in i are shifted j bits to the right. On left 0 bits inserted for unsigned, sign bits for signed.
I/O Operators
There are two operators defined in <iostream>.
cout << xOutput "insertion" operator.
cin >> xInput "extraction" operator.
Reference Operators
a[i]Array element access.
s.fMember. The f field or method of struct/class s.
p->fMember. The f field or method of struct/class pointer p.
&xAddress of x.
*pDereference pointer p.
.*Dereference pointer to class memeber.
->*Dereference pointer to pointer to class memeber.
std::coutScope resolution operator.
Misc Operators
sizeof xMemory size of x in bytes. Usually written as sizeof(x).
Casts
Use casts to make type conversion clear, and especially when "narrowing" range of a value. x is a value and T is a type.
(T)xCasts x to type T. Eg us = (unsigned short)i; Most universal.
T(x)Casts x to type T. Eg i = int(f); Use with simple type names.
dynamic_cast<T>(obj)Downcasts obj to subclass T. May throw bad_cast.
static_cast<t>(x)Used for traditional casts, eg int to double.
const_cast<t>(x)To cast away const or volatile.
reinterpret_cast<t>(x)Reinterprets bit pattern. Evil, dangerous, non-portable.
Dynamic memory allocation/deallocation
T is a type (simple, struct, or class)
T* p;Pointer declaration. Eg, int* a; or Card c;
p = new T;Allocates memory for one T. Returns address.
p = new T(params,...);Allocates new object, constructor called to initialize fields.
p = new T[n];Allocates array of n elements. Returns pointer to first.
delete p;Deallocates object p points to. Must not be an array.
delete [] p;Deallocates array p points to.