CS 112 - Week 8 Lecture 2 - 2022-10-13

TODAY WE WILL
*   announcements
*   continuing with the "big-3",
    destructor, copy constructor,
    overloaded assignment operator
*   prep for next class

*   current reading:
    Savitch - Chapter 11 - Section 11.4 - Classes
        and Dynamic Arrays

====onward with the big 3!

*   we ended last time with a Team class
    that is *mostly* aiight, but will have some
    issues (at least potential) due to not
    having the "big 3" <-- the defaults don't
    do all we would like;

    *   BUT! we never delete the players array we
        allocated using new -- that's a memory leak!
	(and a destructor method can help us take care of
	this)

    *   BUT! with both the copy constructor and the
        overloaded assignment, when the players array
	is copied, JUST its address is copied,
	so two objects end up "sharing" the same
	players array <-- shallow copy! and we'd like
	a deeper one

=====
let's start with a destructor method!
=====
*   destructor cannot be overloaded,
    and ALWAYS it is named as a tilde ~ followed by
    the class name,

    and it expects no arguments, and like a constructor
    you do not specify a return type.

    class Team's destructor would be named ~Team
    and in Team.h its header should be:

    ~Team();

    *   it is called by the system when an object
        of that class goes out of scope

    *   it SHOULD do any cleanup needed when that
        happens! (For example: using delete for
	any dynamic memory! BUT could also be things
	like updated a static variable, I think)

=====
copy constructor
=====
*   called by the system in situations where a copy
    of an object is needed
    *   for example, when an argument needs to be
        copied into a pass-by-value parameter

    *   also called automatically when a function
        returns a value of the class type

*   it is a constructor, its name must the class'
    name

    DesiredClass(const DesiredClass& a_desired_class);

    its job is to set the data fields for a new instance
    of the class such that they are an INDEPENDENT copy
    of the argument object;

====
overloaded assignment operator
====
*   you'll see it typically ends up having parts
    very like those in the destructor and in
    the copy constructor...!

    (plus a dash of protection against self-assignment!)

*   used when a statement tries to use =
    to assign one object to another!

*   SO: (source: ...caltech...cpp-ops.html <-- FILL IN)

    0. check for self-assignment
    1. deallocate any memory that the LHS object is
       using
    2. allocate needed memory to hold the copy of the
       contents on the RHS
    3. copy the values from RHS into the calling instance
    4. return *this (return the object assigned to
       the LHS

    And here's the header syntax:

    DesiredClass& operator=(const DesiredClass &rhs);