Chapter 3 Expressions and Interactivity

advertisement
Starting Out with C++: From Control Structures through Objects, 8/E - Tony Gaddis
Ray Devore's Notes on Chapter 3: Expressions and Interactivity
CHAPTER 3
3.1
Expressions and Interactivity
83
The cin Object...................................................................................................................... 83
Like cout, cin (see-in) is a stream object. The difference is that cin is a buffered input
stream (std::istream) object.
>> stream extraction operator (extracts data from the input stream and moves the data to
variables)
Like cout, cin is part of the standard namespace; therefore, it will be written as:
std::cin
std::cin is used to bring data/information from the keyboard buffer into the program.
The stream extraction operator (>>) stops at white space or an unusable character. If it is
trying to read a value into an int variable, it will stop when it reaches something that
cannot be translated into an integer.
Make sure you give a good prompt so that the user of your program knows what type of
data to enter.
3.2
Mathematical Expressions .................................................................................................. 89
When we talk about mathematical expressions, two terms come into play: precedence and
associativity.
Precedence defines the order actions are taken. NOTE: Use of parentheses can change
precedence.
Associativity defines the direction actions are taken.
+ add
- subtract
* multiply
/ divide
The data type of the result from the above operations is based on the most precise data
type of the operands.
% modulus (integer remainder from integer division)
All of the above operators are binary operators. They operate on two operands.
Page 1 of 6
Starting Out with C++: From Control Structures through Objects, 8/E - Tony Gaddis
Ray Devore's Notes on Chapter 3: Expressions and Interactivity
When multiple operators are in an expression they are processed one at a time according to
precedence and associativity.
= assignment
The left hand value of an assignment operator must be a variable, also known as an l-value
The right hand value of an assignment operator may be any expression that creates a value.
There is no operator to take a value to a power. To do this, we use the pow function.
xy  pow(x,y)
result = std::pow(base, power);
3.3
When You Mix Apples and Oranges: Type Conversion ....................................................... 98
Low rank
High rank
short  int  long  long long  float  double  long double
unsigned  signed
r-value computed then converted to type of l-value then assigned to l-value
Type coercion – automatic conversion from lower rank to higher rank
Highest:
long double
double
float
unsigned long
signed long
unsigned int
signed int
unsigned short
signed short
unsigned char
signed char
bool
1.7E308
1.7E308
3.4E38
4,294,967,295
2,147,483,648
4,294,967,295
2,147,483,648
65,535
32,767
255
127
1
Lowest
Ranked by maximum number the type can hold
Page 2 of 6
Starting Out with C++: From Control Structures through Objects, 8/E - Tony Gaddis
Ray Devore's Notes on Chapter 3: Expressions and Interactivity
3.4
Overflow and Underflow ................................................................................................... 100
Overflow is when the value is larger than the type can hold (too positive)
Underflow is when the value is smaller than the type can hold (too negative)
with floating point the overflow/underflow may be in either the mantissa or exponent.
Assigning a negative value to an unsigned type will result in strange results.
unsigned int xyz = -1;
std::cout << xyz << std::endl;
Result:
4,294,967,295
3.5
Type Casting ...................................................................................................................... 101
Instead of letting the computer convert the type automatically, you can use type casting to
temporarily change the type of a value.
static_cast<new_type>(value)
static_cast<double>(12 / 5)  2
static_cast<double>(12) / 5  2.4
3.6
Multiple Assignment and Combined Assignment ............................................................. 104
int a1, b1, c1;
double d2, e2, f2;
a1 = d2 = b1 = e2 = c1 = f2 = 12.74;
std::cout << "f2 = " << f2 << std::endl;  12.74
Page 3 of 6
Starting Out with C++: From Control Structures through Objects, 8/E - Tony Gaddis
Ray Devore's Notes on Chapter 3: Expressions and Interactivity
std::cout << "c1 = " << c1 << std::endl;  12
std::cout << "e2 = " << e2 << std::endl;  ??
std::cout << "d2 = " << d2 << std::endl;  ??
double a3 = b3 = c3 = 0; // invalid
double a3 = 0, b3 = 0, c3 = 0; // valid
Compound assignment operators (no space between symbols)
+= plus equal
-= minus equal
*= times equal
/= slash equal
%= mod equal
abc += def; is equivalent to abc = abc + def;
result *= var + 5;
is equivalent to
result = result * (var + 5);
3.7
Formatting Output ............................................................................................................ 108
To use the following items, you must have the following preprocessor statement at the
beginning of your program.
#include <iomanip> // input/output manipulators
1) std::setw(num) uses at least num spaces. This function only effects the next value.
Values are right justified within the num spaces. If the value needs more than num
spaces, it will use as many as it needs.
std::cout << std::setw(4) << 1 << 2 << std::endl;
yields
12
Page 4 of 6
Starting Out with C++: From Control Structures through Objects, 8/E - Tony Gaddis
Ray Devore's Notes on Chapter 3: Expressions and Interactivity
std::cout << std::setw(4) << 12345 << 2 << std::endl;
yields
123452
2) std::setprecision(pos) shows pos digits of precision.
0.00007248 output using std::setprecision(2) would yield 0.000072
The value used in std::setprecision stays in effect until it is set to something else
or until the end of the program.
3) std::fixed in conjunction with std::setprecision(pos) shows pos digits after
the decimal point.
4) std::showpoint causes the decimal point to be shown. Only key:

In some obscure compilers (I haven't found one yet)

When std::fixed is set with std::setprecision(0)

For output of integer values in decimal variables when std::fixed is not set
5) std::left/std::right causes the information output within the spaces specified by
std::setw(num) to be left or right justified.
3.8
Working with Characters and string Objects .................................................................... 118
We will cover this at a later date.
3.9
More Mathematical Library Functions .............................................................................. 124
#include <cmath>
std::abs(val) // absolute value
std::exp()
std::fmod(value, divisor) // remainder after an integer quotient
std::sqrt(val) // square root
std::log(val) // natural logarithm
std::log10(val) // logarithm base 10
3.10 Focus on Debugging: Hand Tracing a Program ................................................................. 130
Pencil-running
Page 5 of 6
Starting Out with C++: From Control Structures through Objects, 8/E - Tony Gaddis
Ray Devore's Notes on Chapter 3: Expressions and Interactivity
Walking through each step of the program by hand and keeping track of the changing
values.
3.11 Focus on Problem Solving: A Case Study .......................................................................... 132
Left for your reading pleasure.
Page 6 of 6
Download