CISC 3142
Programming Paradigms in C++
Part II: Basic Facilities
Chapter 10 — Expressions
Reading from the Text
Chapter 10 — Expressions
The chapter introduces expressions in § 10.2 in the context of a developed sample program (a calculator),
which can be skipped
§ 10.3 is an overview of the language's operators; § 10.3.4 can be skipped
§ 10.4 should be basically readable; skip §§ 10.4.4-10.4.5
§ 10.5 is also readable; skip § 10.5.2.4
Overview
This is an overview of the basic expressions and operators of the language. It is presented in the context of a calculator app, which,
while informative, will not be covered.
Topics
Implicit Type Conversion
Promotion
A promotion is a conversion that preserves the value, e.g., short to int
Conversions
C++ (inherited from C) has tons of implicit conversions
Advice
[1] Prefer the standard library to other libraries and to 'handcrafted code'; §10.2.8.
[2] Use character-level input only when you have to; §10.2.3.
prefer token-oriented input, i.e., >> to get or getline
let the system do the parsing and aggregation of the characters into the proper data types for you
[3] When reading, always consider ill-formed input; §10.2.3.
Again, easier said than done
[4] Prefer suitable abstractions (classes, algorithms, etc.) to direct use of language features (e.g., ints, statements); §10.2.8.
Stroustrup mentions the novice attitude towards using sophisticatedm already-defined structures (such as maps) as 'cheating'.
Such entities are almost always better tested and more efficient than any 'homegrown' alternatives.
[5] Avoid complicated expressions; §10.3.3.
[6] If in doubt about operator precedence, parenthesize; §10.3.3.
[7] Avoid expressions with undefined order of evaluation; §10.3.2.
a[i] = i++;
[8] Avoid narrowing conversions; §10.5.2.
e.g., double to int
Java doesn't even allow them implicitly
[9] Define symbolic constants to avoid 'magic constants'; §10.4.1.