Polymorphism Lab

go to your lab directory

copy over the files provided for you ( add the recursive flag -r  )

then copy over the Class files from your inheritance  homework 
cp ../../hw/hw07/Shape.h original/
cp ../../hw/hw07/Line.h original/
cp ../../hw/hw07/Square.h original/
cp ../../hw/hw07/Cube.h original/ 
cp ../../hw/hw07/Rectangle.h original/
cp ../../hw/hw07/Rectangular_Prism.h original/

cd into the directory original

if your classes do not have destructors add them now, just call LogStart and LogEnd

if you run runme1 (main1.cpp) and look at main1.log you should see that when we create an instance of a class  both the constructors and the destructors are called for the actual type we are creating as well as those of the base classes

the creation of a  Rectangular_Prism object calls all the constructors and destructors of the base classes in the order of inheritance 

the constructors are called in this order

first    Shape()
then    Line()
then    Rectangle()
then   Rectangular_Prism()

and the destructors in this order

first   ~Rectangular_Prism()
then    ~Rectangle()
then    ~Line()
then    ~Shape()

if you run runme2 (main2.cpp) you will see that this is the case for all the classes
the constructors and destructors are called for all the base classes

runme3 (main3.cpp)  uses pointers to the base class to store disparate types of classes
note that when it runs it does not call the correct ToString functions
 since it is pointer to the base class it always calls the ToString functions of the base class 

we can fix this by casting the pointer to the correct type first

runme4 (main4.cpp)

if we cast the pointer to the correct type it will then call the correct ToString function


here we have shuffled the array.. we do not know what to cast the pointer to
is it a Shape, Line, Rectangle, Cube??? we don't know
but we can fix that 

first copy all your .h file the virtual directory
MAKE sure you have added your destructors if you don't have them
cp *.h ../virtual 

now to be able to determine a classes type at runtime

add a public member named shape_type to the Shape class
in the Shape constructor set it to 0
in the Line constructor set it to 1
in the Square constructor set it to 2
in the Cube constructor set it to 3
in the Rectangle constructor set it to 4
in the Rectangle_Prism constructor set it to 5

you can now identify the type of the class at runtime

if (shapearray[loop]->shape_type == 4)
  // cast as Rectangle

if (shapearray[loop]->shape_type == 1)
  // cast as Line

make these changes to your classes and then main5 should compile and run and you will see that the ToString for each of the 6 classes is called once each

we have not at this point been able to fix the problem with the destructors not being called properly  we need virtual functions to accomplish this


now cd into the directory virtual
make your ToString and descructor function virtual in the base class

the main1.cpp should compile and run like your main5 in the original folder
without anychanges at all to the main........ furthermore the proper descructors will be called