CS 112 - Week 7 Lecture 2 - 2022-10-06

TODAY WE WILL
*   STOP 6.5 - dynamic arrays of objects,
               arrays of pointers to objects,
	       dynamic arrays of pointers to objects...!

*   start (but not finish)
    STOP 7 - dynamic memory and classes
             (the "big 3")
*   prep for next class

*   get those first attempts at Homework 5's short-answer
    and programming problems in by 11:59 pm Friday,
    October 7

*   watch for class emails as parts of Homework 6 become
    available

*   Current reading: Savitch Ch. 9, pointers and dynamic
    memory allocation

=====
dynamic array of objects?
static array of pointers to objects?
dynamic array of pointers to objects?
...yes, all of these can be done, but be careful!
=====

*   see 112lect07-2.cpp!

====
STOP 7 - dynamic memory and classes
         (and the "big 3")

*   when a class has all-static data fields:

    Point p1(1, 1);
    Point p2 = p1;

    PlayerChar alphonse("Alphonse", 25, 10.6, "armour", 30);
    PlayerChar edward = alphonse;

    *   if you do not explicitly declare and define
        an overloaded = operator (overloaded assignment operator)
	for a class,
	the one you get by default copies the data fields
	    of the RHS into the data fields of the LHS

    *   what about when a Point or PlayerChar is
        passed-by-value?

        ... how is that copy made, to copy an argument
	into a pass-by-value Point or PlayerChar parameter?

	By a method called the copy constructor,
	which the SYSTEM calls when appropriate
	(like for pass-by-value object parameters!)

    *   and at the end of a method or function,
        how is a Point or PlayerChar object
	disposed of?

	using the default destructor method
	that is created if you don't explicitly
	create one.

*   so -- the defaults for these Big 3:
    destructor
    copy constructor
    overloaded assignment operator

    work fine for classes whose data fields
    are all single-valued static memory.

    WHAT IF:
    ...one of the data fields is a pointer to
       a dynamically-allocated array?

    *   the default destructor won't free the
        memory for that data field's dynamically-allocated array --
	that's a memory leak!

        if your class' constructors and/or methods
	dynamically allocate memory using new,
	you PROBABLY need to write a destructor
	to call delete as needed

    *   and for overloaded assignment and copy
        constructor:
	...if you copy a pointer data field,
	   the copy's data field points to the
	   SAME value in memory as the original
	   (they're aliased...)

           ^ that's a so-called shallow copy

           if you want a so-called deeper copy,
	   say making a new array and copying over
	   the original's contents into the copy's
	   copy,
	   you explicitly define a copy constructor
	       and overloaded assignment operator
           
    *   we'll make a class that should have explicitly-defined
        destructor, copy constructor, and overloaded assignment
	operator methods next week,
	and talk about how you can write these as part of that.