Announcements and Reminders
|
Topics |
Terminology |
||||||
Chapter 3cinWhat does the extraction operator do?Entering multiple values white space Program 3-3 - Page 88-89 Mathematical Expressionsz9 z + 9 z * 9 sizeof(z) z + 5 * 3 / 7 What if int z = 5; What is the value of each expression? Operator Precedence and Associativityparenthesessizeof - (unary negation) * / % + - (binary subtraction) << >> Evaluate these expressions 5 + 2 * 6 12 / 4 - 3 3 + 4 * 6 - 2 12 * 18 % 7 - 1 15 - 2 * 6 + 8 - 2 (5 + 2) * 6 12 / (4 - 3) 3 + 4 * (6 - 2) 12 * (18 % 7) - 1 (15 - 2) * (6 + (8 - 2)) 5 * sizeof(1000000) associativity right to left or left to right See Appendix C Page 1289 The pow() functionProgram 3-5 - Page 96Calculate an average Program 3-6 - Page 98 Type ConversionsLevel of Conversionsexact match trivial conversion promotions standard conversions floating point type to integer type demotions integer type to floating point type When/How does a conversion occur assignment binary operation cast Overflow and UnderflowProgram 3-7 - Page 102Program 3-8 - Page 103 CastC-style cast"old" C++ style functional notation for a cast static_cast forces a type conversion sometimes used to document a conversion (that will take place anyway) useful for something like, average of three ints: int a = 2, b = 3, c = 5; float avg = (a + b + c) / static_cast<float>(3); Advice: Do not use C-style casts in this class (or even functional notation for a cast). Use static_cast where appropriate. Multiple Assignment and the Assignment operator int x = 5, z = 99, q, maybe = 9; int a = b = c = d = 9; // Why does this work? Example: Calculate the harmonic mean of the first 5 prime numbers.The formula for a harmonic mean is Example: Calculate the roots of a quadratic equationA quadratic equation has the formax2 + bx + c = 0 The roots are determined by the formula
Some More Integer OperatorsCompound assignment+= -= *= /= %= Increment and decrement operators ++ prefix and postfix -- prefix and postfix Formatted OutputAn output stream manipulator changes the behavior of an output stream.The parameterized manipulators require the <iomanip> header file. The setw manipulatorhas a short memory
Program 3-13 - Page 112 The setprecision manipulatorhas a long memory
Program 3-15 - Page 114 The fixed and scientific manipulatorshave a long memory
Example
The showpoint manipulatorhas a long memory
Example
The left and right manipulatorshave a long memory
Example
string inputExample
What happened? The getline (string) functionreads from an input stream into a stringPrototype istream& getline (istream& is, string& str); Example
char inputThe overloaded get functionreads char input from an input streamPrototypes int istream::get(); istream& istream::get (char& c); Example
The
ignore function
|
cin istream object extraction operator right shift operator precedence operator associativity pow() conversion exact match trivial conversion promotions demotions integer type floating point type assignment assignment operator binary operator, binary operation cast static_cast (operator) output stream manipulator <iomanip> setw setprecision fixed scientific showpoint left right input buffer compound assignment operators += -= *= /= %= increment operator ++ decrement operator -- prefix postfix output stream manipulator <iomanip> setw setprecision fixed scientific showpoint left right input buffer getline prototype get overloaded ignore delimiter default argument math library abs cos exp fmod log log10 sin sqrt tan <cmath> rand() <cstdlib> srand seed a random number generator time <ctime> |