c++: an object-oriented approach more class development- cmp109 w 2007

22
1 C++: An Object-Oriented Approach More Class Development- CMP109 w 2007 Containers and items access Memory – object creation UML - class design

Upload: craig-mcgee

Post on 02-Jan-2016

18 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT 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 topData members, placed in middleMethods, placed at bottom

8

UML

person

nameagebirthday

bday()

UML Diagram for Class person

9

Handle Class in UML

ch_stk_rep

tops[ ]

push()Pop()

ch_stack

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)

ch_stack

ptr

push()

ch_stk_rep

tops[]

push()

Handle Class in UML

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

Success of Invertibility in Nonmath Context

Thank goodness for the UNDO command!

Use the UNDELETEfunction toget it back!

Give me back my file!

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

Invest in existing libraries

Avoid deep hierarchies

Avoid whiz-bang featuresOne task: one member function

Avoid non-standard operator overloading

Dr. P’s Prescriptions