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
- Write code using exceptions and try/catch
Suggested pacing
Monday
- Multiple inheritance and interfaces
- Start assignment 4
Tuesday
- Inheritance Design
- Do design activity
Wednesday
- Operator Overloading
- CPPLab - Operator Basics
Thursday
- Exceptions
- Do Exceptions CPPLab
Friday
- Quiz 2 - it covers up through exceptions. You do not have to finish all the CPPLab problems before you take it, but you should be comfortable with the concepts of operator overloading and exceptions when you take it.
Multiple Inheritance and Interfaces
Read learncpp.com 24.9 and the Interface classes part of 25.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:
Inheritance Design
This video introduces using inheritance to do class design:
Look in the files area for the Inheritance Design Worksheet. It is a quick pen-and-paper exercise based on the video.
Operator Overloading
Operator overloading is not available in every language,
but is critical to being able to do things in C++. We use it to
allow our custom data types to work with existing algorithms - if
we define how to interpret the <
operator for our custom data
types, then any algorithm that depends on using <
will now be able
to work on our data type.
Read 14.1-14.3, and 14.7 on basic operator overloading. Just skim 14.2... it shows you what a class looks like without using operators - like it might look in Java. We aren't too concerned with all the details of how the Rational class actually works.
This video introduces the basics of operator overloading:
Do CPPLab Operator Basics.
We are skipping the rest of Ch 14. There are a few other cool tricks in it, but we don't need any of them in the way we need some of the basics. See below for more.
Optional - Operators - Trickier Topics
None of the rest of Ch14 is required, but you might still find it interesting.
In particular, 14.9 covers how to make our classes work with <<
and >>
so
we can write something like cout << person1;
instead of cout << person1.toString();
.
This video covers some of them:
Exceptions
Exceptions are a mechanism used by C++ and many other languages for error handling. The basic idea is pretty simple - a way to say "we have a problem" all the way up the call stack (the functions that have called other functions to get to where we are) until we find a part of the code that knows how to deal with an error. It allows low-level components that recognize an error ("Ack, I can't open the database!") to abort what is happening in a controlled manner and send a message to higher-level code that knows how to deal with it in a way appropriate to the actual program. For an interactive program, the correct response might be to prompt the user for a new password. For an automated program that runs on a server, the best thing to do might be to record an error message to a file and move on.
Read 16.1-16.3, 16.8. Skim 16.5 & 16.6 (the concepts aren't super deep... you should be able to skim rapidly) and watch these two videos:
Do CPPLab Exceptions