CS 112 - Week 13 Lecture 1 - 2022-11-15 TODAY WE WILL * announcements * continue intro to INHERITANCE * start intro to exception-handling in C++ * prep for next class * Should be working on Homework 9! submitting at least 1st attempts by this 11:59 pm Friday, Nov. 18, * Reading: * inheritance: Savitch Ch. 15 * exception-handling: Savitch Ch. 16 (and also another link that will be posted with these notes) isocpp.org/wiki/faq/exceptions ===== continuing intro to inheritance ===== calling a base class' version of an OPERATOR ----- * can you call the base class' version of an OPERATOR (for a derived class object) like we did for a method? ...ALMOST. You'd still say BaseClass::, BUT you have to put the word operator BEFORE the actual operator in that expression: ColorPoint myCP; ... myCP.Point::operator==(desired_operand) * and within a derived class method, BaseClass::operator==(desired_operand) ===== a few words on derived classes, base classes, and types ===== * when you declare a local variable: string lookity; // lookity is of type string * and this is true of instances of classes users write, also: Point center; // center is of type Point * BUT -- when you have a derived class, objects of that class are considered to have MULTIPLE types! ...both the derived class, AND its base class! (and base classES, and their base classES, etc.!) SO: ColorPoint cp; // cp is of type ColorPoint // AND of type Point ===== starting intro to exception-handling in C++ ===== * see also: * exception-handling: Savitch Ch. 16 (and also another link that will be posted with these notes) isocpp.org/wiki/faq/exceptions * exception-handling: a way to be more specific or intentional about what should happen if something "bad" happens while a program is running * ideally, you use exception handling to handle "exceptional" situations, less-common situations; * the isocpp.org site says: "C++ exceptions are designed to support error handling." * In C++, a library or your code provides a mechanism that signals when something unusual or untoward happens -- that's called: throwing an exception * at ANOTHER place in your program, you place the code that deals with that exceptional case; that's called: handling the exception (I've also seen: catching the exception ) * caveat: do not use exceptions for regular control flow...!!!!!! ...it is slow compared to regular control flow, and will confuse other C++ programmers who have to deal with your code; * THAT said -- how do you do it in C++? you use the try-throw-catch threesome: try block throw statement catch block * try block: try { statement; statement; ... } * the statements within should be statements where an exception might be thrown; * throw statement: throw expression_for_value_to_be_thrown; * C++ lets you throw an expression of any type! * BUT that said, it can be very useful to throw objects of type exception (from the built-in exception class or a class derived from that class) to use this: #include <exception> * and objects of this type can provide more information than some other thrown type might * for example: runtime_error is one of the derived classes from exception #include <exception> using namespace std; class MyException: public runtime_error { public: MyException(); ... } ... MyException::MyException(): runtime_error("MyException") { ... } ... void my_fun { ... throw MyException(); } * catch block catch (exception_type exc_name) { } generally: try { ... } catch (t1 e1) { ... } catch (t2 e2) { ... } * if an exception is thrown in the try block, control passes immediately to the FIRST catch block that "matches" the type of the exception thrown * continuing discussion of C++ exceptions on Thursday;