Presentation is loading. Please wait.

Presentation is loading. Please wait.

2Object-Oriented Program Development Using C++ 3 Relational Expressions Decision-making: comparison of two numerical values Relational expression –Also.

Similar presentations


Presentation on theme: "2Object-Oriented Program Development Using C++ 3 Relational Expressions Decision-making: comparison of two numerical values Relational expression –Also."— Presentation transcript:

1

2 2Object-Oriented Program Development Using C++

3 3 Relational Expressions Decision-making: comparison of two numerical values Relational expression –Also known as a condition –Evaluates to 1 (true) or 0 (false) –Simple type: two operands and relational operator Six relational operators:, =, = =, != Char type coerced to int for comparison Strings compared at character level

4 4Object-Oriented Program Development Using C++ Figure 5-1 Anatomy of a Simple Relational Expression

5 5Object-Oriented Program Development Using C++ Table 5-1 Relational Operators for Primitive Data Types

6 6Object-Oriented Program Development Using C++ Logical Operators Complex expressions –Comprised of simple relational expressions –Logical connectors required AND ( && ), OR ( | | ), NOT (!) Precedence –AND and OR lower than relational operators –NOT (unary) higher than relational operators Associativity –AND and OR: left to right –NOT: right to left

7 7Object-Oriented Program Development Using C++ Table 5-2 Operator Precedence

8 8Object-Oriented Program Development Using C++ Table 5-3 Equivalent Expressions

9 9Object-Oriented Program Development Using C++ A Numerical Accuracy Problem Caveat: equality comparison of floating-point types –Avoid use of equality operator –Computer representation slightly inaccurate Work around problem –If possible, replace floating-point data with integers –If not, use alternative syntax abs (operandOne - operandTwo) < EPSILON EPSILON is very small value such as.0000001

10 10Object-Oriented Program Development Using C++ The if-else Statement if-else statement: fundamental selection structure Purpose: alter instruction sequence Syntax: if (expression) statement1; else statement2; Expression –Relational expression –May consist of single variable, such as type bool

11 11Object-Oriented Program Development Using C++ Figure 5-2 The if-else Flowchart

12 12Object-Oriented Program Development Using C++ Compound Statements Compound statement –Sequence of statements enclosed by braces –Supports construction of complex selection structures Syntax: if (expression){ //sequence of statements } else{ //sequence of statements }

13 13Object-Oriented Program Development Using C++ Block Scope Code block –Set of compound statements –May be nested Variable scope –Variable meaningful between closing braces –Name conflict resolved by location Inner block takes precedence over outer Compiler seeks declaration moving inside out

14 14Object-Oriented Program Development Using C++ One-Way Selection One-way selection: excludes else portion Syntax: if (expression) statement; // code block might follow Non-zero expression triggers statement execution

15 15Object-Oriented Program Development Using C++ Figure 5-3 Flowchart for the One-Way if Statement

16 16Object-Oriented Program Development Using C++ Problems Associated with the if-else Statement Semantic problems –Logical form –Correct by reviewing original design Syntax problems –Misuse of assignment operator (=) in expression Assigns value to operand Non-zero assignments always evaluate to true –Use equality operator (= =) for comparisons

17 17Object-Oriented Program Development Using C++ Nested if Statements Selection structures may be nested –if or if-else statements nest in either (or both) parts of larger if-else statement –Nesting may be deeper than one level Syntax caveat –Use braces to define logical unit –Misused (or missing) braces may cause fatal logical error

18 18Object-Oriented Program Development Using C++ Figure 5-4a The if-else Nested Within the if Part

19 19Object-Oriented Program Development Using C++ Figure 5-4b The if-else Nested Within the else Part

20 20Object-Oriented Program Development Using C++ The if-else chain if-else chain: useful, readable form of nesting Syntax: if (expression1) statement1; // may be code block else if (expression2) statement2; // may be code block else statement3; // may be code block Additional else-if components may be added

21 21Object-Oriented Program Development Using C++ The Switch Statement Switch statement –Variation on chained if-else statement –Control “switches” to case based on condition –Caveat: condition evaluates to an integer Cases may include complex structures Break statement follows each case Default statement is optional

22 22Object-Oriented Program Development Using C++ Figure 5-5 The Expression Determines an Entry Point

23 23Object-Oriented Program Development Using C++ Program Design and Development: Introduction to UML Think and plan before coding –Primary concern: classes and objects needed Uniform Modeling Language (UML) –Supports object-oriented design –Set of rules and diagrams Focus on four UML diagrams –Class, object, state, and sequence –Analogy to specialized blueprints

24 24Object-Oriented Program Development Using C++ Figure 5-7 Basic UML Symbols and Notation

25 25Object-Oriented Program Development Using C++ Class and Object Diagrams Commonality of class and object diagrams –Both employ rectangular containers –Names, attributes, behaviors found in both Chief differences –Class diagram: describes classes and relationships –Object diagram: describes objects and relationships –Class at higher level of abstraction –One class can generate many particular objects

26 26Object-Oriented Program Development Using C++ Figure 5-6 A Class and Object Representation

27 27Object-Oriented Program Development Using C++ Figure 5-8 Including Attributes in UML Class and Object Diagrams

28 28Object-Oriented Program Development Using C++ Class and Object Diagrams (continued) Two aspects to class attributes –Data type: may be primitive or class –Visibility: where variable may be seen (or used) Plus (+) sign indicates public Minus (-) sign indicates private No sign for protected status Operations –Become methods that transform attributes –Follow attribute sign convention for visibility

29 29Object-Oriented Program Development Using C++ Figure 5-9 A Class with Attributes

30 30Object-Oriented Program Development Using C++ Figure 5-10 Including Operations in Class Diagrams

31 31Object-Oriented Program Development Using C++ Relationships Three basic relationships –Association, aggregation, generalization Association –Signified by phrases such as “works for”, “has a” –Indicated by straight line connecting classes/ objects –Multiplicity Numerical relationship between objects/classes Quantities: 0, 1, many, unlimited

32 32Object-Oriented Program Development Using C++ Figure 5-11 An Association

33 33Object-Oriented Program Development Using C++ Table 5-4 UML Association Notation

34 34Object-Oriented Program Development Using C++ Relationships (continued) Aggregation –One class/object consists of other classes/objects –Visualize as relation of whole to parts –Symbolized by diamond Generalization –Relationship between class and its refinement –Ford Taurus is a type of automobile

35 35Object-Oriented Program Development Using C++ Figure 5-12 Single-Level Aggregation

36 36Object-Oriented Program Development Using C++ Figure 5-13 Another Single-Level Aggregation

37 37Object-Oriented Program Development Using C++ Figure 5-14 Multi-Level Aggregation

38 38Object-Oriented Program Development Using C++ Figure 5-15 A Generalization Relationship

39 39Object-Oriented Program Development Using C++ Application: A Date Class Stage one: identify and name objects Stage two –Define attributes Month, day, and year Integer data types Stage three –Create object diagram –Object diagram shows assignment of values

40 40Object-Oriented Program Development Using C++ Figure 5-16 Initial Date Class Diagram

41 41Object-Oriented Program Development Using C++ Figure 5-17 First Refinement-Date Class Diagram

42 42Object-Oriented Program Development Using C++ Figure 5-18 A Date Object Diagram

43 43Object-Oriented Program Development Using C++ Application: A Date Class (continued) Stage four –Identify operations that become methods –Basic operations: constructor, mutator, accessor –Additional operations: queries with comparisons Stage five –Construct second refinement of class diagram –Name, attributes, operations detailed –Visibility denoted

44 44Object-Oriented Program Development Using C++ Table 5-5 Required Operations for the Date Class

45 45Object-Oriented Program Development Using C++ Figure 5-19 Second Refinement-Date Class Diagram

46 46Object-Oriented Program Development Using C++ Explanation of the Basic Date Class Default constructor –Initializes month, day, year –cout object echo prints default data Overloaded constructor –Initializes Date object with parameterized interface –cout object echo prints default data setDate( ): almost identical to overloaded constructor showDate( ): accessor manipulates output stream

47 47Object-Oriented Program Development Using C++ Using the Basic Date Class Constructors instantiate two Date objects Syntax of object declaration –If default constructor used, follow primitive form –If overloaded constructor used, supply arguments Accessors retrieve data in attributes Output modified according to current values

48 48Object-Oriented Program Development Using C++ Simplifying the Code Opportunities for optimization exist Call internal methods when possible –Eliminate redundant code –Do not reinvent your own wheel –Example: call setDate( ) in constructors Dealing with other redundancies –Target common or repeated actions –Fold action into method

49 49Object-Oriented Program Development Using C++ Adding Additional Class Methods Virtue of OO programming: scalability Add isLeapYear ( ) –Based on leap year algorithm –Returns a Boolean value Add dayOfWeek ( ) –Based on Zeller’s algorithm –Returns an integer value Include appropriate declarations, definition, visibility

50 50Object-Oriented Program Development Using C++ A Closer Look at Program Testing Added complexity increases likelihood of errors Selection structures introduce new control paths –Ideally, programmer tests each path –Not physically possible –Growth of test paths: 2 n –n corresponds to number of if-else statements Choose critical elements to test –Legal and limiting input values

51 51Object-Oriented Program Development Using C++ Summary Relational expression (condition) evaluates to 1 (true) or 0 (false) Relational operators:, =, = =, != Logical connectors: AND (&&), OR (| |), NOT (!) Basic selection structure: if-else statement Compound statement: set enclosed by braces

52 52Object-Oriented Program Development Using C++ Summary (continued) Selection variations: nesting, chained if-else, switch Uniform Modeling Language (UML): OO design rules/templates Basic UML diagrams: class, object, state, sequence Use UML tools to construct/implement Date class Testing: selectivity avoids combinatorial explosion


Download ppt "2Object-Oriented Program Development Using C++ 3 Relational Expressions Decision-making: comparison of two numerical values Relational expression –Also."

Similar presentations


Ads by Google