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 appropriate use const and static class members
  • Use composition to build objects from other objects

Suggested pacing

Day 1

  • Intro to Source Control
  • Do Git Tutorial Part 1

Day 2

  • Objects, Functions, & Arrays
  • Static, Const, Interacting Objects
  • CPPLab - OOThinking

Day 3

  • OO Design

Day 4

  • Composition
  • CPPLab - Composition

Activity Outline

Source Control & Git

Source control is going to take you a while to master—don't worry about doing it all this week. Watch the videos and at least work through part 1 of this Git Tutorial, which will walk you through making your own clone of the CS162 repository.

These videos introduce the basic idea behind source control, what git is, and how we will use it:

None of these are required reading, but if you want to learn more about git, here are some good references:

Git Extra Info

Visual Git Reference
Learn Git Branching
Pro Git Book

Object Passing & Objects with Arrays

Read 10.3-10.4. This video helps 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 which we already store. If we stored 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 16.1/16.2 about Composition.

This video walks through a sample of using composition.