CPSC 427a: Object-Oriented Programming

Michael J. Fischer

Lecture 12
October 11, 2011

Interacting Classes and UML (continued)

Association Relationship The association relationship can be diverse. Some developers label text describing the relationship on the edge connecting two associated classes.

Later in the course we will explore and see more examples.

Accessing B in A’s methods

Access patterns:

If A knows B only through parameter or local variables, we also say that A uses B. The use relationship is generally considered to be a weak relationship.

“Law” of Consistency/Encapsulation

Relation of B::var or b.var in A is typically not recommended because it violates encapsulation and may lead to inconsistent state.

Why is the design below not desirable?

class SpeedDataCollection{  
  // add a new data value  
  public void addValue(int speed);  
  // return average speed  
  public double averageSoFar;  

Limiting coupling between classes

Chaining such as c.b.func() is typically not recommended as it increases coupling.

For example, assume class A has a data member Dog* dog. One way to ask the dog object to move is dog->leg()->walk(). But this is less desirable than calling dog->walk().

In OO design, this is called the “Law” of Demeter, also called “Law” of Least Knowledge:

This principle has other names such as Delegation and Do not talk to Strangers.

“Law” of Demeter

Formally, the “Law” of Demeter for functions requires that a method M of an object A may only invoke the methods of the following kinds of objects:

One can consider layered architecture of many systems (e.g., the layered network architecture) as following this design guideline.

Review for Exam

Goals of OO Programming

Insertion sort example

Compiling and linking

Stages of compilation:

Compiler errors

Errors produced at each stage:

Tool set

What does each of these tools do?

When should they be used?

C++ goodies

Stream I/O

Functions and methods

Variables and data

BarGraph demo

More on variables


Assignment and copying

Five kinds of memory errors

  1. Memory leak—Dynamic storage that is no longer accessible but has not been deallocated.
  2. Amnesia—Storage values that mysteriously disappear.
  3. Bus error—Program crashes because of an attempt to access non-existent memory.
  4. Segmentation fault—Program crashes because of an attempt to access memory not allocated to your process.
  5. Waiting for eternity—Program is in a permanent wait state or an infinite loop.

C++ bells and whistles


Derived classes

Polymorphic derivation