Instructors' Guide


Introduction Terminology Expressions Control Objects Inheritance Technology Summary

Expressions

Again due to its C heritage, C++ supports many basic types (including int, char and float) and compound types (including arrays, functions, pointer types, reference types, and user-defined class, union or struct types). See slide cc-expr-1.

Type expressions


slide: C++ -- expressions (1)

Pointer types encompass pointers to basic types and pointers to user-defined types, such as functions and classes. The difference between object, reference and pointer types may be succinctly characterized as the difference between the actual thing, an alias (that looks like the actual thing but isn't) and an address of the actual thing (where you have to go to get it).

Expressions

Assignment


slide: C++ -- expressions (2)

Value expressions may be created using arithmetic and comparison operators (including == for equality and != for inequality). As logical operators, C++ includes conjunction (&&) and disjunction ( || ), as well as a number of bitwise logical operators. Also, we have an indexing operator (which may be defined for arbitrary types), an application operator (which may also be defined for arbitrary types), an access operator (which is as a standard used for member function invocation or method calls), a dereference operator (which is used to invoke member functions through a pointer to an object) and in- and decrement operations (that, again, may be defined for arbitrary types). Needless to say, user-defined operators must be applied with care. Also, we have a conditional expression of the form b?e_1:e_2 testing the condition b to deliver e_1 when it evaluates to true and e_2 otherwise. Also, C++ allows for sequencing within expressions of the form (e_1,...,e_n), which evaluates e_1,...,e_n in that order and delivers e_n as its value.

Assignments in C++, it is important to note, are written as var = expression with a single = symbol. This convention is known to cause mistakes by programmers raised with languages such as Pascal or Modula-2. In addition, C++ offers modifying assignments, which may be used as, for example, in n += 1, which is identical in meaning to n = n + 1.