1 / 22

C++: An Object-Oriented Approach More Class Development- CMP109 w 2007

C++: An Object-Oriented Approach More Class Development- CMP109 w 2007. Containers and items access Memory – object creation UML - class design. Object Creation. Object requires memory and initial value Kernel language provides this through declarations that are definitions

craig-mcgee
Download Presentation

C++: An Object-Oriented Approach More Class Development- CMP109 w 2007

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. C++: An Object-Oriented ApproachMore Class Development- CMP109 w 2007 • Containers and items access • Memory – object creation • UML - class design

  2. Object Creation • Object requires memory and initial value • Kernel language provides this through declarations that are definitions • void foo(){ int n = 5; double z[10] = { 0.0}; struct gizmo { int i, j; } w = { 3, 4}; . . .}

  3. Comments on the foo() Function (1 of 2) • Objects created at block entry when foo() is invoked • gizmo object w requires eight bytes to represent its two integer members • Array of double object z requires ten times sizeof(double) to store its elements • At exit from foo() deallocation occurs

  4. Comments on the foo() Function (2 of 2) • Typical implementation uses a run-time system stack • int object n on a system with four-byte integers gets this allocated off stack and initialized to 5 • System provides for construction and initialization of objects

  5. Design • Elements of design • Relationships: similar to UML class diagrams or CRC cards • Responsibilities: behavior • Collaborations: other classes that cooperate

  6. Man-Page for Design Patterns (2 of 3) Applicability Recognizing where to apply Structure UML notation for pattern or CRC Participants Classes or objects and responsibilities Collaborations Participants work together

  7. UML - Unified Modeling Language • Graphical depiction of class relationships that helps coder design, document, and maintain object-oriented code • Simple diagram is rectangle that represents class • Depict class • Name, placed at top • Data members, placed in middle • Methods, placed at bottom

  8. UML person nameagebirthday bday() UML Diagram for Class person

  9. Handle Class in UML ch_stk_rep ch_stack tops[ ]push() Pop() ptr push()pop()

  10. Class Diagram • Describes types and relationships • Useful documentation • Some Systems provide automated tools to develop UML with coding • Rational Rose • Relationship depicted by UML includes part-whole, or aggregation, relationship (HASA)

  11. The ch_stack Program (1 of 4) • class ch_stack {public:   void  reset() { ptr -> reset(); }   void  push(char c)      { ptr->push(c); }   char  pop() { return ptr->pop(); }   char  top_of() const { return ptr->top_of(); }

  12. The ch_stack Program (2 of 4) •   bool  empty() const      { return ptr -> empty(); }   bool  full() const      { return ptr -> full(); }private:   ch_stk_rep* ptr;      // opaque pointer};

  13. The ch_stack Program (3 of 4) • class ch_stk_rep {public:   void  reset() { top = EMPTY; }   void  push(char c)      { s[top++] = c; }   char  pop() { return s[top--]; }   char  top_of() const { return s[top]; }

  14. The ch_stack Program (4 of 4) •   bool  empty() const      { return (top == EMPTY); }   bool  full() const      { return (top == FULL); }private:   enum { max_len = 100, EMPTY = -1,           FULL = max_len - 1 };   int   top;   char  s[max_len];};

  15. Handle • Handle type such as ch_stack has representation class class ch_stk_rep pointer • Representation class used to concretely implement handle class • Bridge or handle design pattern

  16. Object Oriented Design Focus • Identification of objects • Hierarchical organization of classes • Implementation and design process merge • Reuse of objects • Control & process is not as often reusable • "There is no widely accepted OOD method" • -Winblad, et. al.

  17. Using Existing Designs • Mathematical and scientific community standard definitions readily coded as ADTs • Complex numbers Rationals • Matrices Polynomials • Programming community has experience with standard container classes • Stack Associative array • Binary tree Queue

  18. Invertibility in Design • Invertibility - inverse member functions • In math, addition and subtraction are inverses • In a text editor, add and delete are inverses • Some commands are their own inverses: negation

  19. Use the UNDELETE function to get it back! Give me back my file! Success of Invertibility in Nonmath Context Thank goodness for the UNDO command!

  20. Completeness of Design • Completeness best seen in Boolean algebra, where nand operation suffices to generate all possible Boolean expressions • Boolean algebra taught with negation, conjunction, disjunction as basic operations • Completeness by itself is not enough • Large set of operators is more expressive

  21. Orthogonality in Design • Each element of design should integrate and work with all other elements • Elements of basic design should not overlap or be redundant • System that manipulates shapes functions should be able to position shape anywhere • Horizontal moveVertical moveRotate operation

  22. Dr. P’s Prescriptions • Invest in existing libraries • Avoid deep hierarchies • Avoid whiz-bang features • One task: one member function • Avoid non-standard operator overloading

More Related