Week 2 - Unit Testing, More OOP and Composition

Learning objectives

  • Describe and apply the principles of Unit testing and Test Driven Development
  • Use objects as input and output for functions
  • Recognize how and when to appropriately use const and static class members
  • Use composition to build objects from other objects

Schedule

Day 1

  • Holiday

Day 2

Day 3

Day 4

Object Passing & Objects with Arrays

Read 10.3-10.4. These videos help explain them:

Static, Const, and Interacting Objects

Read 10.5-10.6, 10.10 - Static, Const, Interacting Objects.

The book does not have good examples of interacting objects (a Circle that takes a Point as a parameter). review these videos, and the cs162Code samples for those topics:

Do CPPLab - OOThinking.

OODesign

Read 10.7 and 10.11 for general advice about designing objects - I have one extra point to add:

Single Representation: We only want to represent each piece of information once. If you already have a piece of data represented in a class, or the information to calculate it, you should not store it again. For example, in the Circle class, we might have a getArea() function but do not have an area variable. getArea() can calculate the area whenever it is needed by using the radius that we already store. If we stored the area explicitly, it would be much easier to do something dumb like change the radius of the circle and forget to update the area. Only if after testing do we realize the calculation takes an inordinate amount of time (say we need to get a circle's area thousands of times per second) should we worry about explicitly storing the calculated value.

Composition

Read 10.8 about Composition. I think the treatment there is a bit weak, so I also recommend reading learncpp.com Ch 23.1/23.2 about Composition.

This video walks through a sample of using composition.