Page 31 - DCAP404 _Object Oriented Programming
P. 31
Object-oriented Programming
Notes
Notes Note that the truncated quotient resulting from the division operation, since both
operands represent integer quantities. Also, notice the integer remainder resulting from
the use of the modulus operator in the last expression.
Now suppose that a1 and a2 are floating-point variables whose values are 14.5 and 2.0,
respectively. Several arithmetic expressions involving these variables are shown below, together
with their resulting values.
Expression Value
a1 + a2 16.5
a1 – a2 12.5
a1 * a2 29.0
a1 / a2 7.25
Finally, suppose that x1 and x2 are character-type variables that represent the character M and U,
respectively. Some arithmetic expressions that make use of these variables are shown below,
together with their resulting values (based upon the ASCII character set).
x1+x2 = 162
x1+x2+’5'=215
Note that M is encoded as (decimal) 77, U is encoded as 85, and 5 is encoded as 53 in the ASCII
character set.
If one or both operands represent negative values, then the addition, subtraction, multiplication
and division operations will result in values whose signs are determined by the usual rules of
algebra. Integer division will result in truncation toward zero; i.e., the resultant will always be
smaller in magnitude than the true quotient.
The interpretation of the remainder operation is unclear when one of the operands is negative.
Most versions of C++ assign the sign of the first operand to the remainder. Thus, the condition
a = ((a/b) * b) + a % b)
will always be satisfied, regardless of the signs of the values represented by a and b.
Suppose that x and y are integer variables whose values are 12 and –2, respectively. Several
arithmetic expressions involving these variables are shown below, together with their resulting
values.
Expression Value
x+y 10
x–y 12
x*y –24
x/y –6
x%y 0
24 LOVELY PROFESSIONAL UNIVERSITY