CS 112 - Week 12 Lecture 1 - 2022-11-08

TODAY WE WILL
*   announcements
*   start our intro to INHERITANCE
*   prep for next class

*   sorry for Homework 9's delay!
    ...watch for class emails as its parts are
    available, with an at-least-1st-attempts deadline
    of 11:59 pm on Friday, November 18

*   Reading -
    *   inheritance - Savitch Ch. 15

=====
intro to inheritance!
=====
*   when you want a SPECIALIZED type of an existing
    type -- a specialized class of an existing class,
    in particular -- you can write a special kind of
    class that INHERITS features from another class;

    ...this is when there is an IS-A relationship
    between the existing class and the desired
    specialized class;

    like, you have a Car class, and want a specialized
    RaceCar class -- and in your consideration,
    you consider that a RaceCar instance IS A
    kind of Car;
    
    ^ that's an IS-A relationship;

*   you COULD use Car as inspiration and write
    a completely independent RaceCar class --
    BUT you can, in many object-oriented languages
    including C++,
    declare RaceCar as a SUBCLASS of Car,
    inheriting common features from Car
    (and having to write less code as a result... 8-) )

    ...possibility of code reuse, here!
       and maybe fewer bugs?

*   of course, CS is not great at standardizing its
    terms...!
    might say any of the following:
    I have a superclass Car, I can use it to help create
        a subclass RaceCar;

    (textbook uses base class and derived class)
    I have a base class Car, I can use it to help
        create a derived class RaceCar

    (and sometimes... parent class and child class)
    I have a parent class Car, I can use it to help
        create a child class RaceCar

=====
syntax for declaring a derived class (subclass, child
                                      class)
=====

class MyDerivedClass : public MyBaseClass
{
    ...
};

*   (and #include the header file for MyBaseClass,
    #include "MyBaseClass.h"
    )

*   JUST from the above,
    MyDerivedClass has
    *   all the data fields,
    *   MOST of the methods
    ...of MyBaseClass

*   and you can add additional methods to
        MyDerivedClass,
    and redefine and overload inherited methods
        from MyBaseClass;

*   (and you aren't limited to two levels
    here --
    a derived class can itself be the base class
        for another class!

    and you can derive MORE than one class from
        a single class!

    and then there's also multiple inheritance
        (having more than one base class...!
	but that's a topic for another day...!)

=====
caveats when declaring a derived class
=====
*   it DOES inherit data fields --
    BUT if those data fields are private
    (as they should be), the derived class
    cannot directly access them.

    (it can use all the lovely public
    accessors and mutators you carefully
    wrote for those data fields in your
    base class!)

*   MOST methods are inherited --
    here are a few exceptions:
    *   constructor:
        you are expected to provide a constructor
	for the new derived class;

        and THIS constructor in the derived
	class is expected to CALL the constructor
	for the base class to initialize the
	inherited data fields
	(and then the derived class constructor
	initializes all the data fields new to
	this derived class)

    *   there is special syntax for a derived
        class constructor calling a base class
	constructor:

MyDerivedClass::MyDerivedClass(...) : MyBaseClass(....)
{
    //initialize data fields new to this derived class
}

*   we added an overloaded == operator to our Point class;

*   we started, but did not yet finished,
    writing a derived class ColorPoint
    (from derived class Point)