the other operand.
Float
double
long double
float
double
long double
double
double
long double
Long double
long double
long double
float
double
long double
This ruler can be represented in following table
Note
No exponentiation operator
Operators are overloaded to work with more than one type of object
Integer Division
Integer Division
Define the integer division by
(a/b)*b + a%b
(a/b)*b + a%b
≡
a
(b
a
(b
≠
0)
0)
Integer division produces an integer result
Truncates the result
Examples
3/2
evaluates to 1
4/6
evaluates to 0
10/3
evaluates to 3
Mod
Mod
Produces the remainder of the division
Examples
5 % 2
evaluates to 1
12 % 4
evaluates to 0
4 % 5
evaluates to 4
When operands are negative the sign of result of modulus
expression is compiler dependant.
In Microsoft C++, the result of a modulus expression is
always the same as the sign of the first operand. For
instance,
5%(2) evaluates to 1
5%2
evaluates to 1
(5)%(2) evaluates to 1
Unary Arithmetic Operators
Unary Arithmetic Operators
+35,
40,
a
Mixed
Mixed


mode expression
mode expression
Usual arithmetic conversion for mixedmode expression
int
int
long
float
double
long
float
double
long double
long double
int
long
float
double
long double
Long
long
float
double
long double
float
float
float
double
long double
int
int
long
long
float
float
double
double
long double
long double
long double
long double
long double
long double
long double
double
double
double
double
long double
Precedence and
Precedence and
Associativity
Associativity
Consider
m*x + b
which of the following is it equivalent to
(m * x) + b
m * (x + b)
Operator precedence tells how to evaluate expressions
Standard precedence order
()
Evaluate first, if nested innermost
done first
* / %
Evaluate second. If there are several,
then evaluate from lefttoright
+ 
Evaluate third. If there are several,
then evaluate from lefttoright
Left associativity and Right associativity
Defining and Initializing
Defining and Initializing
When an object is defined using the basic form, the
memory allotted to it contains random information
Better idea to specify its desired value at the time of
definition
Exception is when the next statement is an
extraction for the object
Remember our convention of one definition per
statement!
Compute average velocity of a car
Home works
Home works
Page98,
Exercises
2.10
Page99,
Exercises
2.17 + 2.18
Page102,
Exercises
2.33
Page102,
Exercises
2.34
End of Chapter 2
End of Chapter 2
You've reached the end of your free preview.
Want to read all 38 pages?
 Winter '03
 Pro.Lei
 C++ Programming, object types, Long double