220 likes | 335 Views
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
E N D
C++: An Object-Oriented ApproachMore 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 • void foo(){ int n = 5; double z[10] = { 0.0}; struct gizmo { int i, j; } w = { 3, 4}; . . .}
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
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
Design • Elements of design • Relationships: similar to UML class diagrams or CRC cards • Responsibilities: behavior • Collaborations: other classes that cooperate
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
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
UML person nameagebirthday bday() UML Diagram for Class person
Handle Class in UML ch_stk_rep ch_stack tops[ ]push() Pop() ptr push()pop()
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)
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(); }
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};
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]; }
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];};
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
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.
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
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
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!
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
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
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