object-oriented programming using c++ third edition chapter 2 evaluating c++ expressions

33
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Upload: roxanne-mckenzie

Post on 12-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++Third Edition

Chapter 2Evaluating C++ Expressions

Page 2: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 2

Objectives

• Use C++ binary arithmetic operators

• Learn about the precedence and associativity of arithmetic operations

• Examine shortcut arithmetic operators

• Use other unary operators

• Evaluate Boolean expressions

• Perform operations on struct fields

Page 3: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 3

Using C++ Binary Arithmetic Operators

• Five simple arithmetic operators:– the addition operator (+)– the subtraction operator (–)– the multiplication operator (*)– the division operator (/)– the modulus operator (%)

Note that they are all binary operators

Page 4: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 4

Using C++ Binary Arithmetic Operators (continued)

Page 5: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 5

Using C++ Binary Arithmetic Operators (continued)

• Addition, subtraction, multiplication, division, or modulus of any two integers results in an integer– For example, 7 / 3 evaluates to 3

• Mixed expression: operands have different data types– For example, 3.2 * 2

• Unifying type: – Data type of the value that occupies more memory– All types in the expression are temporarily converted

to a unifying type

Page 6: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 6

Using C++ Binary Arithmetic Operators (continued)

• The order of precedence of unifying types from highest to lowest– long double– double– float– unsigned long– long– unsigned int– int– short– char

Page 7: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 7

Explicit cast

Page 8: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 8

Page 9: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 9

Using C++ Binary Arithmetic Operators (continued)

• Cast: transform a value to another data type

• Implicit cast: automatic cast, or transformation, that occurs when you assign a value of one type to a type with higher precedence– int answer = 2.0 * 7

• Explicit cast: deliberate cast– intResult = static_cast<int>(doubleVariable);

– intResult = (int)doubleVariable;• static_cast<int>('A') // is 65 Preferred method

in C++

Page 10: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 10

Using Modulus

• Modulus (%) gives the remainder of integer division– 7 % 3 results in 1– -10 % 8 produces -2– -10 % -8 produces -2

• Can be used only with integers

• Can be used to extract digits from numbers– 6,543 % 10 is 3– 6,789 % 10 is 9

Page 11: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 11

Using Modulus (continued)

• Check digit: digit added to a number that validates the authenticity of the number

• A simple algorithm:1. Assume an account number named acctNum is

1234542. Remove the last digit3. Perform modulus on the new number with an

arbitrary value, say 7 (remainder = shortAcctNum % 7)

4. Compare the last digit of the original account number (lastDigit = acctNum % 10) with the remainder from the check digit calculation

Page 12: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 12

Precedence and Associativity of Arithmetic Operators

• Multiplication, division, and modulus are said to have higher arithmetic precedence–that is, they are performed first in an arithmetic statement with multiple operations

• Associativity: rule that dictates the order in which an operator works with its operands– In C++, most operators have left-to-right associativity

Page 13: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 13

Precedence and Associativity of Arithmetic Operators (continued)

• When C++ evaluates a mixed arithmetic expression, the following steps occur:1. The leftmost operation with the highest precedence

is evaluateda) If operands are the same type, the result is the same

data type

b) If the operands are different types, C++ performs an implicit cast and the result is the same type as the one that occupies more memory

2. Each subsequent *, /, or % operation is evaluated in the same manner from left to right

Page 14: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 14

Precedence and Associativity of Arithmetic Operators (continued)

• Steps (continued):3. The leftmost operation with the lower precedence

(+ and –) is evaluateda) If operands are the same type, the result is the same

type

b) If operands are different types, C++ performs an implicit cast and the result is the same type as the one that occupies more memory

4. Each subsequent + or – operation is evaluated in the same manner from left to right

• Use parentheses to override precedence rules

Page 15: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 15

Precedence and Associativity of Arithmetic Operators (continued)

Page 16: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 16

Shortcut Arithmetic Operators

• Two categories of shortcut arithmetic operators are:– Compound assignment operators– Increment and decrement operators

Page 17: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 17

Compound Assignment Operators

• Add and assign operator (+=)

• Subtract and assign operator (– =)

• Multiply and assign operator (*=)

• Divide and assign operator (/=)

• Modulus and assign operator (%=)

Page 18: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 18

Increment and Decrement Operators

• Prefix increment operator++count

• Postfix increment operatorcount++

• Prefix decrement operator--count

• Postfix decrement operatorcount--

Page 19: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 19

Using Shortcut Arithmetic Operators

Page 20: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 20

Other Unary Operators

• Positive value operator+5

• Negative value operator-8

• Address operator (&)&x

Page 21: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 21

Hexadecimal number

Other Unary Operators

Page 22: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 22

Evaluating Boolean Expressions

• Relational operators: evaluate the relationship between operands– Used to evaluate Boolean expressions

• Boolean expression: interpreted as true or false

Page 23: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 23

Evaluating Boolean Expressions (continued)

• The unary operator ! is the not operator: reverses the true/false value of an expression– cout<<(9>2); displays a 1– cout<<!(9>2); displays a 0– !0 is 1– !1 is 0– !5 is 0– !-5 is 0

• Don’t confuse = with ==

Page 24: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 24

Performing Operations on struct Fields

Page 25: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 25

Performing Operations on struct Fields (continued)

Page 26: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 26

You Do It: Using Arithmetic Operators

int a,b,c;double x,y,z;a = 13;b = 4;x = 3.3;y = 15.78;c = a + b;cout<<“a + b is ”<<c<<endl;z = x + y;cout <<“x + y is ”<<z<<endl;c = a / b;cout<<“a / b is ”<<c<<endl;c = a % b;cout<<“a% b is “<<c<<endl;

Page 27: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 27

You Do It: Using Arithmetic Operators (continued)

Page 28: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 28

Using Prefix and Postfix Increment and Decrement Operators

a = 2;

c = ++a;

cout<<“a is ”<<a<<“ and c is ”<<c<<endl;

a = 2;

c = a++;

cout<<“a is ”<<a<<“ and c is ”<<c<<endl;

Page 29: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 29

Using Prefix and Postfix Increment and Decrement Operators (continued)

Page 30: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 30

Using Operators with struct Fields

cout<<“Please enter a student's credit hours ”;cin>>oneSophomore.creditHours;cout<<“Please enter the student's grade point

average ”;cin>>oneSophomore.gradePointAverage;cout<<“The number of credit hours is ” << oneSophomore.creditHours<<endl;cout<<“The grade point average is ”<< oneSophomore.gradePointAverage<<endl;hoursRemaining = HOURS_REQUIRED_TO_GRADUATE

oneSophomore.creditHours;cout<<“This student needs ”<<hoursRemaining<< “ more credit hours to graduate”<<endl;

Page 31: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 31

Using Operators with struct Fields (continued)

Page 32: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 32

Summary

• There are five simple binary arithmetic operators: addition (+), subtraction (–), multiplication (*), division (/), and modulus (%)

• When you mix data types in a binary arithmetic expression, the result is always the same type as the type that takes the most memory to store

• Several shortcut operators for arithmetic exist, such as +=, prefix ++, and postfix ++

Page 33: Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions

Object-Oriented Programming Using C++, Third Edition 33

Summary (continued)

• Boolean expression evaluates as true or false– In C++, the value 0 is always interpreted as false; all

other values are interpreted as true

• Fields contained within structures are used in arithmetic and Boolean expressions in the same manner as are primitive variables