Week 5 - Operators

Learning objectives

Upon finishing this week, you should be able to:

  • Create classes that work using standard operators
  • Write code using exceptions and try/catch
Quiz 2 is open this Thurs-Sunday. It covers weeks 3-5, including operators and exceptions.

Suggested pacing

Day 1

  • Operator Overloading
  • CPPLab - Operator Basics

Day 2

  • Exceptions
  • Do Exceptions CPPLab

Day 3

  • Midterm & Quiz Review
  • Finish up CPPLabs

Day 4

  • Take Midterm in class
  • Quiz 2 available

Activity Outline

Operator Overloading Basics

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 is showing 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.

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 appropriately in the actual program. For a 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

Midterm

The midterm covers weeks 1-4. It is a pen and paper test and will be taken in class on Thursday.

Quiz 2

The second quiz wraps up the OOP part of this course, including the material from the start of the week. Make sure to complete the operators and exceptions material before trying to take it.

No Weekly Assignment

There is no weekly assignment this week. Focus on getting through the CPPLabs and the midterm and quiz.

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: