1 / 52

Relational Expressions

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: <, >, <=, >=, = =, !=

marci
Download Presentation

Relational Expressions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Program Development Using C++

  2. 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 Object-Oriented Program Development Using C++

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

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

  5. 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 Object-Oriented Program Development Using C++

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

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

  8. 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 Object-Oriented Program Development Using C++

  9. 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 Object-Oriented Program Development Using C++

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

  11. 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 } Object-Oriented Program Development Using C++

  12. 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 Object-Oriented Program Development Using C++

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

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

  15. 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 Object-Oriented Program Development Using C++

  16. 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 Object-Oriented Program Development Using C++

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

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

  19. 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 Object-Oriented Program Development Using C++

  20. 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 Object-Oriented Program Development Using C++

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

  22. 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 Object-Oriented Program Development Using C++

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

  24. 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 Object-Oriented Program Development Using C++

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

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

  27. 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 Object-Oriented Program Development Using C++

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

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

  30. 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 Object-Oriented Program Development Using C++

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

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

  33. 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 Object-Oriented Program Development Using C++

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

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

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

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

  38. 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 Object-Oriented Program Development Using C++

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

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

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

  42. 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 Object-Oriented Program Development Using C++

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

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

  45. 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 Object-Oriented Program Development Using C++

  46. 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 Object-Oriented Program Development Using C++

  47. 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 Object-Oriented Program Development Using C++

  48. 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 Object-Oriented Program Development Using C++

  49. 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: 2n • n corresponds to number of if-else statements • Choose critical elements to test • Legal and limiting input values Object-Oriented Program Development Using C++

More Related