Week 4 - Inheritance

Learning objectives

Upon finishing this week, you should be able to:

  • Use inheritance to extend classes
  • Identify appropriate uses for abstract classes
  • Describe how virtual functions differ from regular ones and enable polymorphism

Suggested pacing

Day 1

  • Inheritance
  • Do CPPLab Inheritance 1

Day 2

  • Virtual Functions and Polymorphism
  • Do CPPLab Inheritance 2

Day 3

  • Protected, Abstract classes

Day 4

  • Multiple inheritance and interfaces
  • OO Design
  • Do design activity

Activity Outline

Inheritance Basics

Inheritance is one of the fundamental tools for code reuse in Object Oriented programming. It allows one class to define itself as a specialized version of another class. Learncpp.com has a nice overview of why we care about inheritance and the conceptual idea.

Read 15.1-15.5. In 15.4 don't worry too much about what a destructor is and why we need them * we will hit them later.

These videos offer some tips:

Read 15.9 and watch this video about the protected access modifier:

Do CPPLab Inheritance 1

Virtual Functions, Abstract Classes and Dynamic Casting

Read 15.6-15.11

These videos touch on some of the key ideas:

Do CPPLab Inheritance 2

Multiple Inheritance and Interfaces

Read learncpp.com 17.9 and the Interface classes part of 18.7. It is OK to skim the multiple inheritance page * our main goal it to understand what the pitfalls of multiple inheritance are and why many programmers avoid it. On the interfaces page, our main goal is to understand what an interface is, and why they are considered a safe way to use multiple inheritance.

These two videos briefly introduce the ideas of multiple inheritance and interfaces:

Design

This video introduces using inheritance to do class design:

Look in the classroom files for the Inheritance Design Activity. It is a quick pen and paper exercise based on OO Design video.